Sign in to follow this  

Rectangle Collission Problem

This topic is 2636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a simple 2D C# and Xna Game similar to the Ice Caves in Pokemon G/S. I've got the Tile classes made and a rudimentary character created but I've hit a snag on the collision detection.

Right now I'm using a custom Rectangle class and a function that tests if 2 rectangles are colliding. Then I use the player's(made out of a separate class) velocity to figure out which side of the tile the player hit. This lets me set the players location to that side, making it look like the tile stopped the player. This works fine for the Left, Right, and top of the tile but when my character hits the bottom side he goes halfway through the tile before he is set to the correct position.

For an Idea of what the game looks like you can go here. [link]http://steveworks.deviantart.com/gallery/#/d2yxt30[/link]

Here's the source.

The Rectangle Class

public class Float_Rectangle
{
public Vector2 TopLeft; // The different vertices of the rectangle instance
public Vector2 TopRight;
public Vector2 Center;
public Vector2 BottomLeft;
public Vector2 BottomRight;

public Float_Rectangle(Vector2 location,Vector2 size)// Creates an instance of float_rectangle
{
TopLeft = location;
TopRight = new Vector2(location.X + (size.X -1), location.Y);
Center = new Vector2((location.X + (size.X-1)) / 2, (location.Y + (size.Y-1)) / 2);
BottomLeft = new Vector2(location.X, location.Y + (size.Y-1));
BottomRight = new Vector2(location.X + (size.X-1), location.Y + (size.Y-1));
}

public void Update_position(Vector2 location, Vector2 size) // Updates the Rectangles postion.
{
TopRight = new Vector2(location.X + (size.X-1), location.Y);
BottomLeft = new Vector2(location.X, location.Y + (size.Y-1));
Center = new Vector2((location.X + (size.X-1)) / 2, (location.Y + (size.Y-1)) / 2);
BottomLeft = new Vector2(location.X, location.Y + (size.Y-1));
BottomRight = new Vector2(location.X + (size.X-1), location.Y + (size.Y-1));
}
}






The collision detection function.

public bool Rect_collide(Float_Rectangle Rect1, Float_Rectangle Rect2)
{
if (Rect1.TopLeft.X >= Rect2.TopLeft.X && Rect1.TopLeft.X <= Rect2.TopRight.X)
{
if (Rect1.TopLeft.Y >= Rect2.TopLeft.Y && Rect1.TopLeft.Y <= Rect2.BottomLeft.Y)
{
return true;
}
}
if (Rect1.TopRight.X > Rect2.TopLeft.X && Rect1.TopRight.X < Rect2.TopRight.X)
{
if (Rect1.TopRight.Y >= Rect2.TopLeft.Y && Rect1.TopRight.Y <= Rect2.BottomLeft.Y)
{
return true;
}
}
if (Rect1.BottomLeft.X >= Rect2.TopLeft.X && Rect1.BottomLeft.X <= Rect2.TopRight.X)
{
if (Rect1.BottomLeft.Y >= Rect2.TopLeft.Y && Rect1.BottomLeft.Y <= Rect2.BottomLeft.Y)
{
return true;
}
}
if (Rect1.BottomRight.X >= Rect2.TopLeft.X && Rect1.BottomRight.X <= Rect2.TopRight.X)
{
if (Rect1.BottomRight.Y >= Rect2.TopLeft.Y && Rect1.BottomRight.Y <= Rect2.BottomLeft.Y)
{
return true;
}
}
return false;
}






and the part the simulates a wall.

player.position.X += player.velocity.X;
player.position.Y += player.velocity.Y;
player.rect.Update_position(player.position, new Vector2(32, 32));
foreach (BasicTile Tile in Tiles)
Tile.update_position();

foreach(BasicTile Tile in Tiles)
{
if (Rect_collide(player.rect,Tile.rect))
{
if (player.velocity.X > 0)
{
player.position.X = Tile.rect.TopLeft.X-32;
player.movement = 1;
player.velocity.X = 0;
}

if (player.velocity.X < 0)
{
player.position.X = Tile.rect.TopRight.X+1;
player.movement = 1;
player.velocity.X = 0;
}
if (player.velocity.Y > 0)
{
player.position.Y = Tile.rect.TopLeft.Y - 32;
player.movement = 1;
player.velocity.Y = 0;
}
if (player.velocity.Y < 0)
{
player.position.Y = Tile.rect.BottomLeft.Y+1;
player.movement = 1;
player.velocity.Y = 0;
}
}
}






[/source]

[Edited by - steveworks on September 26, 2010 9:17:08 PM]

Share this post


Link to post
Share on other sites
Have you tried using your debugger for this? It seems like it shouldn't be that difficult to breakpoint when the player is colliding/near-colliding with the bottom and you can see what is going on.

I guess you could just wait around for somebody to help you, but that's more code than I'm willing to sift through :)

If you're not experienced with debugging, this would probably be a good time to learn.

Share this post


Link to post
Share on other sites
You should drop whatever else you're doing and learn, you'll be hopeless without it.

Are you using Visual Studio? If you have questions I'm sure either somebody else or myself would be happy to help if you're confused.

Share this post


Link to post
Share on other sites
Sorry off the top of my head I don't know any good resources for beginners. Most of the stuff you can figure out just by playing around with it.

You'll want to familiarize yourself with these terms:
Debug Configuration: set this in visual studio to compile for debug mode

breakpoint : you can set these by clicking on the left side of a particular line in the gray area, the code will stop when it reaches this point

watch window: you can see values of variables currently in the watch window, should appear on the bottom when you start debugging

step over : executes the next instruction or function

step into : moves the debugger into the next function to debug that function


What I would do in your case is remove everything in your demo except for the player and one rectangle. Move the player over the rectangle where they are overlapping but your collision detection is not picking it up. Then set a breakpoint after the start of the Rect_collide function. Step through line by line and see if you can spot what isn't working correctly.

Happy hunting!

Share this post


Link to post
Share on other sites

This topic is 2636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this