1

I have try many things without finding the good solution so here I am.
In my game (2D) I have to check collision with all my Object (house, garage..) which are image inside Rotated Rectangle, between a ray from a Point A to Point B.

I'm using Xna and there some code:

public void Update(List<Obstacle> Lob, DragObj Ldo)
{
    bool inter = false;
    Point A;
    Point B;

    A = new Point((int)pos.X, (int)pos.Y);
    B = new Point((int)Ldo.Position.X, (int)Ldo.Position.Y);
    for (int j = 0; j < Lob.Count(); j++) 
    {
        if (inter = interclass.LineIntersectsRect(A, B, Lob[j].Shape)) // I have this for the moment, Shape is the rectangle but not rotated )
        {
            inter = true;
            islight = false;
        }
        else
        {
            inter = false;
        }
    }
}

So to solve my problem, whether I find a solution to have a rotatedRectangle Object with a method to check collision with line. Whether totally something else, maybe only check collision between yy straight and each rotated Rectangle Axis?

Thanks for your advices.

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Gabson
  • 421
  • 1
  • 4
  • 20
  • Try rotating the line instead, it's just a little trig. – MickLH Nov 27 '13 at 13:39
  • Rotated the line will change my 2D space organisation, doesn't it ? I'm gonna try to rotate my 2 points, but its wont be easy to implement in my current code. – Gabson Nov 27 '13 at 13:47
  • but you can imagine it right? if you take both together and rotate so the rectangle is straight, you can use the regular axis-aligned rectangle function – MickLH Nov 27 '13 at 14:24
  • Y I have draw it to imagine it and its ok. But It has a strange behavior. Intersection is working but not in the right place, i also tried with radians angle. I use this to rotate my point : In 2D, you make (X,Y) from (x,y) with a rotation by angle t so: X = x cos t - y sin t Y = x sin t + y cos t – Gabson Nov 27 '13 at 14:50

2 Answers2

0

I have solver this problem by checking intersection between my line and each side of the rotated Rectangle (I have to rotate each Line-side of the rectangle first). I will post the little algo soon.

Gabson
  • 421
  • 1
  • 4
  • 20
0

I don't know C# but... There is this algorithm that can get the closest point on a line from a point. (Note that the closestPointOnLine function is not my code)

var closestPointOnLine = function(line1, point1)
{ 
    var A1 = line1.y2 - line1.y1;
    var B1 = line1.x1 - line1.x2;

    var C1 =  A1 * line1.x1 + B1 * line1.y1;
    var C2 = -B1 * point1.x + A1 * point1.y;

    var det = A1 * A1 + B1 * B1;

    var cx = 0;
    var cy = 0;

    if(det !== 0)
    { 
        cx = ((A1 * C1 - B1 * C2) / det);
        cy = ((A1 * C2 + B1 * C1) / det);
    }else{
        cx = point1.x;
        cy = point1.y;
    }

    return {
        x : constrain(cx, Math.min(line1.x1, line1.x2), Math.max(line1.x1, line1.x2)),
        y : constrain(cy, Math.min(line1.y1, line1.y2), Math.max(line1.y1, line1.y2)),
    }; 
};

Before we go any further let's make it clear that: Our line is:

var lineToTest = {
    x1: someNumber
    y1: someNumber,
    x2: someNumber,
    y2: someNumber
};

And that our rotated rectangle contains:

var rectToTest = {
    points: [
       {
          x : someNumber,
          y : someNumber,
       },
       {
          x : someNumber,
          y : someNumber,
       },
       {
          x : someNumber,
          y : someNumber,
       },
       {
          x : someNumber,
          y : someNumber,
       },
    ],
};

We then take the lineToTest and take it's first point and begin using the closestPointOnLine to get a point on a line of the rectToTest, We then check if that point is touching the lineToTest, if it is not repeat for the other lines in the rectangle.

Now I don't actually know the code to check if a point is touching a line: But it might go something like this:

function isLineTouchingPoint(line1, point)
{
     //Other code here
     //You'll have to use trigonometry for this one though

     return boolean;
}

Now you could convert this code into C# to get it to work.