Sign in to follow this  
marcus12

Ray Tracing in 2d

Recommended Posts

Hi,
I am trying to use raytrace in 2d for laser. This laser can be translated and rotated.
So for that I have given its direction along X and Y as (Xd=1.0, Yd=1.0);

I am using this logic to calculate if there is a collision or not
//intersection between line and ray
[code]
p4----------p3
| |
| |
p1----------p2

[/code]
//calculate 't': for the ray:
X = Xs +t * Xd and Y = Ys + t * Yd;
where Xs and Ys = Ray Start points and Xd and Yd = Direction

// Using equation of line y-y1= y2-y1/x2-x1 * (x-x1) :
replacing X and Y in line equation from ray equation

I try to solve for t like this:
t = [Xs*(y2 - y1) + Ys*(x1 - x2) + y1*x2 - y2*x1]/[Yd*x2 - x1 *Yd - y2*Xd + y1*Xd]

then using t, I try to get the values of X and Y in ray

Y = Ys + t * Yd;
X = Xs + t * Xd;


Then I try to check the ray X and Y values w.r.t ( P1 & P2 ), ( P2 & P3 ), ( P3 & P4 ), ( P4 & P1 )like
if(p4x <= X && p3x >= X)
{
//Collision happens only considering the X values since Y is constant
}

if(p1y <= Y && p4y >= Y )
{
//Collision happens only considering the Y values since X is constant
}


But I am not getting the correct result.

All suggestions are welcome

Share this post


Link to post
Share on other sites
Set up some simple cases where you can compute the answer by hand, find one where the code doesn't do the right thing and then go through it with a debugger to see what's the first intermediate result you didn't expect. Being able to do this is a very valuable skill.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1318794718' post='4873199']
Set up some simple cases where you can compute the answer by hand, find one where the code doesn't do the right thing and then go through it with a debugger to see what's the first intermediate result you didn't expect. Being able to do this is a very valuable skill.
[/quote]

Hi,
Thanks. My confusion is deciding the Direction of the ray. The laser can be moved in any direction so if the direction is set as (1.0, 1.0) .
Xd = 1, Yd =1

Then making use of the equation to get the value of 't'
t = [Xs*(y2 - y1) + Ys*(x1 - x2) + y1*x2 - y2*x1]/[Yd*x2 - x1 *Yd - y2*Xd + y1*Xd]

then using t, get the values of X and Y in ray

Y = Ys + t * Yd;
X = Xs + t * Xd;

This makes to get values which at some instant does not work properly.

Like if direction is taken along one direction then

x1 =700 x2 =500 y1 = 100 y2 =100
Xs = 532 Ys = 500 Xd = 0 Yd = 1

then t =1 and values come out to be X = 532 Y = 501

but if direction is considered as (1,1) then t = -400 which makes the logic incorrect for the same positions.


if the direction is set according to the rotation then at each 90 degree rotation it can be calculated along which axis it is pointing
then the direction vector can be set like Xd =1 or Yd =1. BUT if the rotation is only for example 45 degree then what direction needs to be assumed. ??

All suggestions are welcome

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this