Jump to content
  • Advertisement
Sign in to follow this  
AntonyCoder

Given a surface normal and a direction vector, how do I reflect?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I reflect a direction vector based on a surface normal? The way I'm doing it atm is this,

  if SurfNorm.x<>0 DirVec=-DirVec
  if SurfNorm.y<>0 dirvec=-dirvec


which works to a degree, it always reflects, it just some times reflects in the wrong direction. it only works on 90 degree surfaces for pretty obvious reasons. Less obvious (to me) is how to reflect it properly. I have a vector lib so I can almost anything required, I just need a pointer in the right direction please :) (Pun of course intended :P)

Share this post


Link to post
Share on other sites
Advertisement
This should do the trick for perfect elastic reflection:


dir -= norm * 2.0f * norm.dot(dir);




If you want to know why it works its because the dot product tells you how much one vector lies in the direction of the other. In other words its like projecting one vector onto the other and taking its projected length. So once you know how much the direction vector is pointing in the normal direction you take twice this length off if the normal direction to reflect around the normal. You can also model plastic reflections and any degree in between by using a number between 1.0f and 2.0f instead (where 1.0f is perfect plastic and 2.0f is perfect elastic).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!