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 |
'----Includes----' Import box2d.dynamics.b2world '----Test Zone----' Function Main() New Box2DLoop() End Function '----Main Loop----' Class Box2DLoop Extends App Field _world:b2World Field RATIO:Float = 8 Field _nextCrateIn:Int '--Main Methods----' Method OnCreate() ' 1. Set Up World setupWorld() ' Create Walls and Floors createWallsAndFloor() setupDebugDraw() _nextCrateIn = 0 'Display Setup SetUpdateRate(60) End Method Method setupDebugDraw:Void() 'Box2D Debug Settings 'Delete this section if you dont need to see the physical process in graphics. Local dbgDraw :b2DebugDraw = New b2DebugDraw() dbgDraw.SetDrawScale(10.0) dbgDraw.SetFillAlpha(0.3) dbgDraw.SetLineThickness(1.0) dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit)'| b2DebugDraw.e_pairBit) _world.SetDebugDraw(dbgDraw) End Method OnRender() Cls _world.DrawDebugData() End Method Method OnUpdate() _world.TimeStep(1.0 /30,10,10) _world.ClearForces() _nextCrateIn = _nextCrateIn - 1 If _nextCrateIn <=0 And _world.m_bodyCount < 80 Then addARandomCrate() _nextCrateIn = 10 Endif End Method Method setupWorld() ' Define gravity Local gravity:b2Vec2 = New b2Vec2(0,9.8) ' Ignore Sleeping Objects Local ignoresleeping:Bool = True _world = New b2World(gravity,ignoresleeping) End Method addARandomCrate() Local fd:b2FixtureDef = New b2FixtureDef() Local sd:b2PolygonShape = New b2PolygonShape() Local bd:b2BodyDef = New b2BodyDef(); bd.type = b2Body.b2_Body fd.friction = 0.8 fd.restitution = 0.3 fd.density = 0.7 fd.shape = sd sd.SetAsBox(randomInt(5,40) / RATIO, randomInt(5, 40) / RATIO) bd.position.Set(randomInt(15,530) / RATIO, randomInt(-100, -10) / RATIO) bd.angle = randomInt(0,360) * 3.14 / 180 Local b:b2Body = _world.CreateBody(bd) b.CreateFixture(fd) End Method randomInt:Int(lowVal:Int, highVal:Int) If (lowVal <= highVal) Return lowVal + Floor(Rnd() * (highVal - lowVal + 1)) Endif End Method createWallsAndFloor:Void() Local sd:b2PolygonShape = New b2PolygonShape() Local fd:b2FixtureDef = New b2FixtureDef() Local bd:b2BodyDef = New b2BodyDef() bd.type = b2Body.b2_staticBody sd.SetAsArray([New b2Vec2(0,0),New b2Vec2(550/RATIO,0), New b2Vec2(550/RATIO,10/RATIO), New b2Vec2(0,10/RATIO)]) fd.friction = 0.5 fd.restitution = 0.3 fd.density = 0.0 fd.shape = sd bd.position.Set(0,560/RATIO) Local b:b2Body = _world.CreateBody(bd) b.CreateFixture(fd) Local sdwall:b2PolygonShape = New b2PolygonShape() Local fdwall:b2FixtureDef = New b2FixtureDef() Local bdwall:b2BodyDef = New b2BodyDef() bd.type = b2Body.b2_staticBody fdwall.friction = 0.5 fdwall.restitution = 0.3 fdwall.density = 0 fdwall.shape = sdwall sdwall.SetAsBox(5/RATIO,390/RATIO) bdwall.position.Set(5/RATIO,195/RATIO) Local leftwall:b2Body = _world.CreateBody(bdwall) leftwall.CreateFixture(fdwall) bdwall.position.Set(545/RATIO,195/RATIO) Local rightwall:b2Body = _world.CreateBody(bdwall) rightwall.CreateFixture(fdwall) End End Class |