Question

Calculating reflection vector

Asked by: sanctimonious

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.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-06-24 at 09:42:32ID20658459
Tags

vector

,

reflection

Topic

Physics & Artificial Intelligence in Game Programming

Participating Experts
2
Points
500
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. 2-D reflection Vector
    Given that I know the coordinates of the points A, B, C which form vectors AB and AC in a 2 dimensional plane. I would like to find the Vector AD such that AC bisects AD and AB, meaning the angle between AB and AC is equal to the angle between AC and AD. Below was one answer ...
  2. Vector Math
    Lets say you have 2 vectors which are normals, A and B. how would you go about findind the rotations, so that you can rotate Vector A to equal Vector B. thx in advanced
  3. Colliding balls and reflection angles
    Hi! I'm writing a game(c#) based on the old one called "Jezzball". My problem is the ball physics. I have a class for the balls class Ball { Point pos; Vector speed; int diameter; . . . static public void Collide(Ball a, Ball b) { //How do I calculate th...
  4. Vectors
    Hello, this is a quick question. How would I write a method that loads values into a vector and calculate the high, median, average and low number from the vector. Also to print out the contents in the vector?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SethHoytPosted on 2003-06-24 at 10:18:37ID: 8791014

Ok, your N is a normal vector, perpendicular (normal) to the surface. If you take your diagrams and draw a line perpendicular to the N vectors, you will see the reflection correctly

Seth

 

by: SethHoytPosted on 2003-06-24 at 10:24:01ID: 8791068

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

 

by: malkiriPosted on 2003-06-24 at 10:28:09ID: 8791098

What you need to is to use the cross product to determine which 180 degree rotation of the solution is the correct one.
If you take the cross product of the first problem's vectors, you'll get a vector (0, 0, -.7072). Assuming you stick with the first equation you gave, you'll want to use a 180 degree rotated version of the vector the equation spits out whenever you have a negative magnitude for the cross product. You can rotate by 180 deg. by flipping the signs of both components.
Now take the cross product of the the second problem's vectors. You get (0, 0, 1.8). If you're still using the first equation, you won't do anything, since this magnitude is positive.
If you prefer the second equation, you can just flip the cases - positive magnitude means rotate, negative means don't.

What you're doing when you calculate the cross product is finding the normal vector of the two original vectors. If you experiment a little, you'll see that this normal's magnitude flips signs under particular conditions. Say we leave vector B as fixed, and only move vector A to illustrate this property. Vector B is horizontal. Vector A starts with its tail above B, and its head lying directly on B. (The best illustration I can come up with: \_ ) In this case, the normal's magnitude will be positive. If you change A so that its tail is also on B (i.e., they're the same vector), the normal's magnitude will be 0. And if you change A so that its tail is below B, the magnitude will be negative.

I couldn't find any particularly good links for you to look at, but if you want more info, look for stuff on the right hand rule, normal vectors, and the cross/vector product.

Good luck!

 

by: SethHoytPosted on 2003-06-24 at 10:50:52ID: 8791251

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

 

by: SethHoytPosted on 2003-06-24 at 10:54:00ID: 8791265

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...

 

by: malkiriPosted on 2003-06-24 at 11:03:20ID: 8791333

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.

 

by: malkiriPosted on 2003-06-24 at 11:38:03ID: 8791556

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.

 

by: SethHoytPosted on 2003-06-24 at 11:50:26ID: 8791659

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

 

by: SethHoytPosted on 2003-06-24 at 11:55:33ID: 8791689

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

 

by: sanctimoniousPosted on 2003-06-24 at 12:05:21ID: 8791768

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!

 

by: sanctimoniousPosted on 2003-06-24 at 12:05:58ID: 8791772

Ergh, not "un-normalize", rather "un-cross-product-on-x-y-planes-only". :)

 

by: SethHoytPosted on 2003-06-24 at 12:16:18ID: 8791856

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

 

by: malkiriPosted on 2003-06-24 at 12:18:13ID: 8791869

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.

 

by: SethHoytPosted on 2003-06-24 at 12:26:44ID: 8791933

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

 

by: malkiriPosted on 2003-06-24 at 12:35:07ID: 8792018

Ah, I see now. Sorry for the confusion.

 

by: sanctimoniousPosted on 2003-06-24 at 15:00:41ID: 8793058

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!


 

by: SethHoytPosted on 2003-06-24 at 18:38:50ID: 8793931

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

 

by: malkiriPosted on 2003-06-25 at 06:44:52ID: 8797407

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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...