Upcoming Events
Southwest Gaming Expo
11/20 - 11/22 @ Dallas, TX

Workshop on Network and Systems Support for Games (NetGames 2009)
11/23 - 11/25 @ Paris, France

ICIDS 2009 Interactive Storytelling
12/9 - 12/11 @ Guimarães, Portugal

Global Game Jam
1/29 - 1/31  

More events...


Quick Stats
7547 people currently visiting GDNet.
2341 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Link to us

  Intel sponsors gamedev.net search:   

2D Rotated Rectangle Collision


Optimizations

There are a few things that can be done to optimize this algorithm:
  • You can and should stop checking for collision the instant you find an axis where the rectangles don’t overlap. Remember, the separating axis theorem says that if two polygons are colliding all axes that are perpendicular to the edges of the polygons will show an overlap. Meaning that, if one axis shows no overlap, then collision is not possible and you should opt out to prevent unnecessary math.
  • It can really pay off to transform rectangle B into rectangle A’s local space. In order to do this, you should maintain these rectangles in local space and then transform rectangle B into world space and then by the inverse of rectangle A’s world space transform to put rectangle B into rectangle A’s local space. Then, translate both rectangles equally so that rectangle A is centered about the x and y axes. This means that two of the four axes that you need to project vectors onto are the unit (x and y) axes. Simply check for overlap between the x values of the corners of both rectangles and between the y values of the corners of both rectangles. With this solution you only have to actually project the vectors onto arbitrary axes twice, instead of four times.

  • Figure 8. Rectangles A and B in world space


    Figure 9. Rectangles A and B Transformed Into A's Local Space

  • It can be wise to utilize a radius that completely encompasses the rectangle. If the distance between the centers of rectangles A and B is greater than the radius of A and B added together then there cannot possibly be a collision and it is unnecessary to use the separating axis theorem.




Contents
  Introduction
  The Method Steps 1 & 2
  The Method Steps 3 & 4
  Optimizations

  Printable version
  Discuss this article