views:

368

answers:

3

I'm working with xna in C# and in my game I will have a variety of space ships flying all over the place. They will each have an arbitrary rotation, size and position in space and I need a method to determine when they collide. Ideally the method would take two Rectangles, two doubles and two Vector2s for size, rotation and position respectively and return a boolean that indicates whether they have intersected or not.

+1  A: 

These rectangles you describe are called OBB (Oriented Bounding Boxes)

The way to do collisions between them is using the 'Separating axis theorem'

A really nice page describing it in detail with lots of pictures can be found here

Toad
They're not axis-aligned if they're rotated around.
Mike Daniels
You are very right of course. I meant OBB. The theory still stands for those as well ;^)
Toad
+2  A: 

You could also consider just using an out of the box solution for this and integrating something like the Farseer Physics Engine:
http://farseerphysics.codeplex.com/

Joel Martinez
+2  A: 

Have a look at these links:

Collision Detection Overview
Collision Detection Matrices
Putting Collision Detection Into Practice

They show you a way to do pixel-based collision detection, which is more accurate than rectangle-based for irregularly shaped objects.

Kyralessa
@kyralessa: you almost never want to do pixel perfect collisions in games. For instance, he is making a game with rocketships. With pixel perfect collision it would mean if 1 pixel of the wing of a rocket would touch 1 pixel of the tip of his spaceship it would explode. Usually you define a collision box to be a much smaller region inside the objects. This looks and feels much better.
Toad