Create a Flash game like Roly Poly with Box2D – Step 2
Welcome to the second step. It’s time to build a level and let the player rotate it with the mouse.
I am going to show you two ways to rotate the level, anyway let’s start fromt the beginning: in the making of step 1 I told you it’s a tile based game, so I am going to use an array to store the tiles.
At the moment each array item can have three values:
0: empty space
1: tile
2: the player
So let me write some more code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),false); private var worldScale:Number=30; private var rolyCenter:b2Vec2=new b2Vec2(320,240); private var rolyLevel:b2Body; private var rolyRadius:Number=230; private var rolyPieces:Number=24; private var tileSize:Number=32; private var level:Array=new Array(); private var startAngle:Number; private var startRolyAngle:Number; public function Main() { level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1); level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1); level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1); level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1); level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1); level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); var centerAngle:Number=2*Math.PI/rolyPieces; var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale; rolyCenter.Multiply(1/worldScale); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position=rolyCenter; rolyLevel=world.CreateBody(bodyDef); var polygonShape:b2PolygonShape=new b2PolygonShape(); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1; fixtureDef.restitution=0; fixtureDef.friction=1; for (var i:Number=0; i<rolyPieces; i++) { var angle:Number=2*Math.PI/rolyPieces*i; polygonShape.SetAsOrientedBox(0.1,rolySide,new b2Vec2(rolyRadius/worldScale*Math.cos(angle),rolyRadius/worldScale*Math.sin(angle)),angle); rolyLevel.CreateFixture(fixtureDef); } var numberOfTiles:Number=Math.ceil(rolyRadius*2/tileSize); if (numberOfTiles%2==0) { numberOfTiles++; } var tileSizePX:Number=tileSize/worldScale; for (i=0; i<numberOfTiles; i++) { for (var j:Number=0; j<numberOfTiles; j++) { switch (level[numberOfTiles-1-j][numberOfTiles-1-i]) { case 1 : polygonShape.SetAsOrientedBox(tileSizePX/2,tileSizePX/2,new b2Vec2(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2,tileSizePX*(numberOfTiles/2-j)-tileSizePX/2),0); rolyLevel.CreateFixture(fixtureDef); break; case 2 : createSphere(rolyCenter.x+(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2),rolyCenter.y+(tileSizePX*(numberOfTiles/2-j)-tileSizePX/2)); break; } } } addEventListener(Event.ENTER_FRAME,updateWorld); } private function createSphere(pX:Number,pY:Number):void { var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(pX,pY); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.bullet=true; var circleShape:b2CircleShape=new b2CircleShape(10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=circleShape; fixtureDef.density=1; fixtureDef.restitution=0.5; fixtureDef.friction=1; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); var debugSprite:Sprite=new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
There’s nothing interesting in it, apart from level array used to build the level.
This is what we have at the moment:
A static level with a dynamic sphere. We must first make the level dynamic, with a revolute joint to pin it to the stage, following the same concept explained int he post creation of a rotating room with Flash and Box2D, so I would change the script this way:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),false); private var worldScale:Number=30; private var rolyCenter:b2Vec2=new b2Vec2(320,240); private var rolyLevel:b2Body; private var rolyRadius:Number=230; private var rolyPieces:Number=24; private var tileSize:Number=32; private var level:Array=new Array(); private var startAngle:Number; private var startRolyAngle:Number; public function Main() { level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1); level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1); level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1); level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1); level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1); level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); var centerAngle:Number=2*Math.PI/rolyPieces; var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale; rolyCenter.Multiply(1/worldScale); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.position=rolyCenter; rolyLevel=world.CreateBody(bodyDef); var polygonShape:b2PolygonShape=new b2PolygonShape(); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1; fixtureDef.restitution=0; fixtureDef.friction=1; for (var i:Number=0; i<rolyPieces; i++) { var angle:Number=2*Math.PI/rolyPieces*i; polygonShape.SetAsOrientedBox(0.1,rolySide,new b2Vec2(rolyRadius/worldScale*Math.cos(angle),rolyRadius/worldScale*Math.sin(angle)),angle); rolyLevel.CreateFixture(fixtureDef); } var numberOfTiles:Number=Math.ceil(rolyRadius*2/tileSize); if (numberOfTiles%2==0) { numberOfTiles++; } var tileSizePX:Number=tileSize/worldScale; for (i=0; i<numberOfTiles; i++) { for (var j:Number=0; j<numberOfTiles; j++) { switch (level[numberOfTiles-1-j][numberOfTiles-1-i]) { case 1 : polygonShape.SetAsOrientedBox(tileSizePX/2,tileSizePX/2,new b2Vec2(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2,tileSizePX*(numberOfTiles/2-j)-tileSizePX/2),0); rolyLevel.CreateFixture(fixtureDef); break; case 2 : createSphere(rolyCenter.x+(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2),rolyCenter.y+(tileSizePX*(numberOfTiles/2-j)-tileSizePX/2)); break; } } } var containerJoint:b2RevoluteJointDef=new b2RevoluteJointDef(); var anchorA:b2Vec2=new b2Vec2(0,0); var anchorB:b2Vec2=rolyCenter; containerJoint.localAnchorA=anchorA; containerJoint.localAnchorB=anchorB; containerJoint.bodyA=rolyLevel; containerJoint.bodyB=world.GetGroundBody(); world.CreateJoint(containerJoint); addEventListener(Event.ENTER_FRAME,updateWorld); } private function createSphere(pX:Number,pY:Number):void { var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(pX,pY); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.bullet=true; var circleShape:b2CircleShape=new b2CircleShape(10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=circleShape; fixtureDef.density=1; fixtureDef.restitution=0.5; fixtureDef.friction=1; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); var debugSprite:Sprite=new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
I used a revolute joint to pin the level to the stage, and made it dynamic. The problem is the level isn’t perfectly balanced, so it will start to rotate although there isn’t any player interaction:
We need a motor with an high torque to keep the level fixed in its starting position, no matter if it’s perfectly balanced, this way:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),false); private var worldScale:Number=30; private var rolyCenter:b2Vec2=new b2Vec2(320,240); private var rolyLevel:b2Body; private var rolyRadius:Number=230; private var rolyPieces:Number=24; private var tileSize:Number=32; private var level:Array=new Array(); private var startAngle:Number; private var startRolyAngle:Number; public function Main() { level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1); level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1); level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1); level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1); level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1); level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); var centerAngle:Number=2*Math.PI/rolyPieces; var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale; rolyCenter.Multiply(1/worldScale); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.position=rolyCenter; rolyLevel=world.CreateBody(bodyDef); var polygonShape:b2PolygonShape=new b2PolygonShape(); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1; fixtureDef.restitution=0; fixtureDef.friction=1; for (var i:Number=0; i<rolyPieces; i++) { var angle:Number=2*Math.PI/rolyPieces*i; polygonShape.SetAsOrientedBox(0.1,rolySide,new b2Vec2(rolyRadius/worldScale*Math.cos(angle),rolyRadius/worldScale*Math.sin(angle)),angle); rolyLevel.CreateFixture(fixtureDef); } var numberOfTiles:Number=Math.ceil(rolyRadius*2/tileSize); if (numberOfTiles%2==0) { numberOfTiles++; } var tileSizePX:Number=tileSize/worldScale; for (i=0; i<numberOfTiles; i++) { for (var j:Number=0; j<numberOfTiles; j++) { switch (level[numberOfTiles-1-j][numberOfTiles-1-i]) { case 1 : polygonShape.SetAsOrientedBox(tileSizePX/2,tileSizePX/2,new b2Vec2(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2,tileSizePX*(numberOfTiles/2-j)-tileSizePX/2),0); rolyLevel.CreateFixture(fixtureDef); break; case 2 : createSphere(rolyCenter.x+(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2),rolyCenter.y+(tileSizePX*(numberOfTiles/2-j)-tileSizePX/2)); break; } } } var containerJoint:b2RevoluteJointDef=new b2RevoluteJointDef(); var anchorA:b2Vec2=new b2Vec2(0,0); var anchorB:b2Vec2=rolyCenter; containerJoint.localAnchorA=anchorA; containerJoint.localAnchorB=anchorB; containerJoint.bodyA=rolyLevel; containerJoint.bodyB=world.GetGroundBody(); containerJoint.enableMotor=true; containerJoint.maxMotorTorque=500000; containerJoint.motorSpeed=0; world.CreateJoint(containerJoint); addEventListener(Event.ENTER_FRAME,updateWorld); } private function createSphere(pX:Number,pY:Number):void { var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(pX,pY); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.bullet=true; var circleShape:b2CircleShape=new b2CircleShape(10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=circleShape; fixtureDef.density=1; fixtureDef.restitution=0.5; fixtureDef.friction=1; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); var debugSprite:Sprite=new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
And now we have our dynamic pinned level pacificly waiting for our interaction:
Now, a simple mouse joint will let us have our first working example of a roly poly level:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),false); private var worldScale:Number=30; private var rolyCenter:b2Vec2=new b2Vec2(320,240); private var rolyLevel:b2Body; private var rolyRadius:Number=230; private var rolyPieces:Number=24; private var tileSize:Number=32; private var level:Array=new Array(); private var startAngle:Number; private var startRolyAngle:Number; private var mouseJoint:b2MouseJoint; public function Main() { level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1); level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1); level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1); level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1); level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1); level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); var centerAngle:Number=2*Math.PI/rolyPieces; var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale; rolyCenter.Multiply(1/worldScale); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.position=rolyCenter; rolyLevel=world.CreateBody(bodyDef); var polygonShape:b2PolygonShape=new b2PolygonShape(); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1; fixtureDef.restitution=0; fixtureDef.friction=1; for (var i:Number=0; i<rolyPieces; i++) { var angle:Number=2*Math.PI/rolyPieces*i; polygonShape.SetAsOrientedBox(0.1,rolySide,new b2Vec2(rolyRadius/worldScale*Math.cos(angle),rolyRadius/worldScale*Math.sin(angle)),angle); rolyLevel.CreateFixture(fixtureDef); } var numberOfTiles:Number=Math.ceil(rolyRadius*2/tileSize); if (numberOfTiles%2==0) { numberOfTiles++; } var tileSizePX:Number=tileSize/worldScale; for (i=0; i<numberOfTiles; i++) { for (var j:Number=0; j<numberOfTiles; j++) { switch (level[numberOfTiles-1-j][numberOfTiles-1-i]) { case 1 : polygonShape.SetAsOrientedBox(tileSizePX/2,tileSizePX/2,new b2Vec2(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2,tileSizePX*(numberOfTiles/2-j)-tileSizePX/2),0); rolyLevel.CreateFixture(fixtureDef); break; case 2 : createSphere(rolyCenter.x+(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2),rolyCenter.y+(tileSizePX*(numberOfTiles/2-j)-tileSizePX/2)); break; } } } var containerJoint:b2RevoluteJointDef=new b2RevoluteJointDef(); var anchorA:b2Vec2=new b2Vec2(0,0); var anchorB:b2Vec2=rolyCenter; containerJoint.localAnchorA=anchorA; containerJoint.localAnchorB=anchorB; containerJoint.bodyA=rolyLevel; containerJoint.bodyB=world.GetGroundBody(); containerJoint.enableMotor=true; containerJoint.maxMotorTorque=500000; containerJoint.motorSpeed=0; world.CreateJoint(containerJoint); addEventListener(Event.ENTER_FRAME,updateWorld); stage.addEventListener(MouseEvent.MOUSE_DOWN,startRotation); } private function createSphere(pX:Number,pY:Number):void { var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(pX,pY); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.bullet=true; var circleShape:b2CircleShape=new b2CircleShape(10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=circleShape; fixtureDef.density=1; fixtureDef.restitution=0.5; fixtureDef.friction=1; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } private function startRotation(e:MouseEvent):void { var jointDef:b2MouseJointDef=new b2MouseJointDef(); jointDef.bodyA=world.GetGroundBody(); jointDef.bodyB=rolyLevel; jointDef.target.Set(mouseX/worldScale,mouseY/worldScale); jointDef.maxForce=1000*rolyLevel.GetMass(); mouseJoint=world.CreateJoint(jointDef) as b2MouseJoint; stage.removeEventListener(MouseEvent.MOUSE_DOWN,startRotation); stage.addEventListener(MouseEvent.MOUSE_MOVE,rotate); stage.addEventListener(MouseEvent.MOUSE_UP,stopRotation); } private function rotate(e:MouseEvent):void { mouseJoint.SetTarget(new b2Vec2(mouseX/worldScale,mouseY/worldScale)); } private function stopRotation(e:MouseEvent):void { world.DestroyJoint(mouseJoint); mouseJoint=null; stage.addEventListener(MouseEvent.MOUSE_DOWN,startRotation); stage.removeEventListener(MouseEvent.MOUSE_MOVE,rotate); stage.removeEventListener(MouseEvent.MOUSE_UP,stopRotation); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); var debugSprite:Sprite=new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
And that’s it:
Click and drag the mouse everywhere to rotate the level.
Since I said I was showing you two ways to rotate the level, what about rotating it according to horizontal mouse position? The more the mouse on the left, the faster the rotation counter-clockwise, the more the mouse on the right, the faster the rotation clockwise, only acting on revolute joint motor speed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),false); private var worldScale:Number=30; private var rolyCenter:b2Vec2=new b2Vec2(320,240); private var rolyLevel:b2Body; private var rolyRadius:Number=230; private var rolyPieces:Number=24; private var tileSize:Number=32; private var level:Array=new Array(); private var startAngle:Number; private var startRolyAngle:Number; private var rolyJoint=b2RevoluteJoint; public function Main() { level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1); level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1); level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1); level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1); level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1); level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1); level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1); level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); var centerAngle:Number=2*Math.PI/rolyPieces; var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale; rolyCenter.Multiply(1/worldScale); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.position=rolyCenter; rolyLevel=world.CreateBody(bodyDef); var polygonShape:b2PolygonShape=new b2PolygonShape(); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1; fixtureDef.restitution=0; fixtureDef.friction=1; for (var i:Number=0; i<rolyPieces; i++) { var angle:Number=2*Math.PI/rolyPieces*i; polygonShape.SetAsOrientedBox(0.1,rolySide,new b2Vec2(rolyRadius/worldScale*Math.cos(angle),rolyRadius/worldScale*Math.sin(angle)),angle); rolyLevel.CreateFixture(fixtureDef); } var numberOfTiles:Number=Math.ceil(rolyRadius*2/tileSize); if (numberOfTiles%2==0) { numberOfTiles++; } var tileSizePX:Number=tileSize/worldScale; for (i=0; i<numberOfTiles; i++) { for (var j:Number=0; j<numberOfTiles; j++) { switch (level[numberOfTiles-1-j][numberOfTiles-1-i]) { case 1 : polygonShape.SetAsOrientedBox(tileSizePX/2,tileSizePX/2,new b2Vec2(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2,tileSizePX*(numberOfTiles/2-j)-tileSizePX/2),0); rolyLevel.CreateFixture(fixtureDef); break; case 2 : createSphere(rolyCenter.x+(tileSizePX*(numberOfTiles/2-i)-tileSizePX/2),rolyCenter.y+(tileSizePX*(numberOfTiles/2-j)-tileSizePX/2)); break; } } } var containerJoint:b2RevoluteJointDef=new b2RevoluteJointDef(); var anchorA:b2Vec2=new b2Vec2(0,0); var anchorB:b2Vec2=rolyCenter; containerJoint.localAnchorA=anchorA; containerJoint.localAnchorB=anchorB; containerJoint.bodyA=rolyLevel; containerJoint.bodyB=world.GetGroundBody(); containerJoint.enableMotor=true; containerJoint.maxMotorTorque=500000; containerJoint.motorSpeed=0; rolyJoint=world.CreateJoint(containerJoint) as b2RevoluteJoint; addEventListener(Event.ENTER_FRAME,updateWorld); } private function createSphere(pX:Number,pY:Number):void { var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(pX,pY); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.bullet=true; var circleShape:b2CircleShape=new b2CircleShape(10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=circleShape; fixtureDef.density=1; fixtureDef.restitution=0.5; fixtureDef.friction=1; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); var debugSprite:Sprite=new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { rolyJoint.SetMotorSpeed((mouseX-rolyCenter.x*worldScale)/100) world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
And this is the result:
Move the mouse along its x axis to see the level rotate.
No need to download anything, just copy/paste the code you need into Main class you can find at step 1.
They can be easily customized to meet the unique requirements of your project.













This post has 6 comments
ViZgl
The simplest way is change gravity vector and rotate graphics based on it.
Create a Flash game like Roly Poly with Box2D – Step 2 – Emanuele Feronato « eaflash
[...] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like [...]
Bhuwan
Hi!
Can you please tell where should I prefer WCK? As I think in the above game WCK would surely save a lot of time and effort?
Also I am unable to find advanced tutorials on WCK? Can you please guide me on how should I proceed? I read the basic tutorial on this blog and have gone through the demo files it gives(though didn’t get some of them)
Thanks
Bhuwan
Also it would be very helpful if you can please explain the relative significance of wck, quickbox2D, citrus engine and other libraries/engines you have discussed on your blog.
Thanks.
MC
you should say which lines were modified in the code on each step
CHINVA
HI Emanuele
can you put an example with texture or make a tutorial on how to texture a tiles made by box2d thanks