Mars714

Collision Class

Mar 4th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.    
  5.     public class Collision
  6.     {
  7.         static public var collisionSide:String = "";
  8.        
  9.         public function Collision()
  10.         {
  11.         }
  12.         static public function block(character:MovieClip, wall:MovieClip):void
  13.         {
  14.             //Calculate the distance vector
  15.             var vx:Number
  16.                 = (character.x + (character.width / 2))
  17.                 - (wall.x + (wall.width / 2));
  18.            
  19.             var vy:Number
  20.                 = (character.y + (character.height / 2))
  21.                 - (wall.y + (wall.height / 2));
  22.            
  23.             //Check whether vx
  24.             //is less than the combined half widths
  25.             if(Math.abs(vx) < character.width / 2 + wall.width / 2)
  26.             {
  27.                 //A collision might be occurring! Check
  28.                 //whether vy is less than the combined half heights
  29.                 if(Math.abs(vy) < character.height / 2 + wall.height / 2)
  30.                 {
  31.                     //A collision has ocurred! This is good!
  32.                    
  33.                     //Find out the size of the overlap on both the X and Y axes
  34.                     var overlap_X:Number
  35.                     = character.width / 2
  36.                         + wall.width / 2
  37.                         - Math.abs(vx);
  38.                    
  39.                     var overlap_Y:Number
  40.                     = character.height / 2
  41.                         + wall.height / 2
  42.                         - Math.abs(vy);
  43.                    
  44.                     //The collision has occurred on the axis with the
  45.                     //*smallest* amount of overlap. Let's figure out which
  46.                     //axis that is
  47.                    
  48.                     if(overlap_X >=  overlap_Y)
  49.                     {
  50.                         //The collision is happening on the X axis
  51.                         //But on which side? _v0's vy can tell us
  52.                         if(vy > 0)
  53.                         {
  54.                             collisionSide = "Top";
  55.                            
  56.                             //Move the rectangle out of the collision
  57.                             character.y = character.y + overlap_Y;
  58.                         }
  59.                         else
  60.                         {
  61.                             collisionSide = "Bottom";
  62.                            
  63.                             //Move the rectangle out of the collision
  64.                             character.y = character.y - overlap_Y;
  65.                         }
  66.                     }
  67.                     else
  68.                     {
  69.                         //The collision is happening on the Y axis
  70.                         //But on which side? _v0's vx can tell us
  71.                         if(vx > 0)
  72.                         {
  73.                             collisionSide = "Left";
  74.                            
  75.                             //Move the rectangle out of the collision
  76.                             character.x = character.x + overlap_X;
  77.                         }
  78.                         else
  79.                         {
  80.                             collisionSide = "Right";
  81.                            
  82.                             //Move the rectangle out of the collision
  83.                             character.x = character.x - overlap_X;
  84.                         }
  85.                     }
  86.                 }
  87.                 else
  88.                 {
  89.                     //No collision
  90.                     collisionSide = "No collision";
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 //No collision
  96.                 collisionSide = "No collision";
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment