Jump to content
  • Advertisement

Dko

Member
  • Content count

    358
  • Joined

  • Last visited

Community Reputation

146 Neutral

About Dko

  • Rank
    Member
  1. 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; }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!