Jump to content

Anyone know how to get the bounding box of a rotated rectangle?


rich
 Share

Recommended Posts

This is best explained with examples. So.. I can easily calculate the bounding box of a rotated rectangle, as you can see in this demo:

 

http://gametest.mobi/rotate/index.php?f=works.js&d=tests

 

I'm using this logic:

 

SpriteUtils._sin = Phaser.GameMath.sinA[sprite.rotation];SpriteUtils._cos = Phaser.GameMath.cosA[sprite.rotation];if (SpriteUtils._sin < 0){    SpriteUtils._sin = -SpriteUtils._sin;}if (SpriteUtils._cos < 0){    SpriteUtils._cos = -SpriteUtils._cos;}sprite.cameraView.width = Math.round(sprite.height * SpriteUtils._sin + sprite.width * SpriteUtils._cos);sprite.cameraView.height = Math.round(sprite.height * SpriteUtils._cos + sprite.width * SpriteUtils._sin);sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.cameraView.width * sprite.transform.origin.x));sprite.cameraView.y = Math.round(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.cameraView.height * sprite.transform.origin.y));

 

That has a bit of camera specific code in it that isn't relevant to the math involved here, but essentially it's a simple bit of math. cameraView is a Rectangle with x/y/width/height and is what you see rendered in the demo above.

 

However, I want to find it regardless of the point of rotation, for example this one:

 

http://gametest.mobi/rotate/index.php?f=fails.js&d=tests

 

As you can see the bounding box is the right size but in totally the wrong place. Does anyone know how to work out the offset from the sprite x/y coordinate (the top left of the image) to accurately position the bounding box? Assume that the point of rotation can be anywhere within the image (top left, bottom right, literally anywhere).

Link to comment
Share on other sites

You can not compute the position of bounding box directly, as you are computing width and height.

 

You have to transform all 4 vertices of the rectangle, then compute minimal / maximal X and Y coordinates for all 4 points. 

Link to comment
Share on other sites

Damn, I thought that might be the case. Still at least if they are rotating from the center I can use the above code, and if from some other angle I'll just have to use the 4 points (well it's 2 points really isn't it as you can discount 2 of them). I was hoping there would be a way to translate before the rotation, but I guess not.

Link to comment
Share on other sites

If you want test if a point is inside a rotated box what you can do is transform the point to the box transformation space using the box inverted rotation matrix. With the point transformed this way you can test it like if the box wasn't rotated. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...