Advertisement
Guest User

Untitled

a guest
Mar 12th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: VirtualMaestro
  4.  * Date: 11.03.12
  5.  */
  6. package tests
  7. {
  8.     import flash.display.Sprite;
  9.     import flash.events.Event;
  10.  
  11.     import nape.geom.Vec2;
  12.     import nape.phys.Body;
  13.     import nape.phys.BodyType;
  14.     import nape.phys.Material;
  15.     import nape.shape.Circle;
  16.     import nape.shape.Polygon;
  17.  
  18.     [SWF(width=800, height=600, backgroundColor=0xdddddd, frameRate=30)]
  19.     public class TestDampening extends Sprite
  20.     {
  21.         static public const APP_WIDTH:int = 800;
  22.         static public const APP_HEIGHT:int = 600;
  23.  
  24.         private var _core:InitNape;
  25.         private var _frameRate:Number = 30;
  26.         private var _timeStep:Number = 1 / _frameRate;
  27.  
  28.         public function TestDampening(initNape:InitNape = null)
  29.         {
  30.             _core = initNape;
  31.  
  32.             if (_core == null)
  33.             {
  34.                 _core = new InitNape(APP_WIDTH, APP_HEIGHT, _frameRate, 10);
  35.                 addChild(_core);
  36.             }
  37.  
  38.             init();
  39.         }
  40.  
  41.         private function init():void
  42.         {
  43.             _core.space().gravity.setxy(0, 0);
  44.  
  45.             createWalls();
  46.             createActor();
  47.             initWorldDampening();
  48.  
  49.             addEventListener(Event.ENTER_FRAME, update);
  50.         }
  51.  
  52.         private function createWalls():void
  53.         {
  54.             var upWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH / 2, 50));
  55.             upWall.shapes.add(new Polygon(Polygon.box(APP_WIDTH, 50)));
  56.             upWall.space = _core.space();
  57.  
  58.             var bottomWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH / 2, APP_HEIGHT - 50));
  59.             bottomWall.shapes.add(new Polygon(Polygon.box(APP_WIDTH, 50)));
  60.             bottomWall.space = _core.space();
  61.  
  62.             var leftWall:Body = new Body(BodyType.STATIC, new Vec2(50, APP_HEIGHT / 2));
  63.             leftWall.shapes.add(new Polygon(Polygon.box(50, APP_HEIGHT)));
  64.             leftWall.space = _core.space();
  65.  
  66.             var rightWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH - 50, APP_HEIGHT / 2));
  67.             rightWall.shapes.add(new Polygon(Polygon.box(50, APP_HEIGHT)));
  68.             rightWall.space = _core.space();
  69.         }
  70.  
  71.         private var _actor:Body;
  72.         private var _linearDampening:Number = Math.abs(Math.random()); // 0- полное отсутсвие торможения - до бесконечности
  73.         private var _angularDampening:Number = Math.abs(Math.random()); // 0- полное отсутсвие торможения - до бесконечности
  74.  
  75.         private function createActor():void
  76.         {
  77.             _actor = new Body(BodyType.DYNAMIC, new Vec2(APP_WIDTH / 2, APP_HEIGHT / 2));
  78.             _actor.shapes.add(new Circle(20, null, new Material(0.5)));
  79.             _actor.space = _core.space();
  80.  
  81.             var linVel:int = 300;
  82.             var angVel:int = 30;
  83.             _actor.velocity.setxy(linVel, linVel);
  84.             _actor.angularVel = angVel;
  85.         }
  86.  
  87.         private function initWorldDampening():void
  88.         {
  89.             _core.space().worldLinearDrag = _linearDampening;
  90.             _core.space().worldAngularDrag = _angularDampening;
  91.         }
  92.  
  93.         private function update(event:Event):void
  94.         {
  95.             var vel:Vec2 = _actor.velocity;
  96.             var velX:int = 0;
  97.             var velY:int = 0;
  98.             var ang:int = 0;
  99.  
  100.             velX = (vel.x + ((-1) * vel.x * _linearDampening * _timeStep)) * 1000;
  101.             velY = (vel.y + ((-1) * vel.y * _linearDampening * _timeStep)) * 1000;
  102.             vel.x = velX / 1000.0;
  103.             vel.y = velY / 1000.0;
  104.  
  105.             ang = (_actor.angularVel + ((-1) * _actor.angularVel * _angularDampening * _timeStep)) * 1000;
  106.             _actor.angularVel = ang / 1000.0;
  107.  
  108.             trace("vel x/y: " + _actor.velocity.x + "/" + _actor.velocity.y + "; ang: " + _actor.angularVel);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement