Search code examples
collision-detectionpointrectanglescalculationhittest

Make rectangle and check point location against it without changing concept very much if we imagine it being rotated by 45°?


Take a look at this simple combination of rectangle zone creation and point location.

enter image description here

Whilst current calculation retrieves a TRUE for this point being inside the calculated area, the new adjusted calculation should interpret the same point position as FALSE, as if object would have been rotated by 45°(this point would be outside now).


Solution

  • Use X+Y and X-Y in the conditional checks for the rotated rectangle.

    Basically you compare to a line y=m*x+b with m=tan ±45°=±1

    Y>y=1x+b=X+b can be simplified to Y-X>b (and the same for <) And Y>-1x+b=-X+b to Y+X>b (and the same for <)

    The constants in the checks (16 in your case) would have to be multiplied by sqrt(2) for a square of the same size. (or X±Y divided by sqrt(2))

    point1 Y-X <= Y-X position of object - 16*sqrt(2)
    point1 Y-X >= Y-X position of object + 16*sqrt(2)
    point1 X+Y >= X+Y position of object + 16*sqrt(2)
    point1 X+Y <= X+Y position of object - 16*sqrt(2)