# Collision Box at an Angle?

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

## 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 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 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 pointthis.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 boxthis.collisionBox.translate((int)x, (int)y);

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009488
• ### Who's Online (See full list)

There are no registered users currently online

×