'----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
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.