Skip to main content
GameDev.net gamedev.net
🔒 Locked

reflection formula

Started by pascalosti Aug 29, 2006 at 6:49 PM 3 replies 850+ views
Original Post
pascalosti
pascalosti
a ball hits a wall the reflecting vector is given by using this formula R = 2N(N dot I) - I

vec2D B ={-(ball.p1.x - ball.p0.x), -(ball.p1.y - ball.p0.y)};
// not sure why extra negatives are there 

vec2D I = Normal(B);

R.x = 2 * wall.N.x * Dot(wall.N,I) - I.x;
R.y = 2 * wall.N.y * Dot(wall.N,I) - I.y;

this is what i think it is: 

dot gives you the angle of ball hitting the wall.
N is where the ball will hit the wall
I is where position where the ball came from

Ezbez
Ezbez
And your question is...?
Jernej.L
Jernej.L
He wants code.. so i am giving it to you, in pascal:

function VectorReflect(const vec, normal: Vector): vector;vard, ol: single;begind:= DotProduct(vec, normal);Result.x:= vec.x - (2 * d) * normal.x;Result.y:= vec.y - (2 * d) * normal.y;Result.z:= vec.z - (2 * d) * normal.z;ol:= vectorsize(vec);result:= Normalize(result);result:= Scalevector(result, ol);end;

Projects: Top Down City: http://mathpudding.com/
xycsoscyx
xycsoscyx
I'm sorry, but Pascal will simply not do, can we please get it in Ada instead?
greenhybrid
greenhybrid
If you have the formula than simply "translate" it to your language.
If you're not able to, buy a "programming for dummies" book...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.