home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Classicos / smashout.swf / scripts / __Packages / Engine / Collision.as next >
Encoding:
Text File  |  2005-11-09  |  5.2 KB  |  133 lines

  1. class Engine.Collision
  2. {
  3.    var point;
  4.    var normal;
  5.    var edge;
  6.    var time;
  7.    var offCorner;
  8.    function Collision(point, normal, time, edge, offCorner)
  9.    {
  10.       this.point = point;
  11.       this.normal = normal;
  12.       this.edge = edge;
  13.       this.time = time;
  14.       this.offCorner = offCorner;
  15.    }
  16.    function toString()
  17.    {
  18.       return "Collision: point: " + this.point + " normal: " + this.normal;
  19.    }
  20.    static function sweepSphereToStaticShape(sphere, shape, noData)
  21.    {
  22.       if(sphere.motionLeft > shape.right || sphere.motionRight < shape.left || sphere.motionTop > shape.bottom || sphere.motionBottom < shape.top)
  23.       {
  24.          return undefined;
  25.       }
  26.       var _loc25_ = shape.edges;
  27.       var _loc22_ = _loc25_.length;
  28.       var _loc15_ = sphere.motionVec;
  29.       var _loc14_ = sphere.motionDir;
  30.       var _loc4_ = sphere.motionDist;
  31.       var _loc5_ = sphere.radius;
  32.       var _loc21_ = undefined;
  33.       var _loc12_ = undefined;
  34.       var _loc20_ = undefined;
  35.       var _loc7_ = 1.7976931348623157e+308;
  36.       var _loc13_ = new Vector(sphere.x - shape.x,sphere.y - shape.y);
  37.       var _loc1_ = undefined;
  38.       var _loc9_ = undefined;
  39.       var _loc23_ = undefined;
  40.       while((_loc22_ = _loc22_ - 1) > -1)
  41.       {
  42.          if(_loc1_ = _loc25_[_loc22_].props & 1)
  43.          {
  44.             if(_loc14_.dot(_loc1_.normal) < 0)
  45.             {
  46.                var _loc16_ = _loc1_.a;
  47.                _loc9_ = new Vector(_loc1_.center.x - _loc13_.x,_loc1_.center.y - _loc13_.y,0);
  48.                if(_loc9_.length - _loc5_ - _loc4_ <= _loc1_.radius)
  49.                {
  50.                   var _loc3_ = new Vector(_loc13_.x - _loc16_.x,_loc13_.y - _loc16_.y);
  51.                   var _loc18_ = _loc3_.dot(_loc1_.normal);
  52.                   if(!(_loc18_ < 0 || _loc18_ - _loc4_ > _loc5_))
  53.                   {
  54.                      var _loc17_ = new Vector(_loc3_.x - _loc1_.normal.x * _loc5_,_loc3_.y - _loc1_.normal.y * _loc5_);
  55.                      var _loc10_ = _loc1_.span;
  56.                      var _loc6_ = (_loc15_.x * (- _loc17_.y) - _loc15_.y * (- _loc17_.x)) / (_loc15_.y * _loc10_.x - _loc15_.x * _loc10_.y);
  57.                      var _loc2_ = undefined;
  58.                      if(_loc6_ >= 0 && _loc6_ <= 1)
  59.                      {
  60.                         var _loc11_ = new Vector(_loc10_.x * _loc6_ + _loc1_.normal.x * _loc5_,_loc10_.y * _loc6_ + _loc1_.normal.y * _loc5_);
  61.                         var _loc19_ = new Vector(_loc11_.x - _loc3_.x,_loc11_.y - _loc3_.y);
  62.                         if((_loc2_ = _loc19_.length) <= _loc4_)
  63.                         {
  64.                            if(_loc7_ < _loc2_ / _loc4_)
  65.                            {
  66.                               continue;
  67.                            }
  68.                            _loc12_ = _loc11_;
  69.                            _loc7_ = _loc2_ / _loc4_;
  70.                            if(_loc7_ < 0.001)
  71.                            {
  72.                               _loc7_ = 0;
  73.                               _loc11_.x = _loc3_.x;
  74.                               _loc11_.y = _loc3_.y;
  75.                            }
  76.                            _loc21_ = _loc1_.normal;
  77.                            _loc20_ = _loc1_;
  78.                         }
  79.                      }
  80.                      else
  81.                      {
  82.                         _loc11_ = _loc6_ <= 1 ? Vector.ZERO : _loc1_.span;
  83.                         _loc9_ = _loc23_ = new Vector(_loc11_.x - _loc3_.x,_loc11_.y - _loc3_.y).length;
  84.                         var _loc8_ = _loc23_.dot(_loc14_);
  85.                         _loc2_ = _loc5_ * _loc5_ - (_loc9_ * _loc9_ - _loc8_ * _loc8_);
  86.                         if(_loc8_ > 0 && _loc2_ > 0 && (_loc2_ = _loc8_ - Math.sqrt(_loc2_)) < _loc4_)
  87.                         {
  88.                            if(_loc7_ < _loc2_ / _loc4_ || _loc2_ / _loc4_ < 0)
  89.                            {
  90.                               continue;
  91.                            }
  92.                            _loc7_ = _loc2_ / _loc4_;
  93.                            _loc20_ = _loc1_;
  94.                            _loc12_ = new Vector(_loc3_.x + _loc2_ * _loc14_.x,_loc3_.y + _loc2_ * _loc14_.y,0);
  95.                            (_loc21_ = new Vector(_loc12_.x - _loc11_.x,_loc12_.y - _loc11_.y,0)).normalize();
  96.                         }
  97.                      }
  98.                   }
  99.                }
  100.             }
  101.          }
  102.       }
  103.       if(_loc20_)
  104.       {
  105.          _loc12_.x += shape.x + _loc20_.a.x;
  106.          _loc12_.y += shape.y + _loc20_.a.y;
  107.          return new Engine.Collision(_loc12_,_loc21_,_loc7_,_loc20_);
  108.       }
  109.       return null;
  110.    }
  111.    static function sweepSphereToSphere(movingObj, obj, noData)
  112.    {
  113.       var _loc6_ = undefined;
  114.       var _loc4_ = undefined;
  115.       var _loc5_ = movingObj.radius + obj.radius;
  116.       _loc4_ = _loc6_ = new Vector(obj.x - movingObj.x,obj.y - movingObj.y,0).length;
  117.       var _loc2_ = _loc6_.dot(movingObj.motionDir);
  118.       var _loc1_ = _loc5_ * _loc5_ - (_loc4_ * _loc4_ - _loc2_ * _loc2_);
  119.       if(_loc1_ > 0 && (_loc1_ = _loc2_ - Math.sqrt(_loc1_)) < movingObj.motionDist)
  120.       {
  121.          if(_loc1_ < 0)
  122.          {
  123.             return false;
  124.          }
  125.          if(noData)
  126.          {
  127.             return true;
  128.          }
  129.       }
  130.       return false;
  131.    }
  132. }
  133.