Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import Box2D.Collision.b2AABB;
- import Box2D.Collision.Shapes.b2CircleDef;
- import Box2D.Collision.Shapes.b2CircleShape;
- import Box2D.Collision.Shapes.b2PolygonDef;
- import Box2D.Collision.Shapes.b2PolygonShape;
- import Box2D.Collision.Shapes.b2Shape;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Dynamics.b2Body;
- import Box2D.Dynamics.b2BodyDef;
- import Box2D.Dynamics.b2World;
- import flash.display.Shape;
- import flash.display.Sprite;
- import flash.events.Event;
- /**
- * ...
- * @author Erlend Hofstad Langseth
- */
- public class Main extends Sprite
- {
- public function Main():void
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private var w:b2World;
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- // entry point
- addChild(s);
- var bounding:b2AABB = new b2AABB();
- bounding.lowerBound.Set( - 100, - 100);
- bounding.upperBound.Set( 1000, 1000);
- w = new b2World(bounding, new b2Vec2(0, -10), true);
- var groundBodyDef:b2BodyDef = new b2BodyDef();
- groundBodyDef.position.Set(400, 580);
- var groundBody:b2Body = w.CreateBody(groundBodyDef);
- var groundShapeDef:b2PolygonDef = new b2PolygonDef();
- groundShapeDef.SetAsBox(400, 20);
- groundBody.CreateShape(groundShapeDef);
- //
- var circleBodyDef:b2BodyDef = new b2BodyDef();
- circleBodyDef.position.Set(300, 200);
- var circleBody:b2Body = w.CreateBody(circleBodyDef);
- var circleDef:b2CircleDef = new b2CircleDef();
- circleDef.radius = 10;
- circleDef.density = 2;
- circleDef.friction = 0.5;
- circleBody.CreateShape(circleDef);
- addEventListener(Event.ENTER_FRAME, update);
- }
- private var s:Shape = new Shape();
- private function update(e:Event):void
- {
- trace("FRAM;E");
- w.Step(1 / 30, 10);
- // DRAW
- s.graphics.clear();
- s.graphics.lineStyle(1, 0xaaaaaa);
- s.graphics.beginFill(0x222222);
- var list:b2Body = w.GetBodyList();
- var shapeList:b2Shape;
- if (!list) return;
- do {
- shapeList = list.GetShapeList();
- if (shapeList) {
- do {
- drawShape(shapeList, list.GetPosition().x, list.GetPosition().y);
- } while (shapeList = shapeList.GetNext());
- }
- //
- } while (list = list.GetNext());
- }
- private function drawShape(shape:b2Shape, x:int, y:int):void
- {
- if (shape is b2PolygonShape) {
- var polygon:b2PolygonShape = shape as b2PolygonShape;
- var ver:Array = polygon.GetVertices();
- delUndefined(ver);
- var vec:b2Vec2;
- for (var i:int = ver.length - 1; i >= 0; i --) {
- vec = ver[i];
- if (vec) {
- trace(i, " : " , ver.length, vec.x, x, vec.y, y);
- if (i == ver.length - 1) s.graphics.moveTo(vec.x + x, vec.y + y);
- else s.graphics.lineTo(vec.x + x, vec.y + y);
- }
- }
- } else if (shape is b2CircleShape) {
- var circle:b2CircleShape = shape as b2CircleShape;
- s.graphics.drawCircle(x, y, circle.GetRadius());
- }
- }
- private function delUndefined(array:Array):void
- {
- for (var i:int = array.length - 1; i >= 0; i --) {
- if (array[i] == undefined) {
- array.splice(i, 1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement