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

This topic is 4481 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
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632591
• Total Posts
3007250

×