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

  1. class Pirrest.PhisicsEngine.ParticleSystem
  2. {
  3.    var _particles_ar;
  4.    var _surfaces_ar;
  5.    var _constraints_ar;
  6.    var _wheels_ar;
  7.    var gravity;
  8.    var coeffRest;
  9.    var coeffFric;
  10.    var coeffDamp;
  11.    var _p_count;
  12.    var _w_count;
  13.    var _s_count;
  14.    var _c_count;
  15.    function ParticleSystem()
  16.    {
  17.       this.init();
  18.    }
  19.    function init()
  20.    {
  21.       this._particles_ar = [];
  22.       this._surfaces_ar = [];
  23.       this._constraints_ar = [];
  24.       this._wheels_ar = [];
  25.       this.gravity = new Pirrest.PhisicsEngine.Vector(0,1);
  26.       this.coeffRest = 1.5;
  27.       this.coeffFric = 0.1;
  28.       this.coeffDamp = 0;
  29.    }
  30.    function addParticle($px, $py, $mass)
  31.    {
  32.       var _loc2_ = new Pirrest.PhisicsEngine.Particle($px,$py,$mass);
  33.       this._particles_ar.push(_loc2_);
  34.       this._p_count = this._particles_ar.length;
  35.       return _loc2_;
  36.    }
  37.    function addWheel($wheel)
  38.    {
  39.       this._wheels_ar.push($wheel);
  40.       this._w_count = this._wheels_ar.length;
  41.    }
  42.    function addSurface($s)
  43.    {
  44.       this._surfaces_ar.push($s);
  45.       this._s_count = this._surfaces_ar.length;
  46.    }
  47.    function addCircleSurface($s)
  48.    {
  49.       this._surfaces_ar.push($s);
  50.       this._s_count = this._surfaces_ar.length;
  51.    }
  52.    function addConstraint($p1, $p2)
  53.    {
  54.       var _loc2_ = new Pirrest.PhisicsEngine.Constraint($p1,$p2);
  55.       trace("Add const: " + $p1 + " " + $p2);
  56.       this._constraints_ar.push(_loc2_);
  57.       this._c_count = this._constraints_ar.length;
  58.       return _loc2_;
  59.    }
  60.    function addRectangle($center, $rWidth, $rHeight)
  61.    {
  62.       return new Pirrest.PhisicsEngine.Rectangle(this,$center,$rWidth,$rHeight);
  63.    }
  64.    function addAngularConstraint($p1, $p2, $p3)
  65.    {
  66.       var _loc2_ = new Pirrest.PhisicsEngine.AngularConstraint($p1,$p2,$p3);
  67.       this._constraints_ar.push(_loc2_);
  68.       this._c_count = this._constraints_ar.length;
  69.       return _loc2_;
  70.    }
  71.    function setKfr($kfr)
  72.    {
  73.       this.coeffRest = 1 + $kfr;
  74.    }
  75.    function setFriction($f)
  76.    {
  77.       this.coeffFric = $f;
  78.    }
  79.    function setDamping($d)
  80.    {
  81.       this.coeffDamp = $d;
  82.    }
  83.    function setGravity($gx, $gy)
  84.    {
  85.       this.gravity.x = $gx;
  86.       this.gravity.y = $gy;
  87.    }
  88.    function verlet()
  89.    {
  90.       var _loc2_ = undefined;
  91.       _loc2_ = 0;
  92.       while(_loc2_ < this._p_count)
  93.       {
  94.          this._particles_ar[_loc2_].verlet(this);
  95.          _loc2_ = _loc2_ + 1;
  96.       }
  97.       _loc2_ = 0;
  98.       while(_loc2_ < this._w_count)
  99.       {
  100.          this._wheels_ar[_loc2_].verlet(this);
  101.          _loc2_ = _loc2_ + 1;
  102.       }
  103.    }
  104.    function satisfy_constraints_ar()
  105.    {
  106.       var _loc2_ = undefined;
  107.       _loc2_ = 0;
  108.       while(_loc2_ < this._c_count)
  109.       {
  110.          this._constraints_ar[_loc2_].resolve();
  111.          _loc2_ = _loc2_ + 1;
  112.       }
  113.    }
  114.    function checkCollisions()
  115.    {
  116.       var _loc4_ = undefined;
  117.       var _loc3_ = undefined;
  118.       var _loc2_ = undefined;
  119.       _loc4_ = 0;
  120.       while(_loc4_ < this._s_count)
  121.       {
  122.          _loc3_ = 0;
  123.          while(_loc3_ < this._p_count)
  124.          {
  125.             this._particles_ar[_loc3_].checkCollision(this._surfaces_ar[_loc4_],this);
  126.             _loc3_ = _loc3_ + 1;
  127.          }
  128.          _loc2_ = 0;
  129.          while(_loc2_ < this._w_count)
  130.          {
  131.             this._wheels_ar[_loc2_].checkCollision(this._surfaces_ar[_loc4_],this);
  132.             _loc2_ = _loc2_ + 1;
  133.          }
  134.          _loc4_ = _loc4_ + 1;
  135.       }
  136.    }
  137.    function paintSurfaces()
  138.    {
  139.       var _loc2_ = 0;
  140.       while(_loc2_ < this._surfaces_ar.length)
  141.       {
  142.          this._surfaces_ar[_loc2_].paint();
  143.          _loc2_ = _loc2_ + 1;
  144.       }
  145.    }
  146.    function paintParticles()
  147.    {
  148.       var _loc2_ = 0;
  149.       while(_loc2_ < this._particles_ar.length)
  150.       {
  151.          this._particles_ar[_loc2_].paint();
  152.          _loc2_ = _loc2_ + 1;
  153.       }
  154.    }
  155.    function paintConstraints()
  156.    {
  157.       var _loc2_ = 0;
  158.       while(_loc2_ < this._constraints_ar.length)
  159.       {
  160.          this._constraints_ar[_loc2_].paint();
  161.          _loc2_ = _loc2_ + 1;
  162.       }
  163.    }
  164.    function paintWheels()
  165.    {
  166.       var _loc2_ = 0;
  167.       while(_loc2_ < this._wheels_ar.length)
  168.       {
  169.          this._wheels_ar[_loc2_].paint();
  170.          _loc2_ = _loc2_ + 1;
  171.       }
  172.    }
  173.    function timeStep()
  174.    {
  175.       this.verlet();
  176.       this.satisfy_constraints_ar();
  177.       this.checkCollisions();
  178.    }
  179. }
  180.