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

LOOP 2009
11/26 - 11/29  

EVA 2009
12/4 - 12/5 @ Buenos Aires, Argentina

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

More events...


Quick Stats
6136 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


Introduction

While working on a project for school, I found it necessary to perform a collision check between sprites that had been translated and rotated. I wanted to use bounding boxes because a per-pixel check was time consuming and unnecessary. After a couple of days of research I managed to work out an efficient solution using the separating axis theorem. After explaining my method to classmates and a few lab technicians, I realized that the game development community could benefit from a clear and thorough explanation of the process. Knowledge of linear algebra, specifically vector math, is useful but not necessary for understanding this article.

Separating Axis Theorem

The separating axis theorem states that for a pair of convex polygons that are not in a state of collision there exists an axis perpendicular to an edge of one of the polygons that has no overlap between the projected vertices of the two polygons. Essentially, what this means is that if we project the vertices of the two polygons that we are testing onto each axis that is perpendicular to the edges of each polygon and we detect an overlap on each polygon there is a collision, if even one axis shows no overlap then a collision is impossible. This solution works for any collision possibility, even the dreaded cross collision.


Figure 1. Cross Collision

Setting Up

Before we dive into the collision algorithm itself, there are a few prerequisites for this particular method. Firstly, although the separating axis theorem can be used to check for collisions between any convex polygons, rectangles are the normal collision method in 2D, so I will assume that you are using rectangles. Additionally, I will assume that you can convert your rectangles into a structure with four vectors, each representing a corner, and labeled or organized in such a way that you can tell which corner is which (specifically, we need to be able to identify which corners are adjacent – if the upper-left corner has been rotated until it is on the bottom of the rectangle that’s fine, just so long as it remains connected by an edge to the corners labeled upper-right and lower-left.).



The Method Steps 1 & 2


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

  Printable version
  Discuss this article