Reputation: 19
I am making a 2D shooting game in Java, I need to be able to work out the maximum distance a circle could travel forwards (Only on the Y-axis) and get the distance as a float value, I have looked everywhere and can only find the closest distance to the line, but I only want the forward distance. This image is an example of how I am trying to get it to work, the line is a wall and the circles are bullets. If someone could give me a simple Java function that would be brilliant. I need it to take into account the whole circumference of the circle, so even if it slightly clips the wall with the edge, I need that distance. If the line is above the circle I want it to return a negative value, if the line is below I want a positive value, and if they would not collide, return POSITIVE_INFITITY.
I have tried many different functions I have found online but none seem to suit my purpose of just using the Y axis distance.
Upvotes: 0
Views: 87
Reputation: 6266
Most of the java.awt.geom framework contains helpful math functions.
The Line2D class has the Line2D#ptLineDist method, for calculating this distance, without the radius.
For that, I imagine you're calculating a different angle for the bullet.
This is at least a resource, for that solution.
double a = Line2D.ptLineDist(20, 40, 100, 10, 40, 100);
double b = Line2D.ptLineDist(20, 40, 100, 10, 65, 100);
System.out.printf("a = %s, b = %s", a, b);
Output
a = 63.202219485910504, b = 71.9803055256203
Upvotes: -1