I have a basic path-finding program where I want the rectangle to follow my mouse. I'm using this specific method of path-finding because I want to expand on it in the future. I have 2 methods: one which gets the angle between 2 rectangles, and one that moves a rectangle at an angle x amount. For some reason, the rectangle only follows my mouse when it's in front of it, not when it's behind it.
If I get rid of the Math.abs
for my angle method, the problem still persists.
Below are my 2 basic classes.
PathfindingTest.java
, which is the main class and initializes everything:
package pathfindingtest;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
*
* @author Preston Tang
*/
public class PathfindingTest extends Application {
private final long[] frameTimes = new long[100];
private int frameTimeIndex = 0;
private boolean arrayFilled = false;
private double mouseX, mouseY;
@Override
public void start(Stage stage) {
Pane base = new Pane();
base.setStyle("-fx-background-color: rgb(" + 40 + "," + 40 + ", " + 40 + ");");
Entity virus = new Entity(100, 100, 20, 5, Color.RED);
Entity cell = new Entity(700, 450, 20, 5, Color.GREEN);
base.getChildren().add(virus);
base.getChildren().add(cell);
base.setOnMouseMoved((MouseEvent event) -> {
mouseX = event.getX();
mouseY = event.getY();
});
Scene scene = new Scene(base, 800, 550);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
//To get frame data
long oldFrameTime = frameTimes[frameTimeIndex];
frameTimes[frameTimeIndex] = now;
frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length;
if (frameTimeIndex == 0) {
arrayFilled = true;
}
if (arrayFilled) {
long elapsedNanos = now - oldFrameTime;
long elapsedNanosPerFrame = elapsedNanos / frameTimes.length;
double frameRate = 1_000_000_000.0 / elapsedNanosPerFrame;
// System.out.println(String.format("Current frame rate: %.3f", frameRate));
}
if (!(mouseX == 0) && !(mouseY == 0)) {
virus.move(virus.getAngle(virus.getX(), virus.getY(), mouseX, mouseY), 1);
}
}
};
timer.start();
stage.setTitle("Pathfinding Test Application Started 9/9/2019");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Entity.java
, which extends a Rectangle
, and is where the math for path-finding is calculated:
package pathfindingtest;
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/**
*
* @author Preston Tang
*/
public class Entity extends Rectangle {
private double x, y, size, speed;
private Color c;
public Entity(double x, double y, double size, double speed, Color c) {
this.setX(x);
this.setY(y);
this.setWidth(size);
this.setHeight(size);
this.speed = speed;
this.setFill(c);
}
public double getDistance(Rectangle r1, Rectangle r2) {
return Math.sqrt((r2.getY() - r1.getY()) * (r2.getY() - r1.getY()) + (r2.getX() - r1.getX() * (r2.getX() - r1.getX())));
}
public double getAngle(double x1, double y1, double x2, double y2) {
double ang = Math.atan(Math.abs((y2 - y1) / (x2 - x1)));
System.out.println("delta x: " + (x2 - x1) + " delta y: " + (y2 - y1) + " + Angle: " + Math.toDegrees(ang));
return ang;
}
public void move(double angle, double distance) {
this.setX(this.getX() + (distance * Math.cos(angle)));
this.setY(this.getY() + (distance * Math.sin(angle)));
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public Color getC() {
return c;
}
public void setC(Color c) {
this.c = c;
}
}
Thank you for your help!
You are calling Math.atan
, which returns a value between -pi/2 (-90 degrees) to pi/2 (90 degrees) ( https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan(double) )
Try switching to Math.atan2
, which returns the full range of -pi to pi ( https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double) )