Archived

This topic is now archived and is closed to further replies.

Calculating a reflected ray

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

This is probably really basic but I''m having trouble working out the code equivalent to this formula: http://astronomy.swin.edu.au/~pbourke/geometry/reflected/ I have the co-ords of the reflective surface x1,y1 x2,y2 (which is a tangent to line N in the formula) and the co-ordinates of the intersecting line and its intersecting point. I''m coding this in ActionScript so any basic C++, C code would be great. I''ve not done Geometry in around 8 years and had enough trouble calculating the intersection. Any help would be great thanks, CJ

Share this post


Link to post
Share on other sites
1) Anywhere you don''t see any operator is usually a multiplication, e.g. 2N means 2*N

2) The upper case letters are all vectors in the link posted, so each has an x, y and z component (for 2D just assume all Z''s are 0). In this post I''m using _ to represent a component rather than . to avoid confusion with the dot product in the link you posted.

3) To multiply a vector by a scalar value (e.g. 2N) you simply multiply each component of the vector by that scalar 2*N_x, 2*N_y, 2*N_z etc.

4) The dot product is simply: dot=A_x*B_x + A_y*B_y + A_z*B_z

5) So from the link you posted: Rr = Ri - 2 N (Ri . N) becomes:
float ridotn = (Ri_x*N_x + Ri_y*N_y + Ri_z*N_z)
Rr_x = Ri_x - 2 * N_x * ridotn;
Rr_y = Ri_y - 2 * N_y * ridotn;
Rr_z = Ri_z - 2 * N_z * ridotn;

Share this post


Link to post
Share on other sites
Thanks for answering S1CA.

I know I'm terrible at Geometry but shouldn't there be a calculation involving either the angles or at least the intersection point.

The only values I see used are one x,y value for N and one x,y value for Ri (for 2d anyway) which doesn't give any information to the angle between the two points, or anything to do with the reflective line.

I could be horribly wrong, if so please explain it to me.

> Oops, I think I somewhat understand now but what defines a component or how do you come about the value. i.e. ri_x? Or am I off the wrong way again.

Cheers,

CJ

[edited by - CaptainJack on June 4, 2003 12:28:33 PM]

Share this post


Link to post
Share on other sites
Nope, the vectors are all you need.

- Those vectors only store directions, and you want to know the reflected direction. You don''t need the location. The location could be anywhere.

- That code gives you the reflection of Ri "around" N, so once again the location and "space" that the vectors is in doesn''t matter - as long as there in the same coordinate system

- A dot B == |A||B|cos(angle), i.e. the cosine of the shortest angle between the two vectors multiplied by the lengths of the two vectors (which will each be 1 if the vectors are normalised) - there''s no need to use angles at all.

- If you''re more familiar with trig, I''d suggest working through the above using cos(angle) [i.e. the angle between the normal and the incoming vector)] on paper - it should become a bit more apparent what''s going on.

Share this post


Link to post
Share on other sites
Yay, Thanks S1CA.

After a bit of work trying to get the values down from 50000x, 42000y it works great (had to divide the components by the length of each line before calculation).

Thanks again,

CJ (off to get his standard grade maths books...)

Share this post


Link to post
Share on other sites
to put it simply

N is the normal of the reflective surface.

D is the direction of the ray

D = D - 2 * ((N.D) * N)

to put ''restitution''

D = D - (1.0f + CoR) * ((N.D) * N)

and friction

Dn = (N.D) * N
Dp = D - Dn

D -= CoF * Dp + (1 + CoR) * Dn

Share this post


Link to post
Share on other sites