Drawing and applying force in box2d

Monkey Forums/Monkey Programming/Drawing and applying force in box2d

slenkar(Posted April) [#1]
I tried to draw box2d rects by myself and applyforces but neither seem to really work:

 Import mojo
 Import box2d
 Import box2d.dynamics
Import box2d.collision
Import box2d.collision.shapes
Import box2d.dynamics.joints
Import box2d.dynamics.contacts
Import box2d.common
Import box2d.common.math
 Function Main:Int()
 New myapp
 End Function
 
 
 
 Class block
 Field myBody:b2Body
 Field track:int
 End Class
 
 Class myapp Extends App
 Field m_world:b2World
 Field width:Int = 640
    Field height:Int = 480
       Const dwidth := 6.0
    Const dheight := 30.0
    Field ddensity:Float
    Const dfriction:Float = 0.6
        Field m_physScale:Float = 30
            Field m_velocityIterations:Int = 10
    Field m_positionIterations:Int = 10
    Const physicsRate:Int = 60
    Field m_timeStep:Float = 1.0/physicsRate
  	Field onepixel:Image
  		Method OnCreate:Int()
  		onepixel=LoadImage("onepixel.png")
  		onepixel.SetHandle(0.5,0.5)
  		Local worldAABB :b2AABB = New b2AABB()
        worldAABB.lowerBound.Set(-1000.0, -1000.0)
        worldAABB.upperBound.Set(1000.0, 1000.0)
        '// Define the gravity vector
        Local gravity :b2Vec2 = New b2Vec2(0.0, 10.0)
        '// Allow bodies to sleep
        Local doSleep :Bool = True
        '// Construct a world object
        m_world = New b2World(gravity, doSleep)
        m_world.SetWarmStarting(True)
        
         Local wall :b2PolygonShape= New b2PolygonShape()
        Local wallBd :b2BodyDef = New b2BodyDef()
        Local wallB :b2Body
        
                 wallBd.position.Set( 0, 480)
        wall.SetAsBox(600, 10)
        wallB = m_world.CreateBody(wallBd)
        wallB.CreateFixture2(wall)
        #rem
         wallBd.position.Set( -95 / m_physScale, height / m_physScale / 2)
        wall.SetAsBox(100/m_physScale, height+100/m_physScale/2)
        wallB = m_world.CreateBody(wallBd)
        wallB.CreateFixture2(wall)
        '// Right
        wallBd.position.Set((width+95) / m_physScale, height / m_physScale / 2)
        wallB = m_world.CreateBody(wallBd)
        wallB.CreateFixture2(wall)
        '// Top
        wallBd.position.Set(width / m_physScale / 2, -95 / m_physScale)
        wall.SetAsBox(width+100/m_physScale/2, 100/m_physScale)
        wallB = m_world.CreateBody(wallBd)
        wallB.CreateFixture2(wall)
        '// Bottom
        wall.SetAsBox(width+100/m_physScale/2, 100/m_physScale)
        wallBd.position.Set(width / m_physScale / 2, (height + 95) / m_physScale)
        wallB = m_world.CreateBody(wallBd)
        wallB.CreateFixture2(wall)
        
        Local x:Float=200
        Local y:Float=200
        Local horizontal:Bool=true
        	Local sd:b2PolygonShape = New b2PolygonShape()
        Local fd :b2FixtureDef = New b2FixtureDef()
        bd  = New b2BodyDef()
        bd.type = b2Body.b2_Body
		sd.SetAsBox(0.5*dwidth/m_physScale, 0.5*dheight/m_physScale)
        fd.density = ddensity
        fd.friction = dfriction
        fd.restitution = 0.2
        fd.shape = sd
		bd.position.Set( x/m_physScale, y/m_physScale)
   		If horizontal
			bd.angle = Constants.PI*0.5
		Else
			bd.angle = 0.0
		End
        myBody= m_world.CreateBody(bd)
        myBody.CreateFixture(fd)
        #end
        
        Local x:Float=200
        Local y:Float=200
        Local horizontal:Bool=True
        
        	Local sd:b2PolygonShape = New b2PolygonShape()
        Local fd :b2FixtureDef = New b2FixtureDef()
        bd  = New b2BodyDef()
        bd.type = b2Body.b2_Body
		sd.SetAsBox(50, 10)
        fd.density = ddensity
        fd.friction = dfriction
        fd.restitution = 0.2
        fd.shape = sd
		
		blocks=New List<block>
        
		For Local iter:Int=0 To 10
		bd.position.Set( (iter*60)+x, y)
   		If horizontal
			bd.angle = Constants.PI*0.5
		Else
			bd.angle = 0.0
		End
        myBody= m_world.CreateBody(bd)
        myBody.CreateFixture(fd)
        Local bl:block=New block
         If iter=0
		bl.track=True
		Endif
        bl.myBody=myBody
        blocks.AddLast(bl)
        Next
        
        For Local iter:Int=0 To 10
		bd.position.Set( (iter*100), y-80)
		
   		horizontal=False
   		If horizontal
			bd.angle = Constants.PI*0.5
		Else
			bd.angle = 0.0
		End
        myBody= m_world.CreateBody(bd)
        myBody.CreateFixture(fd)
        Local bl:block=New block
       
        bl.myBody=myBody
        blocks.AddLast(bl)
        Next
        PiHelper=180/Constants.PI
        End Method
        Field blocks:List<block>
        Field bd:b2BodyDef
        Field myBody:b2Body
        Field PiHelper:Float
        Method OnRender:Int()
        Cls()
       'Print "x "+myBody.GetPosition.x
        
        'Print "y "+myBody.GetPosition.y
       
        'DrawText "dom",myBody.GetPosition.x,myBody.GetPosition.y
        For Local b:block=Eachin blocks
      
        PushMatrix()
        Translate b.myBody.GetPosition.x,b.myBody.GetPosition.y
        Local rot:Float=(b.myBody.GetAngle()*PiHelper)
        Rotate rot
       	Scale 50,20
        DrawImage onepixel,0,0
        PopMatrix()
        Next
        End Method
        
        Method OnUpdate:Int()
        If KeyHit(KEY_LMB)
        Print "LMB"
        For Local b:block=Eachin blocks
         b.myBody.ApplyTorque(5000);
        next
        Endif
         m_world.TimeStep(m_timeStep, m_velocityIterations, m_positionIterations)
        'm_world.ClearForces()
        End Method
  End Class

I created a one pixel image and scaled it for each rect,


muddy_shoes(Posted April) [#2]
You're setting the blocks to be 100 by 20 and scaling the render to make them 50 by 20.
You probably need to matrix rotate in the opposite direction to the body angle (i.e. Rotate -rot).
You should give dynamic objects some density or you'll get odd behaviour.
You'll probably find that your torque value is too small to do anything because you're creating objects that are huge in Box2D world units.


slenkar(Posted April) [#3]
you fixed all of the issues, perfect, thanks :)