theotherguy
Newbie
- Joined
- Jul 5, 2003
- Messages
- 5,107
- Reaction score
- 1
So in coding my game, I've come across a problem which I've been trying to solve through code, but because of special cases, has left me with some nasty bugs.
Basically I am trying to implement a laser weapon into my game. The game is a simple top-down shooter reminiscent of asteroids. I have gotten the laser to be able to be aimed and fired, and have been able to determine whether the laser intersects an asteroid by representing the asteroid as a circle and the laser as a line, and then using Java's geometry functions to figure out whether or not the laser and the asteroid intersect.
Well, this works just fine, except for the fact that the laser always passes through the asteroid, which can work, but is unrealistic. So basically I am trying to determine the point of intersection between the line and the circle, and then setting the endpoint of the laser as this point. This will cause the laser to appear to have struck the asteroid, and will allow me to place a neat "melting" particle effect on it.
Here is how I have tried to implement it:
I am basically moving a test point along the line until I discover it intersects the circle.
If the laser and the asteroid are intersecting, then create a new point at the origin of the laser.
Move the X coordinate of this point every cycle according to the slope's X value and move the Y coordinate of the point every cycle according to the slope's Y value until the point intersects the circle. Then, set the endpoint of the laser as this point.
This works just fine in most cases, except when I fire nearly tangent to the circle. When this happens, the number of steps that the point I am checking causes the point to leave the bounds of the circle, and continue into infinity, causing my computer to slow to a crawl, even when I put a reasonable limit on the distance the point can go. I realized that unless I make the distances that the point travels infinitely small, it could potentially miss the circle in almost every case which brings the line nearly tangent to the circle.
I have thought of two mathematical solutions to my problem, but I can't figure out how to emulate them in code.
Solution 1: Create an equation for the circle representing the asteroid, and an equation representing the line. Find the points where these are equal, and pick the one closest to the player. (Probably the easiest, but I'm not sure how to implement it in code)
Solution 2: Set up a differential calculus equation in which the variables are the changes in x and y experienced by the test point. Find the first exact point of intersection this way. (impossible to implement)
Any ideas?
Basically I am trying to implement a laser weapon into my game. The game is a simple top-down shooter reminiscent of asteroids. I have gotten the laser to be able to be aimed and fired, and have been able to determine whether the laser intersects an asteroid by representing the asteroid as a circle and the laser as a line, and then using Java's geometry functions to figure out whether or not the laser and the asteroid intersect.
Well, this works just fine, except for the fact that the laser always passes through the asteroid, which can work, but is unrealistic. So basically I am trying to determine the point of intersection between the line and the circle, and then setting the endpoint of the laser as this point. This will cause the laser to appear to have struck the asteroid, and will allow me to place a neat "melting" particle effect on it.
Here is how I have tried to implement it:
I am basically moving a test point along the line until I discover it intersects the circle.
If the laser and the asteroid are intersecting, then create a new point at the origin of the laser.
Move the X coordinate of this point every cycle according to the slope's X value and move the Y coordinate of the point every cycle according to the slope's Y value until the point intersects the circle. Then, set the endpoint of the laser as this point.
This works just fine in most cases, except when I fire nearly tangent to the circle. When this happens, the number of steps that the point I am checking causes the point to leave the bounds of the circle, and continue into infinity, causing my computer to slow to a crawl, even when I put a reasonable limit on the distance the point can go. I realized that unless I make the distances that the point travels infinitely small, it could potentially miss the circle in almost every case which brings the line nearly tangent to the circle.
I have thought of two mathematical solutions to my problem, but I can't figure out how to emulate them in code.
Solution 1: Create an equation for the circle representing the asteroid, and an equation representing the line. Find the points where these are equal, and pick the one closest to the player. (Probably the easiest, but I'm not sure how to implement it in code)
Solution 2: Set up a differential calculus equation in which the variables are the changes in x and y experienced by the test point. Find the first exact point of intersection this way. (impossible to implement)
Any ideas?