Advertisement
7heSama

Directional Collision Detection

Aug 10th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1.  
  2.                 //NOTE: THE BELOW WILL ONLY WORK in POSITIVE Y quadrants
  3.                 foreach (Entities.Mobile mobile in Game1.registry.Mobiles)
  4.                 {
  5.                     if (mobile.bounds.Contains((int)Position.X, (int)Position.Y))
  6.                     {
  7.                         if (LastPos.X > mobile.bounds.Right)
  8.                         {
  9.                             if (mobile.bounds.Top < LastPos.Y && LastPos.Y < mobile.bounds.Bottom)
  10.                             {
  11.                                 BounceVert(mobile.bounds.Right);
  12.                             }
  13.                             else
  14.                             {
  15.                                 Vector2 crosspoint = new Vector2();
  16.                                 crosspoint.X = mobile.bounds.Right;
  17.                                 crosspoint.Y = (Position.X / crosspoint.X) * Position.Y;
  18.  
  19.                                 if (crosspoint.Y > mobile.bounds.Top)
  20.                                 {
  21.                                     if (crosspoint.Y < mobile.bounds.Bottom)
  22.                                         BounceVert(mobile.bounds.Right);
  23.                                     else
  24.                                         BounceHorz(mobile.bounds.Bottom);
  25.                                 }
  26.                                 else
  27.                                     BounceHorz(mobile.bounds.Top);
  28.                             }
  29.                         }
  30.                         else if (LastPos.X < mobile.Position.X)
  31.                         {
  32.                             if (mobile.bounds.Top < LastPos.Y && LastPos.Y < mobile.bounds.Bottom)
  33.                             {
  34.                                 BounceVert(mobile.bounds.Left);
  35.                             }
  36.                             else
  37.                             {
  38.                                 Vector2 crosspoint = new Vector2();
  39.                                 crosspoint.X = mobile.bounds.Left;
  40.                                 crosspoint.Y = (crosspoint.X / Position.X) * Position.Y;
  41.  
  42.                                 if (crosspoint.Y > mobile.bounds.Top)
  43.                                 {
  44.                                     if (crosspoint.Y < mobile.bounds.Bottom)
  45.                                         BounceVert(mobile.bounds.Left);
  46.                                     else
  47.                                         BounceHorz(mobile.bounds.Bottom);
  48.                                 }
  49.                                 else
  50.                                     BounceHorz(mobile.bounds.Top);
  51.                             }
  52.                         }
  53.                         else
  54.                         {
  55.                             if (LastPos.Y < mobile.bounds.Top)
  56.                             {
  57.                                 BounceHorz(mobile.bounds.Top);
  58.                             }
  59.                             else
  60.                             {
  61.                                 BounceHorz(mobile.bounds.Bottom);
  62.                             }
  63.                         }
  64.                     }
  65.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement