1

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

Markus
  • 13
  • 1
  • 3
  • I know that one way would be to rotate all corner points of the rectangle and use them, but I thought that there may be a better one. – Markus Oct 24 '10 at 13:22
  • And @mojuba: If you (or any of the two people who upvoted your comment) know the answer it would be really nice if you could post it as well... – Markus Oct 24 '10 at 13:26
  • possible duplicate of [Calculate Bounding box coordinates from a rotated rectangle, Picture inside.](http://stackoverflow.com/questions/622140/calculate-bounding-box-coordinates-from-a-rotated-rectangle-picture-inside) – Paul R Oct 24 '10 at 22:05

2 Answers2

1

this may be what you need:

Calculate Bounding box coordinates from a rotated rectangle, answered by someone named markus.

Community
  • 1
  • 1
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
  • 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 Oct 25 '10 at 12:18
0

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
  • 131,802
  • 30
  • 241
  • 343