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