home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Corrida / amazingrace.swf / scripts / __Packages / Pirrest / PhisicsEngine / Surface.as < prev    next >
Encoding:
Text File  |  2006-06-13  |  4.4 KB  |  149 lines

  1. class Pirrest.PhisicsEngine.Surface extends MovieClip
  2. {
  3.    var coord_space;
  4.    var p1;
  5.    var p2;
  6.    var isOrientH;
  7.    var normal;
  8.    var rise;
  9.    var run;
  10.    var invRun;
  11.    var slope;
  12.    var invB;
  13.    function Surface()
  14.    {
  15.       super();
  16.    }
  17.    function init($coord_space)
  18.    {
  19.       this.coord_space = $coord_space;
  20.       this.p1.init($coord_space);
  21.       this.p2.init($coord_space);
  22.       this.isOrientH = true;
  23.       this.normal = new Pirrest.PhisicsEngine.Vector(0,0);
  24.       this.calcNormal();
  25.       this.rise = this.p2.y - this.p1.y;
  26.       this.run = this.p2.x - this.p1.x;
  27.       this.invRun = 1 / this.run;
  28.       this.slope = this.rise / this.run;
  29.       this.invB = 1 / (this.run * this.run + this.rise * this.rise);
  30.       this.coord_space.lineStyle(10,2237064,100);
  31.    }
  32.    function setIsOrientH($isOrientH)
  33.    {
  34.       this.isOrientH = $isOrientH;
  35.    }
  36.    function calcNormal()
  37.    {
  38.       var _loc4_ = this.p2.x - this.p1.x;
  39.       var _loc3_ = this.p2.y - this.p1.y;
  40.       this.normal.x = _loc3_;
  41.       this.normal.y = - _loc4_;
  42.       var _loc2_ = Math.sqrt(this.normal.x * this.normal.x + this.normal.y * this.normal.y);
  43.       this.normal.x /= _loc2_;
  44.       this.normal.y /= _loc2_;
  45.    }
  46.    function paint()
  47.    {
  48.       Pirrest.PhisicsEngine.Graphics.prototype.paintLine(this.coord_space,this.p1.x,this.p1.y,this.p2.x,this.p2.y);
  49.    }
  50.    function resolveWheelCollision($w)
  51.    {
  52.       if(this.bounds($w.wp.curr,$w.contactRadius))
  53.       {
  54.          this.getClosestPoint($w.wp.curr,$w.closestPoint);
  55.          var _loc3_ = $w.closestPoint.minusNew($w.wp.curr);
  56.          _loc3_.normalize();
  57.          if(this.inequality($w.wp.curr))
  58.          {
  59.             var _loc5_ = Math.abs(_loc3_.x);
  60.             _loc3_.x = this.normal.x >= 0 ? - _loc5_ : _loc5_;
  61.             _loc3_.y = Math.abs(_loc3_.y);
  62.          }
  63.          var _loc4_ = $w.wp.curr.plusNew(_loc3_.mult($w.wr));
  64.          if(this.segmentInequality(_loc4_))
  65.          {
  66.             var _loc7_ = _loc4_.x - $w.closestPoint.x;
  67.             var _loc6_ = _loc4_.y - $w.closestPoint.y;
  68.             $w.wp.curr.x -= _loc7_;
  69.             $w.wp.curr.y -= _loc6_;
  70.             $w.resolve(this.normal);
  71.          }
  72.       }
  73.    }
  74.    function resolveParticleCollision($p, $sysObj)
  75.    {
  76.       if(this.boundedSegmentInequality($p.curr))
  77.       {
  78.          var _loc3_ = $p.curr.minusNew($p.prev);
  79.          var _loc2_ = this.normal.dot(_loc3_);
  80.          if(_loc2_ < 0)
  81.          {
  82.             var _loc8_ = _loc3_.minusNew(this.normal.multNew(_loc2_));
  83.             var _loc5_ = _loc8_.multNew($sysObj.coeffFric);
  84.             var _loc6_ = this.normal.multNew(_loc2_ * $sysObj.coeffRest);
  85.             var _loc7_ = _loc6_.plusNew(_loc5_);
  86.             var _loc11_ = _loc3_.minusNew(_loc7_);
  87.             var _loc10_ = this.normal.dot($p.curr.minusNew(this.p1)) * $sysObj.coeffRest;
  88.          }
  89.       }
  90.    }
  91.    function segmentInequality($toPoint)
  92.    {
  93.       var _loc2_ = this.findU($toPoint);
  94.       var _loc3_ = this.inequality($toPoint);
  95.       return _loc2_ >= 0 && _loc2_ <= 1 && _loc3_;
  96.    }
  97.    function boundedSegmentInequality($toPoint)
  98.    {
  99.       var _loc3_ = undefined;
  100.       if(this.isOrientH)
  101.       {
  102.          _loc3_ = $toPoint.x >= this.p1.x && $toPoint.x <= this.p2.x;
  103.       }
  104.       else if(this.p1.y < this.p2.y)
  105.       {
  106.          _loc3_ = $toPoint.y >= this.p1.y && $toPoint.y <= this.p2.y;
  107.       }
  108.       else
  109.       {
  110.          _loc3_ = $toPoint.y <= this.p1.y && $toPoint.y >= this.p2.y;
  111.       }
  112.       if(_loc3_)
  113.       {
  114.          return this.inequality($toPoint);
  115.       }
  116.       return false;
  117.    }
  118.    function inequality($toPoint)
  119.    {
  120.       var _loc2_ = this.slope * ($toPoint.x - this.p1.x) + (this.p1.y - $toPoint.y);
  121.       return _loc2_ <= 0;
  122.    }
  123.    function bounds($toPoint, $r)
  124.    {
  125.       return $toPoint.x >= this.p1.x - $r && $toPoint.x <= this.p2.x + $r;
  126.    }
  127.    function getClosestPoint($toPoint, $returnVect)
  128.    {
  129.       var _loc2_ = this.findU($toPoint);
  130.       if(_loc2_ <= 0)
  131.       {
  132.          return this.p1;
  133.       }
  134.       if(_loc2_ >= 1)
  135.       {
  136.          return this.p2;
  137.       }
  138.       var _loc4_ = this.p1.x + _loc2_ * (this.p2.x - this.p1.x);
  139.       var _loc3_ = this.p1.y + _loc2_ * (this.p2.y - this.p1.y);
  140.       $returnVect.x = _loc4_;
  141.       $returnVect.y = _loc3_;
  142.    }
  143.    function findU($p)
  144.    {
  145.       var _loc2_ = ($p.x - this.p1.x) * this.run + ($p.y - this.p1.y) * this.rise;
  146.       return _loc2_ * this.invB;
  147.    }
  148. }
  149.