Advertisement
Guest User

Untitled

a guest
Jan 17th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1.         void CalculateMovement(float frameTime)
  2.         {
  3.             Vector2 previousPos = Position;
  4.  
  5.             Velocity.X = MathHelper.Clamp(Velocity.X + horizontalAcceleration * frameTime * movement, -maxHorizontalSpeed, maxHorizontalSpeed);
  6.             if (onGround)
  7.                 Velocity.X *= horizontalDragGround;
  8.             else
  9.                 Velocity.X *= horizontalDragAir;
  10.             Velocity.Y = MathHelper.Clamp(Velocity.Y + verticalAcceleration * frameTime, -maxVerticalSpeed, maxVerticalSpeed);
  11.             if (onGround)
  12.                 Velocity.Y = DoJump();
  13.             DoCollisions();
  14.  
  15.             if (previousPos.X == Position.X)
  16.                 Velocity.X = 0;
  17.             if (previousPos.Y == Position.Y)
  18.                 Velocity.Y = 0;
  19.         }
  20.  
  21.         float DoJump()
  22.         {
  23.             if (World.renderWindow.Input.IsKeyDown(KeyCode.Space))
  24.                 return -10;
  25.             else
  26.                 return Velocity.Y;
  27.         }
  28.  
  29.         void DoCollisions()
  30.         {
  31.             onGround = false;
  32.             Position.Y += Velocity.Y;
  33.             Vector2 tileCollision = GetTileCollision();
  34.             if (tileCollision.X != -1 || tileCollision.Y != -1)
  35.             {  
  36.                 Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
  37.                     new Rectangle(
  38.                         tileCollision.X * World.tileEngine.TileWidth,
  39.                         tileCollision.Y * World.tileEngine.TileHeight,
  40.                         World.tileEngine.TileWidth,
  41.                         World.tileEngine.TileHeight
  42.                     )
  43.                 );
  44.                 Position.Y += collisionDepth.Y;
  45.                 if (collisionDepth.Y < 0)
  46.                     onGround = true;
  47.             }
  48.  
  49.             Position.X += Velocity.X;
  50.             tileCollision = GetTileCollision();
  51.             if (tileCollision.X != -1 || tileCollision.Y != -1)
  52.             {
  53.                 Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
  54.                     new Rectangle(
  55.                         tileCollision.X * World.tileEngine.TileWidth,
  56.                         tileCollision.Y * World.tileEngine.TileHeight,
  57.                         World.tileEngine.TileWidth,
  58.                         World.tileEngine.TileHeight
  59.                     )
  60.                 );
  61.                 Position.X += collisionDepth.X;
  62.             }
  63.         }
  64.  
  65.         Vector2 GetTileCollision()
  66.         {
  67.             int topLeftTileX = (int)(CollisionRectangle.TopLeft.X / World.tileEngine.TileWidth);
  68.             int topLeftTileY = (int)(CollisionRectangle.TopLeft.Y / World.tileEngine.TileHeight);
  69.             int BottomRightTileX = (int)(CollisionRectangle.DownRight.X / World.tileEngine.TileWidth);
  70.             int BottomRightTileY = (int)(CollisionRectangle.DownRight.Y / World.tileEngine.TileHeight);
  71.  
  72.             if (CollisionRectangle.DownRight.Y % World.tileEngine.TileHeight == 0)
  73.                 BottomRightTileY -= 1;
  74.             if (CollisionRectangle.DownRight.X % World.tileEngine.TileWidth == 0)
  75.                 BottomRightTileX -= 1;
  76.  
  77.             for (int i = topLeftTileX; i <= BottomRightTileX; i++)
  78.             {
  79.                 for (int j = topLeftTileY; j <= BottomRightTileY; j++)
  80.                 {
  81.                     if (World.tileEngine.TileIsSolid(i, j))
  82.                     {
  83.                         return new Vector2(i,j);
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             return new Vector2(-1,-1);
  89.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement