Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Would like a little explination as to how some collision detection code works.

Started by Dko Jun 2, 2012 at 9:02 AM 0 replies 1.2k views
Original Post
Dko
Dko
I've been following the book XNA 4.0 Game Development by Example and I'm currently on the project Robot Rampage.
In the following method I get what it's trying to do, but one thing isn't explained in the book.
How does the code detect a collision properly if you come at an wall at an angle to it's corner and your perfectly dead on so that the two corners touch? Seems like to me that it would check horizontally then vertically and see they are empty (if they are) for no collision and let the tank move into the wall.
Also attached a picture to show more clearly what I mean.

private static Vector2 checkTileObstacles(float elapsedTime, Vector2 moveAngle)
{
Vector2 newHorizontalLocation = BaseSprite.WorldLocation + (new Vector2(moveAngle.X, 0) * (playerSpeed * elapsedTime));
Vector2 newVerticalLocation = BaseSprite.WorldLocation + (new Vector2(0, moveAngle.Y) * (playerSpeed * elapsedTime));
Rectangle newHorizontalRect = new Rectangle(
(int)newHorizontalLocation.X, (int)BaseSprite.WorldLocation.Y,
BaseSprite.FrameWidth, BaseSprite.FrameHeight);
Rectangle newVerticalRect = new Rectangle(
(int)BaseSprite.WorldLocation.X, (int)newVerticalLocation.Y,
BaseSprite.FrameWidth, BaseSprite.FrameHeight);
int horizLeftPixel = 0;
int horizRightPixel = 0;
int vertTopPixel = 0;
int vertBottomPixel = 0;
if (moveAngle.X < 0)
{
horizLeftPixel = (int)newHorizontalRect.Left;
horizRightPixel = (int)BaseSprite.WorldRectangle.Left;
}
if (moveAngle.X > 0)
{
horizLeftPixel = (int)BaseSprite.WorldRectangle.Right;
horizRightPixel = (int)newHorizontalRect.Right;
}
if (moveAngle.Y < 0)
{
vertTopPixel = (int)newVerticalRect.Top;
vertBottomPixel = (int)BaseSprite.WorldRectangle.Top;
}
if (moveAngle.Y > 0)
{
vertTopPixel = (int)BaseSprite.WorldRectangle.Bottom;
vertBottomPixel = (int)newVerticalRect.Bottom;
}
if (moveAngle.X != 0)
for (int x = horizLeftPixel; x < horizRightPixel; x++)
{
for (int y = 0; y < BaseSprite.FrameHeight; y++)
if (TileMap.IsWallTileByPixel(new Vector2(x, newHorizontalLocation.Y + y)))
{
moveAngle.X = 0;
break;
}

if (moveAngle.X == 0)
break;
}
if (moveAngle.Y != 0)
for (int y = vertTopPixel; y < vertBottomPixel; y++)
{
for (int x = 0; x < BaseSprite.FrameWidth; x++)
if (TileMap.IsWallTileByPixel(new Vector2(newVerticalLocation.X + x, y)))
{
moveAngle.Y = 0;
break;
}
if (moveAngle.Y == 0)
break;
}
return moveAngle;
}
Robbie Woods
Robbie Woods
Just check that the left edge of the object is more right than the left edge of the wall, the right edge of the object is more left than the right edge of the wall, the upper edge of the Object is lower than the upper edge of the wall and the lower edge of the object is higher than the lower edge of the wall.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.