hi I the original question can be found with the following link but it was put on hold because I did not relate it to my implementation so I will do this now.
I found a solution to this problem in another thread but I always get wrong results with this eventhough the output seems reasonable...
Intersection of two Moving Objects with Latitude/Longitude Coordinates
my code is straightforward. I have a point class (not java.awt.Point since my points always lie on exact integers) that should help me determining the problem.
public static class Point {
/**
* the x position of this position
*/
private int x;
/**
* the y position of this position
*/
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/**
* calculates the Euclidean distance between this position and another one.
*
* @param point the position to which the distance should be calculated.
* @return the Euclidean distance
*/
public int getDistance(Point point) {
int dx = Math.abs(x - point.getX());
int dy = Math.abs(y - point.getY());
return (int) Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
/**
* calculates the angle between two points.
*
* @param target the point to which the angle should be calculated
* @return the angle in degrees
*/
public double getAngle(Point target) {
double angle = Math.toDegrees(Math.atan2(target.y - y, target.x - x));
if (angle < 0) {
angle += 360;
}
return angle;
}
public Point getBestIntersectionPoint(int mySpeed, Point target, int targetSpeed, Point targetsTarget) {
double distance = getDistance(target);
double angle = 180 - Math.abs(target.getAngle(this) - target.getAngle(targetsTarget));
double a = Math.pow(mySpeed, 2) - Math.pow(targetSpeed, 2);
double b = 2 * distance * targetSpeed * Math.cos(Math.toRadians(angle));
double c = -Math.pow(distance, 2);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
//Finding out the roots
double temp1 = Math.sqrt((Math.pow(b, 2)) - 4 * a * c);
double root1 = (-b + temp1) / (2 * a);
double root2 = (-b - temp1) / (2 * a);
Point intersection1 = target.getPositionInTurns(targetsTarget, targetSpeed, root1);
Point intersection2 = target.getPositionInTurns(targetsTarget, targetSpeed, root2);
int distance1 = intersection1.getDistance(target);
int distance2 = intersection2.getDistance(target);
int targetDistance = target.getDistance(targetsTarget);
System.out.println(angle + " - " + intersection1 + "; " + intersection2);
return null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point point = (Point) o;
if (getX() != point.getX()) return false;
return getY() == point.getY();
}
}
the problem here is the method "getBestIntersectionPoint". For example lets say objects 1 is on coordinates (x=1100,y=1200), object 2 on (x=8250, y=4500) and object 3 is on (x=8250, y=8999) the velocities keep the same as in the picture of the other thread. the correct intersection point should be approximately on (x=8250, y=5000). But with this implementation I get two intersections not even close to this point...
Point{x=8250, y=-4738}; Point{x=8250, y=8538}