Shrink it Box2D prototype
- January 5, 2010 by Emanuele Feronato
- Filed under Actionscript 3, Box2D, Flash, Game design | 3 Comments
Finally the Shrink it prototype is ready.
This post continues Scaling objects with Box2D and Scaling objects with Box2D – part 2, with these features:
1) You can’t modify static objects
2) You can shrink objects with left mouse button, and expand them with left mouse button + SPACE, as in the original game
3) You can shrink/expand any polygon thanks to Ilya‘s suggestion made in this post.
Now in the stage we have four objects: a circle, a square, a triangle and a custom shape. Refer to Understanding custom polygons in Box2D for more information about custom shapes.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.KeyboardEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; public class shrink extends Sprite { var body:b2Body; public var m_world:b2World; public var m_iterations:int=10; public var m_timeStep:Number=1.0/30.0; public var mousePVec:b2Vec2 = new b2Vec2(); // variable to store if the player is pressing SPACE public var space_pressed:Boolean=false; public function shrink() { var worldAABB:b2AABB = new b2AABB(); var bodyDef:b2BodyDef = new b2BodyDef(); var polygon:b2PolygonDef = new b2PolygonDef(); var circleDef:b2CircleDef= new b2CircleDef(); worldAABB.lowerBound.Set(-100.0, -100.0); worldAABB.upperBound.Set(100.0, 100.0); m_world=new b2World(worldAABB,new b2Vec2(0,10),true); // debug draw start var m_sprite:Sprite; m_sprite = new Sprite(); addChild(m_sprite); var dbgDraw:b2DebugDraw = new b2DebugDraw(); var dbgSprite:Sprite = new Sprite(); m_sprite.addChild(dbgSprite); dbgDraw.m_sprite=m_sprite; dbgDraw.m_drawScale=30; dbgDraw.m_alpha=1; dbgDraw.m_fillAlpha=0.5; dbgDraw.m_lineThickness=1; dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit; m_world.SetDebugDraw(dbgDraw); // debug draw end // ground bodyDef.position.Set(10, 12); polygon.SetAsBox(30, 3); polygon.density=0; polygon.friction=0.3; polygon.restitution=0.2; body=m_world.CreateBody(bodyDef); body.CreateShape(polygon); body.SetMassFromShapes(); // circle bodyDef.position.Set(3,5); circleDef.radius=2; circleDef.density=1; circleDef.friction=0.5; circleDef.restitution=0.2; body=m_world.CreateBody(bodyDef); body.CreateShape(circleDef); body.SetMassFromShapes(); // box bodyDef.position.Set(13, 5); polygon.SetAsBox(2, 2); polygon.density=1; polygon.friction=0.5; polygon.restitution=0.2; body=m_world.CreateBody(bodyDef); body.CreateShape(polygon); body.SetMassFromShapes(); // triangle bodyDef.position.Set(13,3); polygon.vertexCount=3; polygon.vertices[0].Set(0,-2); polygon.vertices[1].Set(2,2); polygon.vertices[2].Set(-2,2); polygon.density=1; polygon.friction=0.5; polygon.restitution=0.2; body=m_world.CreateBody(bodyDef); body.CreateShape(polygon); body.SetMassFromShapes(); // custom shape bodyDef.position.Set(8,4); polygon.vertexCount=5; polygon.vertices[0].Set(0,-2); polygon.vertices[1].Set(2,0); polygon.vertices[2].Set(1,2); polygon.vertices[3].Set(-1,2); polygon.vertices[4].Set(-2,0); polygon.density=1; polygon.friction=0.5; polygon.restitution=0.2; body=m_world.CreateBody(bodyDef); body.CreateShape(polygon); body.SetMassFromShapes(); // addEventListener(Event.ENTER_FRAME, Update, false, 0, true); stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); stage.addEventListener( KeyboardEvent.KEY_UP, key_up); stage.addEventListener(MouseEvent.MOUSE_DOWN, GetBodyAtMouse); } // detecting if the player pressed SPACE public function key_down(event:KeyboardEvent):void { if (event.keyCode==32) { space_pressed=true; } } // detecting if the player released SPACE public function key_up(event:KeyboardEvent):void { if (event.keyCode==32) { space_pressed=false; } } // public function GetBodyAtMouse(e:MouseEvent):b2Body { var mult:Number=0.9; if (space_pressed) { mult=1.1; } var mouseXWorldPhys = (mouseX)/30; var mouseYWorldPhys = (mouseY)/30; mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys); var aabb:b2AABB = new b2AABB(); aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001); aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001); var k_maxCount:int=10; var shapes:Array = new Array(); var count:int=m_world.Query(aabb,shapes,k_maxCount); var body:b2Body=null; for (var i:int = 0; i < count; ++i) { var tShape:b2Shape=shapes[i] as b2Shape; var inside:Boolean=tShape.TestPoint(tShape.GetBody().GetXForm(),mousePVec); if (inside) { body=tShape.GetBody(); break; } } // if I selected a STATIC body... if (body&&! body.IsStatic()) { var s:b2Shape=body.GetShapeList(); var type:int=s.GetType(); switch (type) { case 0 : // I know it's a circle, so I am creating a b2CircleShape variable var circle:b2CircleShape=body.GetShapeList() as b2CircleShape; // getting the radius.. var r=circle.GetRadius(); // removing the circle shape from the body body.DestroyShape(circle); // creating a new circle shape var circleDef:b2CircleDef; circleDef = new b2CircleDef(); // calculating new radius circleDef.radius=r*mult; circleDef.density=1.0; circleDef.friction=0.5; circleDef.restitution=0.2; // attach the shape to the body body.CreateShape(circleDef); // determine new body mass body.SetMassFromShapes(); break; case 1 : // now I know it's a polygon var poly:b2PolygonShape=body.GetShapeList() as b2PolygonShape; // UNIVERSAL POLYGON SCALING ROUTINE THANX TO ILYA var vertex_num:int=poly.GetVertexCount(); var vertex_array:Array=poly.GetVertices(); for each (var vert:b2Vec2 in vertex_array) { vert.Multiply(mult); } body.DestroyShape(poly); var new_shape:b2PolygonDef = new b2PolygonDef(); new_shape.vertexCount=vertex_num; new_shape.vertices=vertex_array; new_shape.friction=0.5; new_shape.density=1; new_shape.restitution=0.2; body.CreateShape(new_shape); body.SetMassFromShapes(); break; } } return body; } public function Update(e:Event):void { m_world.Step(m_timeStep, m_iterations); } } } |
And this is the result:
Left click on a static body with the mouse to shrink it, left click + SPACE to expand it.
Next time… player, goal and “mass energy”.
They can be easily customized to meet the unique requirements of your project.
3 Responses
Leave a Reply
TUTORIAL SERIES:
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online


(8 votes, average: 4.13 out of 5)

niceeeeee, but i cant do AS3. It looked cool though :D
[...] the previous step I showed you how to shrink/expand any kind of [...]
that last level was hard but awesome.