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

## 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 on other sites
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).

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011318
• ### Who's Online (See full list)

There are no registered users currently online

×