So i am making a very basic 2D parking game in javafx (like Parking Mania) and im having a problem with the rotation of my car. It rotates fine, but when the car is rotated, the hitbox of the car doesnt rotate but transforms to a larger non-rotated rectangle around the car object, so i cant track collision accurately. I tried looking everywhere for a solution but havent been successful. Im really new at java so i would appreciate any simple help.
Here is the code:
Main.java
package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class Main extends Application {
private Pane root;
private List<GameObject> walls = new ArrayList<>();
private GameObject wall;
private GameObject player;
private GameObject park;
private GameObject ok;
private Parent createContent(){
/*private GameObject wall;
private GameObject player;*/
root = new Pane();
root.setPrefSize(1380, 720);
BackgroundImage myBI= new BackgroundImage(new Image("parkovisko.png",1380,720,false,true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
root.setBackground(new Background(myBI));
ImageView bg = new ImageView();
player = new Player();
wall = new Wall();
park = new Park();
ok = new Ok();
/*player.setVelocity(new Point2D(1, 0));*/
addGameObject(park, 385, 0);
addGameObject(player, 610, 245);
player.rotateStart();
//addGameObject(wall, 250, 330);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
onUpdate();
}
};
timer.start();
return root;
}
private void addGameObject(GameObject object, double x, double y) {
object.getView().setTranslateX(x);
object.getView().setTranslateY(y);
root.getChildren().add(object.getView());
}
public int h = 0;
Rectangle rectangle = new Rectangle();
private void addWall(GameObject wall, double x, double y){
walls.add(wall);
addGameObject(wall, x, y);
}
private void onUpdate() {
for (GameObject wall : walls) {
if (player.isColliding(wall)) {
// player.setAlive(false);
//System.exit(1);
addGameObject(ok, 350, 175);
} else if (park.isContaining(player) && h == 0) {
addGameObject(ok, 350, 175);
h++;
}
}
player.update();
}
}
private static class Player extends GameObject {
Player() {super(new ImageView("caarr.png"));
//Player() {super(new Polygon( ));
}
}
private static class Wall extends GameObject {
Wall() {
super(new Rectangle(150, 300));
}
}
private static class Park extends GameObject {
Park() {
super(new Rectangle(50, 100, Color.RED));
}
}
private static class Ok extends GameObject {
Ok() {
super(new ImageView("levelcomp.png"));
}
}
@Override
public void start(Stage stage) throws Exception{
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.setTitle("Parking :)");
stage.setScene(new Scene(createContent()));
ArrayList<String> input = new ArrayList<String>();
Wall car = new Wall();
/* Wall car1 = new Wall();
Wall car2 = new Wall();
*/
addWall( car, 800, 350);
/*
addWall( car1, 150, 150);
addWall(car2, 350, 350);
*/
stage.getScene().setOnKeyPressed(
new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent e) {
String code = e.getCode().toString();
if ( !input.contains(code) )
input.add( code );
}
});
stage.getScene().setOnKeyReleased(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
input.remove( code );
if (e.getCode() == KeyCode.UP) {
player.setVelocity(new Point2D(0, 0));
}
if (e.getCode() == KeyCode.DOWN) {
player.setVelocity(new Point2D(0, 0));
}
}
});
new AnimationTimer()
{
public void handle(long currentNanoTime)
{
if (input.contains("UP"))
player.move();
if
(input.contains("LEFT") && input.contains("UP"))
player.rotateLeft();
if (input.contains("RIGHT") && input.contains("UP"))
player.rotateRight();
if (input.contains("DOWN"))
player.moveback();
if (input.contains("RIGHT") && input.contains("DOWN"))
player.rotateLeft();
if (input.contains("LEFT") && input.contains("DOWN"))
player.rotateRight();
}
}.start();
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
GameObject.java
package sample;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import java.util.ArrayList;
public class GameObject {
private Node view;
private Point2D velocity = new Point2D(0, 0);
public GameObject(Node view){
this.view = view;
}
public void update(){
view.setTranslateX(view.getTranslateX() + velocity.getX());
view.setTranslateY(view.getTranslateY() + velocity.getY());
}
public void setVelocity(Point2D velocity) {
this.velocity = velocity;
}
public Point2D getVelocity() {
return velocity;
}
public Node getView() {
return view;
}
public double getRotate(){
return view.getRotate();
}
public void rotateRight() {
view.setRotate(view.getRotate() + 0.9);
}
public void rotateLeft() {
view.setRotate(view.getRotate() - 0.9);
}
public void rotateStart() {
view.setRotate(view.getRotate() - 90);
}
public void rotate(){
view.setRotate(view.getRotate() - 90);
}
public boolean isColliding(GameObject other){
return getView().getBoundsInParent().intersects(other.getView().getBoundsInParent());
}
public boolean isContaining(GameObject other){
return getView().getBoundsInParent().contains(other.getView().getBoundsInParent());
}
public void move(){
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())), Math.sin(Math.toRadians(getRotate()))));
}
public void moveback(){
setVelocity(new Point2D(0 - Math.cos(Math.toRadians(getRotate())), 0 - Math.sin(Math.toRadians(getRotate()))));
}
public void moves(GameObject obj, ArrayList<String> i){
if (i.contains("UP")){
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())), Math.sin(Math.toRadians(getRotate()))));
}
}
}