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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
Strict Import diddy Import box2d.collision Import box2d.collision.shapes Import box2d.collision.shapes Import box2d.dynamics.joints Import box2d.common.math Import box2d.dynamics.contacts Import box2d.dynamics Import box2d.flash.flashtypes Import box2d.common.math.b2vec2 Global gameScreen:Screen Function Main:Int() game=New MyGame() Return 0 End Function Class MyGame Extends DiddyApp Method OnCreate:Int() Super.OnCreate() SetUpdateRate(60) gameScreen = New Box2dscreen gameScreen.PreStart() Return 0 End End Class Box2dscreen Extends Screen ' Field world:b2World Field contactlistener:ContactListener ' Field m_velocityIterations:Int = 10 Field m_positionIterations:Int = 10 Field m_timeStep:Float = 1.0/60 ' set this to your SetUpdateRate() Field m_physScale:Float = 1' 30 ' Not in use, but you should not use pixels as your units, an object of length 200 pixels would be seen by Box2D as the size of a 45 story building. Field m_debugdrawscale:Float=1 ' set this to m_physScale if you are scaling your world. Method New() Local doSleep:Bool = True Self.world = New b2World(New b2Vec2(0,0), doSleep) ' no need for AABB in newer versions of box2d, the world is infinite Self.SetGravity(1.0,20) ' world.SetWarmStarting(True) ' ' '// set debug draw Local dbgDraw :b2DebugDraw = New b2DebugDraw() dbgDraw.SetDrawScale(m_debugdrawscale) ' was 30 dbgDraw.SetFillAlpha(0.3) dbgDraw.SetLineThickness(1.0) dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit)'| b2DebugDraw.e_pairBit) world.SetDebugDraw(dbgDraw) Self.contactlistener=New ContactListener() world.SetContactListener(Self.contactlistener)' for collision detection, if you need information which objects collide End Method Method Start:Void() Self.CreateDemo() End Method Render:Void() Cls Self.world.DrawDebugData() End Method Update:Void() world.TimeStep(m_timeStep, m_velocityIterations, m_positionIterations) world.ClearForces() End Method Method SetGravity:Void(x:Float,y:Float) Self.world.SetGravity(New b2Vec2(x,y)) End Method Method CreateDemo:Void() Local b:b2Body ' a box for ground b=Self.CreateBox(340,420,480,30,True) b.SetUserData(New StringObject("ground")) ' give object a name for collision detection in contactlistener ' a sphere b=Self.CreateSphere(350,220,40) b.SetUserData(New StringObject("sphere")) ' create a polygon from an array of b2d Vectors Local tri:b2Vec2[]= [New b2Vec2(0, 0), New b2Vec2(0, -100), New b2Vec2(200 ,0)] b=Self.CreatePolybody(200,20,tri) b.SetUserData(New StringObject("triangle1")) ' ' create a polygon from an array with floats Local triangle:Float[]=[0.000000000,-49.000000,99.000000,59.0000000,-77.000000,79.0000000] b=Self.CreatePolybody(200,100,Self.ArrayToVectorarray(triangle)) b.SetUserData(New StringObject("triangle2")) End Method Method CreateBox:b2Body (xpos:Float,ypos:Float,width:Float,height:Float,static:Bool=True) Local fd :b2FixtureDef = New b2FixtureDef() Local sd :b2PolygonShape = New b2PolygonShape() Local bd :b2BodyDef = New b2BodyDef() If static=True bd.type = b2Body.b2_staticBody Else bd.type = b2Body.b2_Body Endif fd.density = 1.0 fd.friction = 0.5 fd.restitution = 0.1 fd.shape = sd sd.SetAsBox(width,height) bd.position.Set(xpos,ypos) Local b :b2Body b = Self.world.CreateBody(bd) ' Recall that shapes don’t know about bodies and may be used independently of the physics simulation. Therefore Box2D provides the b2Fixture class to attach shapes to bodies. b.CreateFixture(fd) ' Return b End Method Method CreateSphere:b2Body (xpos:Float,ypos:Float,radius:Float,static:Bool=False) Local fd :b2FixtureDef = New b2FixtureDef() Local bd :b2BodyDef = New b2BodyDef() Local cd :b2CircleShape = New b2CircleShape() ' cd.m_radius = radius fd.density = 2 fd.restitution = 0.2 fd.friction = 0.5 fd.shape=cd If static=True bd.type = b2Body.b2_staticBody ' a static body Else bd.type = b2Body.b2_Body 'a dynamic body Endif bd.position.Set(xpos,ypos) Local b :b2Body b = Self.world.CreateBody(bd) b=Self.world.CreateBody(bd) b.CreateFixture(fd) Return b End Method Method CreatePolybody:b2Body(xpos:Float,ypos:Float,verts:b2Vec2[],static:Bool=False,bullet:Bool=False) ' polys must be defined vertices from left to right or their collision will not work. They must be convex. ' A polygon is convex when all line segments connecting two points in the interior do not cross any edge ' of the polygon. ' Polygons are solid and never hollow. A polygon must have 3 or more vertices. Local b :b2Body Local fd :b2FixtureDef = New b2FixtureDef() Local sd :b2PolygonShape = New b2PolygonShape() Local bd :b2BodyDef = New b2BodyDef() ' sd.SetAsArray(verts,verts.Length) fd.shape=sd bd.type = b2Body.b2_Body ' bd.position.Set(xpos,ypos) bd.bullet=bullet b=Self.world.CreateBody(bd) ' fd.density = 1.0 fd.friction = 0.5 fd.restitution = 0.1 b.CreateFixture(fd) ' Return b End Method Method ArrayToVectorarray:b2Vec2[](arr:Float[]) ' converts a normal array with floats to an array of b2Vec2 Local vecs:b2Vec2[1] vecs=vecs.Resize(arr.Length/2) Local count:Int For Local f:Float=0 To arr.Length-2 Step 2 vecs[count]=New b2Vec2(arr[f],arr[f+1]) count+=1 Next Return vecs End Method Method Cleanup:Void() ' When a world leaves scope or is deleted by calling delete on a pointer, all the memory reserved for bodies, fixtures, and joints is freed. ' This is done to improve performance and make your life easier. However, you will need to nullify any body, fixture, or joint pointers you have because they will become invalid. End Method End Class Class ContactListener Extends b2ContactListener ' to gather information about collided objects. ' http://www.box2dflash.org/docs/2.1a/reference/Box2D/Dynamics/b2ContactListener.html ' You cannot create/destroy Box2D entities inside these callbacks. Method New() Super.New() End Method PreSolve : Void (contact:b2Contact, oldManifold:b2Manifold) End Method BeginContact:Void(contact:b2Contact) ' Print "objects did collide.." ' ' get the fixtures involved in collision Local fixA:b2Fixture=contact.GetFixtureA()' Instead of telling you which b2Body collided, the b2Contact tells you which fixture collided. Local fixB:b2Fixture=contact.GetFixtureB()' To know which b2Body was involved in the collision you can retrieve a reference to the fixture. Local userdata1:Object=Object(fixA.GetBody().GetUserData()) Local userdata2:Object=Object(fixB.GetBody().GetUserData()) If userdata1<>Null Local name:String=StringObject(userdata1).ToString() ' Print "involved in collision:"+ name Endif If userdata2<>Null Local name:String=StringObject(userdata2).ToString() ' Print "involved in collision:"+ name Endif End Method End |