Advertisement
Guest User

My first working physics engine

a guest
Aug 17th, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1. /*
  2. This is functional for the most part, but there are serious flaws. I can't seem to do collisions against the top of the player and bottom of a block and the player is prone to
  3. falling through the floor if he jumps into corners in a specific way, such as this:
  4. http://i.imgur.com/DhkPL.png
  5. */
  6.  
  7. //=========== Level.cs ============
  8.  
  9. //Go through each block
  10. public static List<Block> CollisionsBottom()
  11.         {
  12.             List<Block> ret = new List<Block>();
  13.             foreach (Block b in blocks)
  14.             {
  15.                 if (b.IsCollision)
  16.                 {
  17. //If b is a collision block, then make a rectangle from player to bottom of screen
  18.                     if (b.Rect.Intersects(new Rectangle(Player.Rect.X, Player.Rect.Y, Player.Rect.Width, Util._STAGEHEIGHT)))
  19.                     {
  20.                         ret.Add(b);
  21.                     }
  22.                 }
  23.             }
  24.             return ret;
  25.         }
  26.         public static List<Block> CollisionsRight()
  27.         {
  28.             List<Block> ret = new List<Block>();
  29.             foreach (Block b in blocks)
  30.             {
  31.                 if (b.IsCollision)
  32.                 {
  33.                     if (b.Rect.Intersects(new Rectangle(Player.Rect.X, Player.Rect.Y, Util._STAGEWIDTH - Player.Rect.X, Player.Rect.Height)))
  34.                     {
  35.                         ret.Add(b);
  36.                     }
  37.                 }
  38.  
  39.             }
  40.             return ret;
  41.         }
  42.         public static List<Block> CollisionsLeft()
  43.         {
  44.             List<Block> ret = new List<Block>();
  45.             foreach (Block b in blocks)
  46.             {
  47.                 if (b.IsCollision)
  48.                 {
  49.                     if (b.Rect.Intersects(new Rectangle(0, Player.Rect.Y, Player.Rect.X, Player.Rect.Height)))
  50.                     {
  51.                         ret.Add(b);
  52.                     }
  53.                 }
  54.             }
  55.             return ret;
  56.         }
  57.  
  58. //============== Player.cs ===============
  59.  
  60. private static void Physics()
  61.         {
  62.             if (Velocity.X > 0)
  63.             {
  64.                 if (Math.Abs(DistToRight() - Rect.Width) > Velocity.X)
  65.                 {
  66.                     if (Player.Rect.X < Util._STAGEWIDTH / 2 + 30)
  67.                     {
  68.                         Player.Rect.X += (int)Velocity.X;
  69.                     }
  70.                     else
  71.                     {
  72.                         MoveAll(Velocity);
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     Velocity.X = 0;
  78.                 }
  79.             }
  80.             if (Velocity.X < 0)
  81.             {
  82.                 if (Math.Abs(DistToLeft()) > Math.Abs(Velocity.X))
  83.                 {
  84.                     if (Player.Rect.X > Util._STAGEWIDTH / 2 - 30)
  85.                     {
  86.                         Player.Rect.X += (int)Velocity.X;
  87.                     }
  88.                     else
  89.                     {
  90.                         MoveAll(Velocity);
  91.                     }
  92.                 }
  93.             }
  94. //Only allow the player to move as long as distance to B is greater than his speed.
  95. //Otherwise, player doesn't move.
  96.             if (Math.Abs(DistToFloor()) > Player.Velocity.Y)
  97.             {
  98.                 Velocity.Y += 1;
  99.                 Rect.Y += (int)Velocity.Y;
  100.                 onGround = false;
  101.             }
  102.             else
  103.             {
  104.                 onGround = true;
  105.                 Velocity.Y = 0;
  106.             }
  107.         }
  108. //Take the nearest block to the player that has collided with the rectangle and
  109. //calculate the distance to it
  110.         private static float DistToFloor()
  111.         {
  112.             Block b = Level.CollisionsBottom()[0];
  113.                 return Rect.Y + Rect.Height - b.Rect.Y;
  114.         }
  115.         private static float DistToRight()
  116.         {
  117.  
  118.             if (Level.CollisionsRight().Count > 0)
  119.             {
  120.                 Block b = Level.CollisionsRight()[0];
  121.                 return b.Rect.X - Rect.X;
  122.             }
  123.             return Velocity.X + 1;
  124.  
  125.         }
  126.         private static float DistToLeft()
  127.         {
  128.             if (Level.CollisionsLeft().Count > 0)
  129.             {
  130.                 Block b = Level.CollisionsLeft().Last();
  131.                 return Rect.X - b.Rect.X - b.Rect.Width;
  132.             }
  133.             return Math.Abs(Velocity.X) + 1;
  134.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement