Monkey: minimum Box2d with diddy example

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