Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created with IntelliJ IDEA.
- * User: VirtualMaestro
- * Date: 11.03.12
- */
- package tests
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import nape.geom.Vec2;
- import nape.phys.Body;
- import nape.phys.BodyType;
- import nape.phys.Material;
- import nape.shape.Circle;
- import nape.shape.Polygon;
- [SWF(width=800, height=600, backgroundColor=0xdddddd, frameRate=30)]
- public class TestDampening extends Sprite
- {
- static public const APP_WIDTH:int = 800;
- static public const APP_HEIGHT:int = 600;
- private var _core:InitNape;
- private var _frameRate:Number = 30;
- private var _timeStep:Number = 1 / _frameRate;
- public function TestDampening(initNape:InitNape = null)
- {
- _core = initNape;
- if (_core == null)
- {
- _core = new InitNape(APP_WIDTH, APP_HEIGHT, _frameRate, 10);
- addChild(_core);
- }
- init();
- }
- private function init():void
- {
- _core.space().gravity.setxy(0, 0);
- createWalls();
- createActor();
- initWorldDampening();
- addEventListener(Event.ENTER_FRAME, update);
- }
- private function createWalls():void
- {
- var upWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH / 2, 50));
- upWall.shapes.add(new Polygon(Polygon.box(APP_WIDTH, 50)));
- upWall.space = _core.space();
- var bottomWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH / 2, APP_HEIGHT - 50));
- bottomWall.shapes.add(new Polygon(Polygon.box(APP_WIDTH, 50)));
- bottomWall.space = _core.space();
- var leftWall:Body = new Body(BodyType.STATIC, new Vec2(50, APP_HEIGHT / 2));
- leftWall.shapes.add(new Polygon(Polygon.box(50, APP_HEIGHT)));
- leftWall.space = _core.space();
- var rightWall:Body = new Body(BodyType.STATIC, new Vec2(APP_WIDTH - 50, APP_HEIGHT / 2));
- rightWall.shapes.add(new Polygon(Polygon.box(50, APP_HEIGHT)));
- rightWall.space = _core.space();
- }
- private var _actor:Body;
- private var _linearDampening:Number = Math.abs(Math.random()); // 0- полное отсутсвие торможения - до бесконечности
- private var _angularDampening:Number = Math.abs(Math.random()); // 0- полное отсутсвие торможения - до бесконечности
- private function createActor():void
- {
- _actor = new Body(BodyType.DYNAMIC, new Vec2(APP_WIDTH / 2, APP_HEIGHT / 2));
- _actor.shapes.add(new Circle(20, null, new Material(0.5)));
- _actor.space = _core.space();
- var linVel:int = 300;
- var angVel:int = 30;
- _actor.velocity.setxy(linVel, linVel);
- _actor.angularVel = angVel;
- }
- private function initWorldDampening():void
- {
- _core.space().worldLinearDrag = _linearDampening;
- _core.space().worldAngularDrag = _angularDampening;
- }
- private function update(event:Event):void
- {
- var vel:Vec2 = _actor.velocity;
- var velX:int = 0;
- var velY:int = 0;
- var ang:int = 0;
- velX = (vel.x + ((-1) * vel.x * _linearDampening * _timeStep)) * 1000;
- velY = (vel.y + ((-1) * vel.y * _linearDampening * _timeStep)) * 1000;
- vel.x = velX / 1000.0;
- vel.y = velY / 1000.0;
- ang = (_actor.angularVel + ((-1) * _actor.angularVel * _angularDampening * _timeStep)) * 1000;
- _actor.angularVel = ang / 1000.0;
- trace("vel x/y: " + _actor.velocity.x + "/" + _actor.velocity.y + "; ang: " + _actor.angularVel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement