## Ricochet trig

• 556 Views
• 6 Replies
Deathcon7
Deathcon7
• Member since: Oct. 1, 2003
• Offline.
Forum Stats
Member
Level 21
Writer
Ricochet trig 2008-02-25 00:00:13

So I'm trying to program some richochet effects into my game. What I can't seem to wrap my head around is the reflection. I thought it was going to be as simple as inverting either the x or y velocities on contact but the slope of the surface is taken into account as well as it affects the inbound/outbound angles. The illustration below is an example of what I mean. I'm trying to figure out at what angle the blue line would shoot away. Assume that all energy transfers and all that. I'm not trying to make a painfully accurate simulation, I'm only trying to create a rebound effect.

BoMToons
BoMToons
• Member since: Nov. 29, 2005
• Offline.
Forum Stats
Moderator
Level 38
Game Developer
Response to Ricochet trig 2008-02-25 00:26:25

At 2/25/08 12:00 AM, Deathcon7 wrote: So I'm trying to program some richochet effects into my game. What I can't seem to wrap my head around is the reflection. I thought it was going to be as simple as inverting either the x or y velocities on contact but the slope of the surface is taken into account as well as it affects the inbound/outbound angles. The illustration below is an example of what I mean. I'm trying to figure out at what angle the blue line would shoot away. Assume that all energy transfers and all that. I'm not trying to make a painfully accurate simulation, I'm only trying to create a rebound effect.

Check out this thread where Glaiel and Delta discuss "normal detection" engines for doing what you're talking about. I've tested both sets of code and they work great.

http://www.newgrounds.com/bbs/topic/5059 59

23450
23450
• Member since: May. 28, 2003
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to Ricochet trig 2008-02-25 00:59:07

Here is a small picture of how its all done. After the object hits the wall, you find the normal line using glaiels code. After you have this code, you take the difference between the normal angle and the incomming angle, and use that to get the outgoing angle. The outgoing angle is the same distance away from the normal line as the incomming angle. So angle 1 is equal to angle 2, in relation to the normal.

dELtaluca
dELtaluca
• Member since: Apr. 16, 2004
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Ricochet trig 2008-02-25 08:29:46

At 2/25/08 12:59 AM, 23450 wrote: Here is a small picture of how its all done. After the object hits the wall, you find the normal line using glaiels code. After you have this code, you take the difference between the normal angle and the incomming angle, and use that to get the outgoing angle. The outgoing angle is the same distance away from the normal line as the incomming angle. So angle 1 is equal to angle 2, in relation to the normal.

you don't have to deal with angles once you have the normal, that only complicates things.

given the normal N, and the incident vector V, the reflected vector R is given by R = V-2N(N.V) where . denotes the dot product. if R,V,N are objects with x and y members, this suffices.

var dot:Number = -2*(N.x*V.x+N.y*V.y);
R.x = V.x + dot*N.x;
R.y = V.y + dot*N.y;

Deathcon7
Deathcon7
• Member since: Oct. 1, 2003
• Offline.
Forum Stats
Member
Level 21
Writer
Response to Ricochet trig 2008-02-25 14:49:26

At 2/25/08 08:29 AM, dELtaluca wrote: you don't have to deal with angles once you have the normal, that only complicates things.

given the normal N, and the incident vector V, the reflected vector R is given by R = V-2N(N.V) where . denotes the dot product. if R,V,N are objects with x and y members, this suffices.

var dot:Number = -2*(N.x*V.x+N.y*V.y);
R.x = V.x + dot*N.x;
R.y = V.y + dot*N.y;

So what I end up doing is using an mc to find the normal, or the perpendicular angle to the ground, and then use atan 2 to find the radian measure from the object mc to the normal. Flipping that along the normal will result in the angle i need to direct the object.

GustTheASGuy
GustTheASGuy
• Member since: Nov. 2, 2005
• Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Ricochet trig 2008-02-25 16:46:08

No you don't need the angle of the object if you calculate it using a dot product.
The dot product is if one vector is the hypotenuse the length of the adjacent along the other vector. Therefore the dot product with the normal indicates the force of the collision in relation to the surface and subtracting it twice inverts the movement in relation to the normal. You can figure it out with the image above.

Deathcon7
Deathcon7
• Member since: Oct. 1, 2003
• Offline.
Forum Stats
Member
Level 21
Writer
Response to Ricochet trig 2008-02-25 17:12:02

Right right, so all I have to do is use the formula delta gave me. I haven't really taken the time to sit down and analyze it, and since my trig isn't all that sharp I need some time to test it out and see how things play out. But i think I get it theoretically now, all i need is to plug it in.