Advertisement

06.24.2003 at 09:42AM PDT, ID: 20658459
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Calculating reflection vector

Tags: vector, reflection
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.

Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: sanctimonious
Solution Provided By: SethHoyt
Participating Experts: 2
Solution Grade: B
Views: 183
Translate:
Loading Advertisement...
06.24.2003 at 10:18AM PDT, ID: 8791014

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 10:24AM PDT, ID: 8791068

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 10:28AM PDT, ID: 8791098

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 10:50AM PDT, ID: 8791251

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 10:54AM PDT, ID: 8791265

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 11:03AM PDT, ID: 8791333

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 11:38AM PDT, ID: 8791556

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 11:50AM PDT, ID: 8791659

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 11:55AM PDT, ID: 8791689

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:05PM PDT, ID: 8791768

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:05PM PDT, ID: 8791772

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:16PM PDT, ID: 8791856

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:18PM PDT, ID: 8791869

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:26PM PDT, ID: 8791933

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 12:35PM PDT, ID: 8792018

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 03:00PM PDT, ID: 8793058

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2003 at 06:38PM PDT, ID: 8793931

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.25.2003 at 06:44AM PDT, ID: 8797407

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
06.24.2003 at 10:18AM PDT, ID: 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
Accepted Solution
 
06.24.2003 at 10:24AM PDT, ID: 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
 
06.24.2003 at 10:28AM PDT, ID: 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!
Assisted Solution
 
06.24.2003 at 10:50AM PDT, ID: 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
 
06.24.2003 at 10:54AM PDT, ID: 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...
 
06.24.2003 at 11:03AM PDT, ID: 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.
 
06.24.2003 at 11:38AM PDT, ID: 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.
 
06.24.2003 at 11:50AM PDT, ID: 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
 
06.24.2003 at 11:55AM PDT, ID: 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
 
06.24.2003 at 12:05PM PDT, ID: 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!
 
06.24.2003 at 12:05PM PDT, ID: 8791772
Ergh, not "un-normalize", rather "un-cross-product-on-x-y-planes-only". :)
 
06.24.2003 at 12:16PM PDT, ID: 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
 
06.24.2003 at 12:18PM PDT, ID: 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.
 
06.24.2003 at 12:26PM PDT, ID: 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
 
06.24.2003 at 12:35PM PDT, ID: 8792018
Ah, I see now. Sorry for the confusion.
 
06.24.2003 at 03:00PM PDT, ID: 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!


 
06.24.2003 at 06:38PM PDT, ID: 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
 
06.25.2003 at 06:44AM PDT, ID: 8797407
No worries. I'll do a third check next time. :)
 
 
20080236-EE-VQP-29