Jump to content
  • Advertisement
Sign in to follow this  
Stormtrooper

Collision Box at an Angle?

This topic is 3706 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!