Vector Reflection

Started by
18 comments, last by adam17 18 years, 1 month ago
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); }
Advertisement
Quote:Original post by adam17
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);
}
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.
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
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?
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).
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...
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.
Quote:Original post by adam17


i=<-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)) ...?

w00t!!!!! hell fraking yeah!

that works now! i really appreciate it!
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?

This topic is closed to new replies.

Advertisement