views:

43

answers:

2

If I have a given rectangle, with the width w, height h and angle r

How large does another rectangle which contains all points of the rotated rectangle need to be?

I would need this to perform fast bounding box checks for a 2D physics engine I am making

+1  A: 

this may be what you need:

http://stackoverflow.com/questions/622140/calculate-bounding-box-coordinates-from-a-rotated-rectangle-picture-inside, answered by someone named markus.

Orbit
I just added the following comment to Markus' answer: Actually, due to symmetry, you need to transform only 2 corners, and if you give a little additional thought, it is just 1 corner to rotate.
ysap
A: 

You usually should consider rotating rectangles in a collision detection engine, since it will be quite straightforward to implement (I mean considering the rotated rectangle as it is).

In any case if you really want to simplify to have a coarse-level of collision detection the best thing is to embed the rectangle inside a circle, because it's really simple (centered over rectangle center and with a radius of the semi-diagonal of the rectangle) and compared to using a box it can be quite accurate for a coarse detection. Actually you can have an angle threshold to decide if it's better to use a circle or to consider the original rectangle (most degenerating cases are when angle is near to k*PI with k = 0,1,2,3

If you really really want to consider the rotated rectangle you can calculate it easily by choosing the topmost vertex of your rectangle (xT, yT) and the leftmost (xL, yL) (after the rotation of course) to obtain the topleft point that will be (xL, yT). Then you do the same thing for the bottomright corner taking (xR, yB) from the rightmost and lowest point of your rectangle and you have it. It will be the rectangle included in (xL, yY) (xR, yB).

Jack
Thanks for the helpful advice!
Markus