Sign in to follow this  

Collision Box at an Angle?

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game is setup to have a cannon(think pirates), with a collision box to check if an object collides with the end. How do I get the collision box to rotate with the end of the cannon when it rotates? The collision box is setup simply as x,y,width,height. Cannon is 22x51 So it rotates around 11x51

Share this post


Link to post
Share on other sites
You have to apply a transformation to the box. The box itself can still have just a positional coordinate plus width/height, but when rotated those values will be in a different coordinate system, one that is rotated from the world coordinates. This is called an oriented box, or oriented bounding box. Do a search on "oriented bounding box" and you'll probably turn up some code on how to do collision detection against these oriented boxes. (Remember, the object that hits the end might have a box that is oriented differently, so the collision test is more complex.)

Another approach, depending on your goals (and this might be your intent) would be to just have the unoriented box move to always sits at the position of the end of the cannon. This is a bit easier. You probably already know how to find the endpoint of the cannon as it rotates, and that would become your new xy for the collision box.

Hope that helps!

Share this post


Link to post
Share on other sites
Thanks for your reply. I'm going to go with your second suggestion. Here is my go at it, but the collision box flies waay off screen into the unknown. This gets called every frame, if the cannon is rotating.

51 is the radius, or length of the cannon. 22 is the width of the cannon, assuming everything is being calculated from the center of the cannon. The cannon is being rotated at 51(bottom)x11(middle)


// First move the collisionbox to the rotation point
this.collisionBox.translate(this.collisionBox.x + 11, this.collisionBox.y + 51);

// Calculate the angle
float x = 51.0f * (float)Math.cos(angle) - 11.0f;
float y = 51.0f * (float)Math.sin(angle) - 11.0f;

// Move the collision box
this.collisionBox.translate((int)x, (int)y);

Share this post


Link to post
Share on other sites

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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