# [XNA] Rectangle Collision and Rotating

## Recommended Posts

Andy474    694
Ok, my game requires the player sprite to be rotated to face the mouse.

Now im not sure how to go about the collision detection, so here is a picture of the car

Dodgem Car

I need to be able to tell if the sprite has been hit on the green or blue lines.

my Plan was to use rectangles and simply rotate the rectangles with the sprite, however I don't know if this is possible. Any advice?

##### Share on other sites
jyk    2094
Quote:
 Original post by Andy474I need to be able to tell if the sprite has been hit on the green or blue lines.?
This can certainly be accomplished. The details will depend though on various factors (such as what shapes you'll be checking for collisions against, etc.).

##### Share on other sites
Andy474    694
Its Car to Car. So exactly the same .png file just tinted a different color :)

After a Little bit of googling I found this:

public Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform)        {            // Get all four corners in local space             Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);            Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);            Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);            Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);            // Transform all four corners into work space             Vector2.Transform(ref leftTop, ref transform, out leftTop);            Vector2.Transform(ref rightTop, ref transform, out rightTop);            Vector2.Transform(ref leftBottom, ref transform, out leftBottom);            Vector2.Transform(ref rightBottom, ref transform, out rightBottom);            // Find the minimum and maximum extents of the rectangle in world space             Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),            Vector2.Min(leftBottom, rightBottom));            Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),            Vector2.Max(leftBottom, rightBottom));            // Return that as a rectangle             return new Rectangle((int)min.X, (int)min.Y,            (int)(max.X - min.X), (int)(max.Y - min.Y));        }

EDIT: I can infact vouch now, this doesn't rly work :P Ideas?

[Edited by - Andy474 on October 17, 2010 5:11:38 AM]

##### Share on other sites
jyk    2094
It looks like that code builds a new AABB from a rotated AABB, which isn't what you want here, I don't think.

Try searching for 'sat', 'separating axis test', or 'separating axis theorem', as that would probably be the most reasonable way to do what you're wanting (that is, test for collision between two oriented boxes in 2-d, and also determine where the intersection, if any, occurred).

Another option would be to use an existing physics engine.