# Vector Reflection

This topic is 4451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok i have been trying to get my reflection function to work for a while now and i'm having no such luck. i want to be able to input an incident vector and the surface normal and get the reflected vector or the incident vector. i have been using the formula I' = I-2 * dot(N,I) * N; I is the incident vector N is the normal dot() is the dot product does this formula only work for normals that are not parallel to an axis (ie X, Y, or Z axes)? the incident vector was originally along the same axis but in the opposite direction and it went all weird, giving me odd results. should this function work for incident vectors perpendicular and parallel to the normal? also here is my dot product function. its possible there is something wrong here float Dot3(Vector3 vector1, Vector3 vector2) { return (vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z); }

##### Share on other sites
Quote:
 Original post by adam17ok i have been trying to get my reflection function to work for a while now and i'm having no such luck. i want to be able to input an incident vector and the surface normal and get the reflected vector or the incident vector. i have been using the formula I' = I-2 * dot(N,I) * N;I is the incident vectorN is the normal dot() is the dot productdoes this formula only work for normals that are not parallel to an axis (ie X, Y, or Z axes)? the incident vector was originally along the same axis but in the opposite direction and it went all weird, giving me odd results. should this function work for incident vectors perpendicular and parallel to the normal?also here is my dot product function. its possible there is something wrong herefloat Dot3(Vector3 vector1, Vector3 vector2){ return (vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z);}
Your dot product looks correct, and the reflection equation should work for any orientation of I and N. Keep in mind though that N needs to be unit length.

##### Share on other sites
hmm well im not sure what is going on if those are right. i have a point moving with a velocity of -0.1 on the X axis and -0.1 on the Y axis. when it collides with a plane, the point reflects along the normal. since the normal is 1,0,0, the point travels along the normal.

any ideas

##### Share on other sites
Quote:
 when it collides with a plane, the point reflects along the normal. since the normal is 1,0,0, the point travels along the normal.

The values you present, yield I' = (-.1, .1, 0) which is -clearly- not colinear to N = (1,0,0).
How can the point travel along the normal then?

##### Share on other sites
im not sure what happens. the point travels along the path (-0.1,-0.1, 0) until it collides with the plane. once it bounces off of the wall it then changes its velocity to (0.1, 0, 0).

##### Share on other sites
Then it seems that the result is -somehow- overwritten, right after you've calculated it... What do you get on the debugger?
Have you tried stepping into the heart of the calculations?

Also make sure that your dot product function is exactly as posted above, and the multiplications are indeed component-wise.
There can't be a mistake anywhere else. It's the only thing used there...

##### Share on other sites
ok i think ive narrowed it down. i manually calculated everything. i used the incident vector of (-0.1, -0.1, 0.0) and a normal of (1,0,0). i got (.1414, 0, 0). the normal y and z values limit the reflected angle to only the x-axis. ill show you what i found:

i=<-0.1, -0.1, 0> n=<1, 0, 0>
r = (i-2) * n * dot(i,n)
r = (-1.414, -1.414, 0) * (1, 0, 0) * ((-0.1*1) + (-0.1*0) + (0*0))
r = (-1.414, -1.414, 0) * (1, 0, 0) * -0.1
r = (-1.414, 0, 0) * -0.1
r = (.1414, 0, 0)

there has to be a better reflect equation out there unless i somehow messed this calculation up too.

##### Share on other sites
Quote:
 Original post by adam17i=<-0.1, -0.1, 0> n=<1, 0, 0>r = (i-2) * n * dot(i,n)

Well this is definately wrong. i is a vector, 2 is a scaler. You can't subtract a scaler from a vector :)

Shouldn't it be r = i - (2 * n * dot(i, n)) ...?

##### Share on other sites
w00t!!!!! hell fraking yeah!

that works now! i really appreciate it!

##### Share on other sites
ok now i have a couple of balls bouncing around on the screen. now i want to implement a way for them to bounce off of each other when they collide. unfortunately im not sure on how to go about this. im assuming i can still use the reflect function i have but what do i use as the normal?

• 9
• 9
• 13
• 41
• 15