1

I am currently creating an android game and implemented collision detection a while back. I am simply drawing a Rect around sprites using their position, width and height and seeing if they intersect other Rects. However, my sprites now rotate depending on their trajectory, but I cannot find how to rotate the Rect so the bound is correct. Any suggestions?

Thanks Andy

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
Andrew Seymour
  • 250
  • 1
  • 4
  • 18
  • Do the sprites rotate around their centre? If you draw them rotated, how is it that you can't calculate the rotated rectangle? – paddy Jan 22 '13 at 21:59
  • They do rotate around their centre. How do I go about creating the rotated rectangle? I am using a Rect object, but couldn't see how to rotate it – Andrew Seymour Jan 22 '13 at 22:02

1 Answers1

1

Rect objects are usually axis-aligned, and so they only need 4 values: top, left, bottom, right.

If you want to rotate your rectangle, you'll need to convert it to eight values representing the co-ordinate of each vertex.

You can easily calculate the centre value by averaging all the x- and y-values.

Then it's just basic maths. Here's something from StackOverflow:

Rotating a point about another point (2D)

Your eight values, or four corners are (assuming counter-clockwise from the top right):

v0 : (right, top)
v1 : (left, top)
v2 : (left, bottom)
v3 : (right, bottom)

Create your own rectangle object to cope with this, and compute intersections etc.

Note that I've talked about how to rotate the rectangle's vertices. If you still want a bounding box, this is normally still considered to be axis-aligned, so you could take the max and min of the rotated vertices and construct a new (larger) rectangle. That might not be what you want though.

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103
  • How do I convert it to 8 values? I'm a bit of a newbie, thanks for the help and patience – Andrew Seymour Jan 22 '13 at 22:14
  • Ahh, I see. Thank you. So, the bound will still be out. Is there any way to make the bounding box fit the sprite? With or without the method I am currently using? – Andrew Seymour Jan 22 '13 at 22:22
  • Um, either you misunderstood me or I misunderstood you. You want a rotated rectangle, right? I told you how you can do that by rotating the vertices around the centre. That should fit the sprite, assuming that by 'fit' you mean rectangle that has been rotated to mimic the rotated sprite. – paddy Jan 22 '13 at 22:28
  • 1
    I can't put the four vertices into a Rect and let intersect() do all the work for me. However, after reading this: http://www.gamedev.net/page/resources/_/technical/game-programming/2d-rotated-rectangle-collision-r2604 I do not need the Rect object. Apologies for the confusion, but thanks for the help – Andrew Seymour Jan 22 '13 at 22:36
  • Nice link. I was suggesting that you don't use the Rect but create your own rectangle class that is essentially a 4-sided polygon. That article is great though. Good find. – paddy Jan 22 '13 at 22:45