Skip to main content
gamedev.net
🔒 Locked

Point Within Box Check

Started by RegularKid Dec 12, 2008 at 11:02 AM 5 replies 1.3k views
Original Post
RegularKid
RegularKid
I'm trying to detect if a point is within a non-axis aligned box. Here's what I have to work with: - Center point of the box - Width / Height of the box - Rotation of the box - Test point Is there a simple way of detecting if the test point is within this box?
Zakwayda
Zakwayda
Quote:
Original post by RegularKid
I'm trying to detect if a point is within a non-axis aligned box. Here's what I have to work with:

- Center point of the box
- Width / Height of the box
- Rotation of the box
- Test point

Is there a simple way of detecting if the test point is within this box?
Testing a point against an oriented box basically reduces to transforming the point into the local space of the box and performing a 'point in axis aligned box' containment test. The latter part is easy, and only involves a few comparisons. The first part can be done by transforming the point by the inverse of the box transform in matrix form, or manually using vector math.

Post back if you have further questions about either of these steps.
RegularKid
RegularKid
Hmmm, ok. So basically transform the point into the boxes' local space and then do the normal axis aligned checks ( which are just a few ifs, right? ). My problem is getting the point into the boxes local space. Happen to have any C++ for that you can point me to? :)
Zakwayda
Zakwayda
Quote:
Hmmm, ok. So basically transform the point into the boxes' local space and then do the normal axis aligned checks ( which are just a few ifs, right? ). My problem is getting the point into the boxes local space. Happen to have any C++ for that you can point me to? :)
How is the rotation represented?
RegularKid
RegularKid
I've got the rotation for my box in degrees ( I can convert it to radians if it makes the math easier ). So, technically with the center point and the rotation angle I can calculate all four corners of the box as well. Maybe that makes the calculation easier?
Zakwayda
Zakwayda
Quote:
Original post by RegularKid
I've got the rotation for my box in degrees ( I can convert it to radians if it makes the math easier ). So, technically with the center point and the rotation angle I can calculate all four corners of the box as well. Maybe that makes the calculation easier?
You don't need the corners. Here's some pseudocode for computing the point coordinates in the box's local space:
vector2 x_axis(cos(box.angle), sin(box.angle));vector2 y_axis(-x_axis.y, x_axis.x);vector2 diff = point - box.center;float x = dot(diff, x_axis);float y = dot(diff, y_axis);
If you're not sure what this pseudocode does, I'd recommend reading up on some basic linear algebra and vector math (I found the book '3D Math Primer' to be a good introduction to this material).
RegularKid
RegularKid
Perfect!

That's just what I needed. Implemented that algorithm and viola...everything works great! Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.