JeroMiya
2004.01.08, 02:05 PM
I had a difficult time finding a simple, elegant solution to a seemingly simple problem.
What I had:
The position of a projectile, it's size, and it's velocity
The intersection point with the terrain it collided with
The normal at the intersection point of the terrain it collided with
The percentage of speed to lose after the bounce
What I wanted:
The resulting position and velocity of the projectile after it collides and bounces off the terrain.
Here was the eventual solution (still approximation, but ok)
N: terrain normal
P: projectile position
V: projectile velocity
NV: normalized projectile speed
I: intersection point (from center of the projectile)
distanceToTerrain: distance between P and I
speed: basically the length of V
size: size of projectile (a bounding sphere)
scale: velocity is scaled by scale after the bounce (so usually between 0.0 and 1.0 for a percentage speed loss after bounce)
V = (V + (N * -2)) * scale
NewNV = normalized V
P = (I - (NV * size)) + (NewNV * (speed - distanceToTerrain - size))
The second line warrants some explanation: The projectile has a size parameter. We just simplify all objects and consider them spheres, so the size may be actual size or a bounding sphere. First we find the actual intersection point, which takes into account the size of the projectile, then we go from that point in the direction of the new velocity. We scale this by the time left over after the projectile travels to the real intersection point to get our new position.
Note about order of operations:
In my game, I have a mutator system which mutates projectiles for different things like gravity, hovering, wind resistance, spring forces, etc.. Projectiles also have an update which adds the velocity to the position of the projectile. Most mutators will modify only speed, and then Update after all the mutators are done mutating the projectiles. I implemented the above algorithm as a mutator as well, but it mutates the position as well as the velocity, essentially doing an update operation at the same time (the other alternative was to place the projectile behind the intersection point so that the next projectile update operation would put it where it was supposed to go). Therefor, you must be careful not to double update. Apply this mutator after the other mutators have been processed, and without a separate update operation. (although I suppose separate Move and Update operations would be better so that you could have animation considerations in Update and just update the position in Move).
Comments? Inaccuracies? Inefficiencies? Let me know.
Jeremy Bell
WolverineSoft Project Coordinator
www.umich.edu/~wsoft
What I had:
The position of a projectile, it's size, and it's velocity
The intersection point with the terrain it collided with
The normal at the intersection point of the terrain it collided with
The percentage of speed to lose after the bounce
What I wanted:
The resulting position and velocity of the projectile after it collides and bounces off the terrain.
Here was the eventual solution (still approximation, but ok)
N: terrain normal
P: projectile position
V: projectile velocity
NV: normalized projectile speed
I: intersection point (from center of the projectile)
distanceToTerrain: distance between P and I
speed: basically the length of V
size: size of projectile (a bounding sphere)
scale: velocity is scaled by scale after the bounce (so usually between 0.0 and 1.0 for a percentage speed loss after bounce)
V = (V + (N * -2)) * scale
NewNV = normalized V
P = (I - (NV * size)) + (NewNV * (speed - distanceToTerrain - size))
The second line warrants some explanation: The projectile has a size parameter. We just simplify all objects and consider them spheres, so the size may be actual size or a bounding sphere. First we find the actual intersection point, which takes into account the size of the projectile, then we go from that point in the direction of the new velocity. We scale this by the time left over after the projectile travels to the real intersection point to get our new position.
Note about order of operations:
In my game, I have a mutator system which mutates projectiles for different things like gravity, hovering, wind resistance, spring forces, etc.. Projectiles also have an update which adds the velocity to the position of the projectile. Most mutators will modify only speed, and then Update after all the mutators are done mutating the projectiles. I implemented the above algorithm as a mutator as well, but it mutates the position as well as the velocity, essentially doing an update operation at the same time (the other alternative was to place the projectile behind the intersection point so that the next projectile update operation would put it where it was supposed to go). Therefor, you must be careful not to double update. Apply this mutator after the other mutators have been processed, and without a separate update operation. (although I suppose separate Move and Update operations would be better so that you could have animation considerations in Update and just update the position in Move).
Comments? Inaccuracies? Inefficiencies? Let me know.
Jeremy Bell
WolverineSoft Project Coordinator
www.umich.edu/~wsoft