Jump to content
  • Advertisement
Sign in to follow this  
StasB

Help needed with some basic vector math...

This topic is 3613 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

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 this post


Link to post
Share on other sites
Advertisement
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)

That way you can just start with a vector and just adjust it for reflection/refraction.

Share this post


Link to post
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.

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!