Fastest point within OBB test

Recommended Posts

Looking for the fastest boolean method to check if a point is within an OBB, in 2D. The OBB will always be a square, not a rectangle. Thanks in advance.

Share on other sites
jyk    2094
Quote:
 Original post by drvannostrandLooking for the fastest boolean method to check if a point is within an OBB, in 2D. The OBB will always be a square, not a rectangle.
vector2 diff = point - box.center;return    std::fabs(dot(diff,box.axis[0])) <= box.extent &&    std::fabs(dot(diff,box.axis[1])) <= box.extent;
That should be plenty fast, I would think :)

Share on other sites
nmi    978
So you have a point P and a square as an OBB with center C, side length a and rotation angle alpha. To test if P is inside the square, you can calculate:
1) Move coordinate system so that C is in the origin: P1 = P - C
2) Rotate around the origin so that the square becomes axis aligned: P2 = M * P1, where M is the rotation matrix for -alpha
3) Check if the point is in the bounds: result = abs(P2.x) < a/2 && abs(P2.y) < a/2
If result is true, the point is inside the OBB.
Calculation costs are 2 additions, 2 subtractions and 4 multiplications.

Edit:
Seems I have to type faster...