Jump to content
  • Advertisement
Sign in to follow this  
marcus12

Ray Tracing in 2d

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

p4----------p3
| |
| |
p1----------p2


//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
Advertisement
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

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.


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
So the problem seems to be that the straight line containing the ray intersects the segment at a point behind the camera? This is normal and expected. Just ignore any solution with negative t.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!