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]