# Rotational Collision Rectangle Problem

• (2 Pages)
• 1
• 2

## 15 Replies - 5491 Views - Last Post: 23 December 2010 - 09:04 PM

### #1 SMASH

Reputation: 3
• Posts: 34
• Joined: 01-May 09

# Rotational Collision Rectangle Problem

Posted 14 December 2010 - 12:54 PM

Hey guys I'm new here and I'm a Game and Simulation Programming student at DeVry and getting into the thick of my classes, so I'm starting to develop on the side for a bit of a portfolio and for the experience. Currently I'm working on a game(or atleast the barebones) that is a top down 2D game I call BattleBumperCars. The point of the game is to damage the enemy by hitting them on the sides, while anything hitting the front or back bumpers don't do damage. This, I've come to realize, will mean I need to make rotational collision rectangles. So I've been researching and I found a pretty solid theory tutorial, I think I must've gotten something lost in translation. Tutorial: http://www.gamedev.n...sion/page2.asp. Also I do understand there are some classes out there that do this already but I'd rather develop my own from the ground up so I can make sure that everything in this project is my own code as well I need the experience. We should never fully rely on other people's code. Anyways here's what I have and I am getting some interesting results lol:

```namespace BattleBumperCars
{
class RotatedRectangle
{

protected Vector2 corner1, corner2, corner3, corner4;
protected Vector2 axis1, axis2, pCorner1Axis1, pCorner1Axis2, pCorner3Axis1, pCorner3Axis2;
protected float min1, max1, min2, max2;

public RotatedRectangle()
{
corner1 = new Vector2(0 , 0);
corner2 = new Vector2(0 , 0);
corner3 = new Vector2(0 , 0);
corner4 = new Vector2(0 , 0);
axis1 = new Vector2(0, 0);
axis2 = new Vector2(0, 0);
}
public RotatedRectangle(Rectangle rect, double rot)
{
corner1 = new Vector2((rect.Center.X + rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y + rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y + rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X + rect.Width / 2) * (float)Math.Sin(rot));
corner2 = new Vector2((rect.Center.X - rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y + rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y - rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X + rect.Width / 2) * (float)Math.Sin(rot));
corner3 = new Vector2((rect.Center.X + rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y - rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y + rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X - rect.Width / 2) * (float)Math.Sin(rot));
corner4 = new Vector2((rect.Center.X - rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y - rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y - rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X - rect.Width / 2) * (float)Math.Sin(rot));

axis1 = new Vector2(corner1.X - corner3.X, corner1.Y - corner3.Y);
axis2 = new Vector2(corner3.X - corner4.X, corner3.Y - corner4.Y);

pCorner1Axis1 = new Vector2((corner1.X * axis1.X + corner1.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)) * axis1.X, (corner1.X * axis1.X + corner1.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)));
pCorner1Axis2 = new Vector2((corner1.X * axis2.X + corner1.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)) * axis2.X, (corner1.X * axis2.X + corner1.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)));

pCorner3Axis1 = new Vector2((corner3.X * axis1.X + corner3.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)) * axis1.X, (corner3.X * axis1.X + corner3.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)));
pCorner3Axis2 = new Vector2((corner3.X * axis2.X + corner3.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)) * axis2.X, (corner3.X * axis2.X + corner3.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)));

min1 = Vector2.Dot(pCorner1Axis1, axis1);
max1 = Vector2.Dot(pCorner3Axis1, axis1);

min2 = Vector2.Dot(pCorner1Axis2, axis2);
max2 = Vector2.Dot(pCorner3Axis2, axis2);

if (min1 > max1)
{
float temp = min1;
min1 = max1;
max1 = temp;
}

if (min2 > max2)
{
float temp = min2;
min2 = max2;
max2 = temp;
}

}
public bool Intersects(RotatedRectangle rr)
{
if (rr.max1 <= max1 || rr.max1 >= min1 && rr.max2 <= max2 || rr.max2 >= min2)
return true;
else
return false;
}

}
}

```

As an addition, it compiles fine but I get pretty random results in the game. For testing I have a playable car and a car that just sits there and will reset it's position and rotation if there is collision. I look forward to becoming an active member in this community and contributing to other peoples problems .

Is This A Good Question/Topic? 0

## Replies To: Rotational Collision Rectangle Problem

### #2 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 14 December 2010 - 01:06 PM

The Rectangle structure in the XNA framework already comes equipped with members to find the top, bottom, left, and right sides of the rectangle that will make your collision detection a lot simpler. Following is a link to the Rectangle Members page on the MSDN library:
http://msdn.microsof...le_members.aspx

The public properties are where the top, left, bottom, and right members are located. Take a look at them and I guarantee they will make your life a lot easier.

### #3 SMASH

Reputation: 3
• Posts: 34
• Joined: 01-May 09

## Re: Rotational Collision Rectangle Problem

Posted 14 December 2010 - 01:10 PM

Oh, one more thing, I'm still working with XNA 3.1. Not sure if that changes anything but probably should've mentioned it. Thanks for the link bud though are you saying the part that is making this not work is vertex (or corner as I call them in the program) locating?

Also, the rect.Bottom function would return the y value of the vertex at the lowest position? Does that still work when the rectangle is rotated?

This post has been edited by SMASH: 14 December 2010 - 01:12 PM

### #4 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 14 December 2010 - 01:50 PM

I'm not sure about how it would work after rotation, and I barely looked at your code before, so I don't know if it's the vertex locating that's causing issues or what. I just thought I'd bring those properties to your attention and see if they'd help you at all. As for actually finding what in your current code is causing issues, I'm afraid I don't really have the time to dissect everything just yet since I'm still at work, but maybe when I get home I'll have a little time to look through it.

### #5 SMASH

Reputation: 3
• Posts: 34
• Joined: 01-May 09

## Re: Rotational Collision Rectangle Problem

Posted 14 December 2010 - 01:57 PM

I would REALLY appreciate that and absolutely no problem. I'm off to work soon too so I feel ya. Just so you know though, I followed that tutorial in the link and it really shows how I came up with what I have. I could also send you the full project if you think that would help. I don't expect you to finish it (that's not what I'm angling for) but rather some guidance on where I've gone wrong and my personal favorite way of doing it is through trial and error.

This post has been edited by SMASH: 14 December 2010 - 02:01 PM

### #6 Fib

Reputation: 162
• Posts: 554
• Joined: 12-March 09

## Re: Rotational Collision Rectangle Problem

Posted 17 December 2010 - 12:41 PM

What you're trying to accomplish is an oriented bounding box.

I think your code can definitely be cleaned up by using a rotation matrix. Remember, XNA has a boat load of prebuilt functions that help with math problems like this. The Vector2 class has a Transform() method that can rotate your rectangle. the Transform() method takes in a vector position, and a rotation matrix. XNA has a wonderful Matrix structure that can easily create your rotation matrix.

To rotate your rectangle you can do this:
```corner1 = Vector2.Transform(corner1, Matrix.CreateRotationZ(rot));
corner2 = Vector2.Transform(corner2, Matrix.CreateRotationZ(rot));
corner3 = Vector2.Transform(corner3, Matrix.CreateRotationZ(rot));
corner4 = Vector2.Transform(corner4, Matrix.CreateRotationZ(rot));

```

If you need it to rotate around a certain object on the screen, then you have to do a little more vector math. Instead of the above code, you would do this.
```Vector2 temp;

temp = corner1 - objectToRotateAroundPosition;
temp = Vector2.Transform(temp, Matrix.CreateRotationZ(rot));
corner1 = temp + objectToRotateAroundPosition;

temp = corner2 - objectToRotateAroundPosition;
temp = Vector2.Transform(temp, Matrix.CreateRotationZ(rot));
corner2 = temp + objectToRotateAroundPosition;

temp = corner3 - objectToRotateAroundPosition;
temp = Vector2.Transform(temp, Matrix.CreateRotationZ(rot));
corner3 = temp + objectToRotateAroundPosition;

temp = corner4 - objectToRotateAroundPosition;
temp = Vector2.Transform(temp, Matrix.CreateRotationZ(rot));
corner4 = temp + objectToRotateAroundPosition;

```

This does the exact same thing you're doing, with much less code. This actually might fix your problem, since your code looks very messy and the odds that you made a typo somewhere is pretty high.

Now as for checking for an intersection is an entirely different problem. I can't help you with that, as I do not understand the math behind it... yet.

Let me know if this helps you.

This post has been edited by Fib: 17 December 2010 - 01:08 PM

### #7 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 17 December 2010 - 04:45 PM

Nicely put together, Fib. Well done.

### #8 Fib

Reputation: 162
• Posts: 554
• Joined: 12-March 09

## Re: Rotational Collision Rectangle Problem

Posted 18 December 2010 - 07:30 PM

Kilorn, on 17 December 2010 - 04:45 PM, said:

Nicely put together, Fib. Well done.

Thank you. I just recently took a game programming class at my university, and we used XNA. So this stuff is pretty fresh in my memory.

### #9 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 18 December 2010 - 08:29 PM

Show us some of the projects you worked on in the course. I'm curious what type of curriculum you had.

### #10 SMASH

Reputation: 3
• Posts: 34
• Joined: 01-May 09

## Re: Rotational Collision Rectangle Problem

Posted 19 December 2010 - 07:49 AM

That's funny Fib, I've gone through Math for Game Programming I and Math for Game Programming II and we only used C++ and C# and the only collision we ever covered was circular.

I mean, it's Math for GAME Programming... why not XNA? I have no idea...

### #11 Fib

Reputation: 162
• Posts: 554
• Joined: 12-March 09

## Re: Rotational Collision Rectangle Problem

Posted 20 December 2010 - 09:47 PM

At the beginning of the class, we had a few assignments that went over basic 2d sprites, animation, single bounding shape collision, multiple bounding shape collision, input, and sound.

Then we had to make a full 2d game in 2 weeks. I recreated break out.

We then got into 3d stuff. Our first 3d assignment, we had to create a 3d cube and allow it to rotate based on user input. Then we had a few assignments on first person and third person cameras, and drawing, scaling, rotating 3d models.

Then we had 1 assignment on using basic shaders. We created a textured cube, then had to apply certain shader effects on the texture, like blur, grayscale, horizontal flip, and a basic spotlight that followed the mouse.

Towards the end of the class there was an assignment on 3d terrain rendering and collision, using height maps, and uv texture mapping. We used the free version of EarthSculptor to make our terrain.

Finally, our last assignment was to make a 2 player LAN, first person shooter, death match kind of game. We had 1 month to work on it.

I can upload the code for some of my projects if you want, just let me know.

### #12 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 21 December 2010 - 06:35 AM

I wasn't necessarily wanting to see the code, I was just looking to see some screenshots, but if you want to upload the code, that's fine with me. I always enjoy looking through code from other programmers to get a feel for their style as opposed to my own.

### #13 Fib

Reputation: 162
• Posts: 554
• Joined: 12-March 09

## Re: Rotational Collision Rectangle Problem

Posted 21 December 2010 - 09:07 AM

Oh, I can definitely post some screen shots. I'll do that when I get home today.

### #14 Kilorn

• XNArchitect

Reputation: 1357
• Posts: 3,528
• Joined: 03-May 10

## Re: Rotational Collision Rectangle Problem

Posted 21 December 2010 - 09:09 AM

### #15 Fib

Reputation: 162
• Posts: 554
• Joined: 12-March 09

## Re: Rotational Collision Rectangle Problem

Posted 21 December 2010 - 11:24 AM

Here's a website that may give you more insight SMASH.

http://www.flipcode....ersection.shtml

The code is a bit different from yours, so it may give you more insight on how to test for intersections between 2 oriented bounding boxes.