Sign in to follow this  
Chrfried

Rotated box collision with a point

Recommended Posts

I've got a 3D box in game that can be rotated about any axis and I was wondering about the best method for collision detection with that and a point in the world. I've read up about the separating-axis test and I could do that, but is there another way? I'm only doing collisions with a single point so it seems a full implementation of the SAT would be overkill. Any suggestions would be greatly appreciated.

Share this post


Link to post
Share on other sites
I had to do this the other day, but only in 2D. Maybe it could be extended to 3D? I gotta go so I have to write this up quick.

I rotated the point around to where it would be on the unrotated collision box, and then tested against the unrotated collision box. Here's how I did it

Givens:

ul = upper left corner of collision box
lr = lower right corner of collision box
theta = current angle sprite is facing
position = position of sprite (vector)
point = point to test (vector)

Then calculate:

P(vector) = position - point
alpha = atan(P.y/P.x)
beta = alpha - theta
r = sqrt( P.x*P.x + P.y*P.y)

Then the rotated point:

R = <r * cos(beta), r * sin(beta>

Finally test:

if (R.x > ul.x && R.x < lr.x &&
R.y < ul.y && R.y > lr.y ) {
POINT IS TOUCHING
} else {
POINT IS NOT TOUCHING
}

It helps to draw that on a whiteboard or something. Hope that gives you some ideas

Share this post


Link to post
Share on other sites
Is the 3D box moving? Is the Point moving?

If the box is just rotating and the point is stationary then make the box stationary and the point rotating. You'll notice it forms a circle. A circle to plane intersection test can be done. You'll get 0,1,2 or infinite intersection points. I'm not sure what the algorithm is though for the test, but it does exist.

Okay once you have the intersection points you can find the angles between them and which one is the first intersection point. This can then give you the exact time T between 0 and 1 when the intersection happened.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this