# Help needed with some basic vector math...

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

## Recommended Posts

Hi. I'm writing a 2D ray-tracer and I'm trying to implement refractions. When a ray hits a surface, I can easily compute the angle of incidence and the angle or refraction for the ray, but I'm having trouble constructing the vector of the refracted ray, based on the normal vector and the refraction angle. If I have two unit vectors originating from the same point, one of which is given and the angle between the two is given, how should I go about computing the (components of the) second vector? I came up with two equations based on the definition of the dot product and some trigonometry: Ax * Bx + Ay * By = cos(a) (Ax - Bx)^2 + (Ay - By)^2 = 2 * (1 - cos(a)) They look perfectly solvable, but it really looks like an ugly way of doing it. I've seen many examples of code online (with no explanation of how it was derived) which look much cleaner than the solution to the above system of equations. Could anyone please show me (and explain) a neat way of solving this problem? Thanks in advance. ;D Stas B.

##### Share on other sites
In 2D, you can just apply the rotation equations to rotate your incoming vector by the required refraction angle:

X' = X*cos(a) - Y*sin(a)
Y' = X*sin(a) + Y*cos(a)

##### Share on other sites
In 3D it's: Imagine a triangle ABQ', you need to find a point B'. Normal to the plane is a vector AQ, the reflected vector is a BA. Q' is shadow of BA casted on a normal. Q' = cos(BAQ')*AQ. Because both BA and AQ are normalized, you can write Q' = BA DOT AQ * AQ. Then it's the easy part. Q' is between B and B' thus: B' = B + 2*(Q'-B) ... B' = 2*Q' - B

B' = 2*(BA DOT AQ) * AQ - B

It might actually work, try it. I did something similar in few integrators.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013166
×