Link to home
Start Free TrialLog in
Avatar of sanctimonious
sanctimonious

asked on

Calculating reflection vector

I'm hoping my question ends up being very simple to answer, yet it troubles me a lot.


I have two 2D direction vectors A and B.

All I want to do is calculate the a new direction for vector A based on how it would reflect or "bounce" when hit on vector B. Imagine a ball hitting a wall, if you will, to get the basic idea.  2D. A and B are sometimes paraller with the X or Y planes, sometimes not.

Now, here's the trick. I've been using an algorithm often mentioned in the tutorials around the net:

R = V - ( 2 * V [dot] N ) N

(V = vector A, N = vector B, [dot] stands for dot product)

However, I've noticed some occassions where this algorithm does not work, here is an example of one: http://www.infire.com/vectorproblem.gif


I'd like to either find out what am I doing wrong on calculating the reflection or in case the whole algorithm is faulty, find out how to really calculate the angle so that it works every single time.


I've also tried another algorithm, R = 2 ( V [dot] N ) N - V. This works fine on that example above, but it's faulty on another occassion: http://www.infire.com/vectorproblem2.gif


Of course, now that I think of it, maybe I really am doing something drastically wrong... Please tell me what. Points to person who can give me an algorthm or -even better- pseudo code for calculating this thing.

ASKER CERTIFIED SOLUTION
Avatar of SethHoyt
SethHoyt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SethHoyt
SethHoyt

As a note, the reason we use normal vectors to represent the surface is that it works in more than just one dimension. For a two dimensional surface, at a given point it looks like a plane, and you can draw the vector normal to the surface at that point. That vector tells you everything you need to know about the surface at that point in space.

Seth
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't see any need for cross products here....

The first equation is correct, the second one is negated. The only reason your diagram for the second one comes out incorrect also is that you are drawing your normal vector incorrectly. You're swapping x and y coordinates.

I just whipped up the derivation for that formula... it's not hard.

The reflected vector R must have the property that (R.N) = -(V.N), this makes it in the opposite direction by an equal amount.

It must also satisfy R - (R.N)N = V - (V.N)N, and this makes the components along the surface equal before and after.

Solving these equations is easy, and yields your first equation.

Seth
I find it kind of humorous that I just had to explain that two wrongs didn't make a right because there were really three wrongs...
I disagree...

Both equations are correct. They're each the negation of the other. In any particular case, they will give you opposite answers - the reflection of A when it hits B, or the reflection of A when it hits the vector perpendicular to B. The second diagram appears to be correct to me. Note that he uses the second equation in the second diagram.
Ah, I see where the coordinates were switched now, but doing so doesn't make any difference...it just makes the drawing slightly inaccurate. The relative positioning of the vectors is still correct.
Why bother having two "correct" equations that give opposite results?

If N does not refer to the normal vector, then it should be called something else. The second equation is not correct if N stands for normal, which it usually does. And there's no reason to add another equation here when the first is perfectly valid. Further, as I've explained, we should not use tangent vectors to the surface since they don't generalize in higher dimensions. So, that alone makes the second equation pretty useless in addition to being unneccesary.

Also, the second diagram IS incorrect, regardless of how it appears to you....  it is quite obvious that he has mixed his signs, as I pointed out before. Just look at the components for B, they are in (y,x) form, but his resultant vector is plotted in (x,y) form. This is an obvious error and I should not have to say more about it. Read what I've said more carefully before replying next time.

Seth
Well you beat me to my response on that one....   slightly inaccurate?  I never disputed that all of these errors leads to a change in sign. That's why three of them led to a sign change. I don't see your purpose for confusing a simple issue....

Look, he should ONLY use the first equation treating N as the normal vector, END OF STORY. There is no need for anything else, I showed how to derive that equation using no additional assumptions, why do you insist on making it more complicated than that... you are not helping, and I have to spend time undoing the damage you keep doing, so please stop it, for all of our sake.

Seth
Avatar of sanctimonious

ASKER

I think SethHoyt is correct, the vector B indeed is a cross product of a (3d) triangle and so does stand to perpendicular direction.  At least trying the suggested cross product on the result and negating it if the z was negative did not work.  And my apologies for drawing a flawed image, that was my fault altogether.  

Now, two points are still a little unclear to me:

I think that the problem with the second equation is that it indeed is perpendicular to the direction I trying to calculate. How do I effectively rotate the N vector to the correct angle (kinda "un-normalize" it, I suppose?) and is it scary and alarming that the second equation did actually work on the first graph?


Thanks for your fast answers!
Ergh, not "un-normalize", rather "un-cross-product-on-x-y-planes-only". :)
Ok, here's the deal...

The second equation happened to work by a fluke, two wrongs made a right. However, you can, in two dimensions only, use that second equation on a tangent vector N (you should call that something different) and it will work. But I suggest that you always use normal vectors rather than tangent vectors to the surface.

If you have a tangent vector to a surface, it is enough to calculate a normal vector only for 1-D surfaces, for an n-dim. surface, you will need n independent tangent vectors to compute the normal. But this is what you should do nonetheless.

The basic idea to getting a normal vector from tangent vectors is that they are perpendicular. In other words, the dot product is zero. So the normal vector is the vector perpendicular to all tangent vectors. You can just write an equation for each tangent vector if you wish of the form (T.N)=0, and you can solve for N. Alternatively, you can use cross products, but it's easier this way, I think.

Seth
There's no need to be hostile. I apologize for wasting your time with my careless mistakes, and I won't offer any new suggestions for you to correct. His original question still isn't answered, though. You've shown the incorrectness of the second diagram, but haven't touched the first. Unless there's something I'm missing, which seems to be a great possiblity, the first equation still gives an incorrect vector in the first example. If I'm missing it, there's a chance he's missing it to.
Your apology is accepted.

I'm only interested in avoiding confusion, and I was becoming upset that you were making it more difficult for me to do that. All I ask is that you try to be more cautious with your responses in the future to avoid that.

As far as the first diagram is concerned, there is nothing wrong with it as long as the vector N is treated as normal to the surface. That was my point from the beginning, it is the only thing he was missing. Everything went downhill from there...

Take a look at that first diagram and draw a line perpendicular to the blue line. This would be the surface, since the blue line should be normal to the surface. You can see the reflection is correct using this interpretation of what the blue (normal) vector means.

Seth
Ah, I see now. Sorry for the confusion.
All right, I believe I have solved my problem. Thanks for your help. Here's a reference to anyone who might having similar troubles, but BE WARNED: This is unsophisticated, unorthodox and probably very wrong way of doing things.

V is a direction represented as a vector
N is a normalized vector, perpendicular to a 3D-triangle (in other words its cross product)


R = 2( N . V ) N - V
P = 2( N . V )

R.x = P * N.x - V.x
R.y = P * N.y - V.y


D = ( N . V )
C = ( V [cross product] N )

if( D > 0.00001 ) AND ( C.z > 0 ) then
     R.x = -R.x
     R.y = -R.y
endif



I feel like splitting the points, because both of you did lead me in right clues. Thanks a bunch!


Hey sanctimonious, I'm glad we could help out. I was beginning to think all that bickering we were doing was distracting from getting to the truth. But I think it helped everyone understand things a bit better.


And malkari, Your points are well deserved, if only for having the guts to put up a fight with me ;)

Although you were starting to get on my nerves there, I know I'm not always right, and it's always good to have some disagreement. Most of the people I know stopped disagreeing with me a while ago (wonder why?), but having the disagreement can be healthy and helps to get at the truth. Thus, I appreciate it when people actually have the nerve to do it with me, it keeps me honest.

So no hard feelings I hope...

Seth
No worries. I'll do a third check next time. :)