I am trying to figure out what formula I would use to determine a redirection or ricochet of one object impacting another object. I know that the ricochet would be the same angle after the collision that it was prior to the collision. My objects in this example are solid, not water or energy absorbent. I am just unsure of how to calculate that angle.
Can anyone shed some light on this for me? Thanks!
EDIT: I should have mentioned that I am using a typical velocity.x,velocity.y movement. Basically I think I need to know the line between the two objects and then find the line perpendicular to that line so that I can find the angle of impact based on the stationary object's x,y. The angle of departure would be the same angle in an opposite velocity of either velocity.x or velocity.y.
Am I at all close? What math functions do I use to determine these angles?
In research I have found people speaking of vectors, but I have no idea what that is or how to calculate it.
Get out the graph paper - I can get you started with the theory, although this isn't the fastest way to do it. It's faster to use matrices, but it's better to understand what's going on first I think.
You already have a vector - your two numbers (x and y) give you a 2d vector. The vector gives us speed and direction - we know how fast we're moving and what direction.
Bouncing off vertical or horizontal surfaces is easy - if you're bouncing off the ceiling or floor, just reverse the y coordinate of your vector. Your speed was (5,5), now you bounce off the floor and your speed is (5,-5). If you're bouncing off the sides, reverse the x so (5,5) becomes (-5,5).
Angled surfaces is harder, but start with the simple case of bouncing off the floor again. Draw this on that graph paper. The ball comes down at an angle, which you can calculate by taking arctan(velocity.x / velocity.y) , and exits at angle (0 - angle) . Then we need to turn the angle back into a vector using sin and cosine.
Not bad, but there's one bug. We have the new angle, but we lost the speed (the magnitude of the vector). You can prove this by plugging (5,5) and (1,1) into the code above - 5/5 and 1/1 are both "1", so they give the same angle, and the same deflection angle.
We can fix that by finding the speed before we start (the magnitude of the vector; raw speed without direction, AKA the "length" of the vector) and multiplying our new vector by that magnitude. We can get the magnitude of the vector with the Pythagorean theorem.
In your post you describe the "angleOfSurface". The issue I have there is that in my (immediate) circumstance, the stationary object is circular. Do I then define the angle of the object as perpendicular to the slope of the moving object? Is that again done with "atan"?
If the stationary object is round, the math actually gets (amazingly) much easier. Just swap the signs on both coordinates. If you were travelling at (5,5) your new velocity is (-5,-5). Perfect bounce back in the direction you came. If your speed was (0, -5) it's now (0,5). Draw that on graph paper and you'll see what I mean. it's just x=-x, y=-y.
EDIT: Hold on, I probably simplified too much. I assumed your bullet or ball was heading straight on. If your bullet or ball makes a glancing blow, I'm wrong. You need to figure out the tanget of the sphere at the point of impact, and reflect off of the tangent as though it were a flat surface. Use that tangent as surfaceAngle.
EDIT: Hold on, I probably simplified too much. I assumed your bullet or ball was heading straight on. If your bullet or ball makes a glancing blow, I'm wrong. You need to figure out the tanget of the sphere at the point of impact, and reflect off of the tangent as though it were a flat surface. Use that tangent as angleOfSurface.
Okay, you edited before I could explain that. the radius of my stationary object is also at play. I am adding the radius of the stationary object and the radius of my moving object in the collision detection. Do I need to take that into consideration when figuring the bounce angle also?
Okay, you edited before I could explain that. the radius of my stationary object is also at play. I am adding the radius of the stationary object and the radius of my moving object in the collision detection. Do I need to take that into consideration when figuring the bounce angle also?
Hmmn. I haven't done that before. But if you know the two objects have collided, you could draw a line between the center of the two objects, get the angle of that line with arctan(dy/dx), add 90 degrees (I mean M_PI/2 of course) to get the angle of a perpendicular line, and bounce the moving object off of that.
I just tried it on paper - draw a pea hitting a glancing blow at the top of the big melon, draw a line from the pea to the melon's center, cross it with a perpendicular, and reflect off of it. Looks good to me.
Hmmn. I haven't done that before. But if you know the two objects have collided, you could draw a line between the center of the two objects, get the angle of that line with arctan(dy/dx), add 90 degrees (I mean M_PI/2 of course) to get the angle of a perpendicular line, and bounce the moving object off of that.
I just tried it on paper - draw a pea hitting a glancing blow at the top of the big melon, draw a line from the pea to the melon's center, cross it with a perpendicular, and reflect off of it. Looks good to me.
Awesome, that's what I was wondering, so if I understand correctly. I'll use the method you described above using the "arctan(dy/dx) + M_PI/2" for the "deflectAngle". Right?
I should have described my objects more throughly in my OP, however I am quite sure I will use the deflection on a solid angled wall eventually also. I am also sure this thread will benefit many others!!! Thanks Again Smasher!
Awesome, that's what I was wondering, so if I understand correctly. I'll use the method you described above using the "arctan(dy/dx) + M_PI/2" for the "deflectAngle". Right?
I should have described my objects more throughly in my OP, however I am quite sure I will use the deflection on a solid angled wall eventually also. I am also sure this thread will benefit many others!!! Thanks Again Smasher!
You'll use "arctan(dy/dx) + M_PI/2" for the surfaceAngle - that's the angle of the fake line we're bouncing off of. The deflectAngle is still depends on the direction of the ball and of the surface, so it's "(2*surfaceAngle)- angle" , which I think is finally correct.
I may not understand your question, but if I do, it sounds like you are looking for a bounce vector, such as is common in ray tracing. To find this, you need to know the velocity vector ("V") of your object, and the (normalized) normal vector ("N") of the point with which it is colliding. Then your bounce vector is:
B = V - 2*N*dot_product(V,N)
Do a google search on "ray tracing bounce vector" to find more info.