Android Question libGDX library - Box2D - how to increase the velocity?

FJS

Active Member
Licensed User
Longtime User
Hello,

I would like to create a game, so I am learning about the needed libraries, I read the posts from informatix:


https://www.b4x.com/android/forum/threads/introduction-to-the-libgdx-library.32592/
https://www.b4x.com/android/forum/threads/libgdx-game-engine.32594/

Thanks to thoses examples, I work about a tiny game, but I have a problem with the velocity of the objets, because it is slow, and I would like to increase the velocity, please could you help me??

here after is the code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: xp_prueba01
    #VersionCode: 1
    #VersionName: v.01
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: true
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'Defining new structures
    Type typContact(Body1 As lgBox2DBody, Body2 As lgBox2DBody)
  

End Sub

Sub Globals
    Dim lGdx As LibGDX
    Dim GL As lgGL
    Dim Camera As lgOrthographicCamera
    Dim Renderer As lgBox2DDebugRenderer
    Dim World As lgBox2DWorld
    Dim bdyGroundL,bdyGroundR,bdyGroundD,bdyGroundU,ballbody As lgBox2DBody
    Dim lstContacts As List
    Dim lstNuke As List
  
    Dim x,y,a As Float
    Dim x0, y0, vx0, vy0 As Float
    Dim timer1 As Timer
    Dim timertime As Int
  
        Dim lGdx_IP As lgInputProcessor
        Dim lGdx_GD As lgGestureDetector
      
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Disables the accelerometer and the compass
    Dim Config As lgConfiguration
    Config.useAccelerometer = False
    Config.useCompass = False

    'Creates the libGDX surface
    lGdx.Initialize2(Config, "LG")
  
    lGdx_IP.Initialize("IP")
    lGdx_GD.Initialize("GD")
  
End Sub

Sub Activity_Resume
    'Informs libGDX of Resume events
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    'Informs libGDX of Pause events
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub LG_Create
    'Debug renderer
    Renderer.Initialize2(True, False, False, True, True, True)
  
x=Activity.Width/100
y=Activity.Height/100
Log("x=" & x)
Log("y=" & y)
    'World
    Dim vGravity As lgMathVector2
    World.Initialize(vGravity.set(0, 0), True, "Box2D")
    World.SetContinuousPhysics(True)
    World.SetWarmStarting(True)

    'Ground body left
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(1*y, -49*y), V2.set(1*y, 49*y))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1
  
    Dim bd As lgBox2DBodyDef
    bdyGroundL = World.CreateBody(bd)
    bdyGroundL.createFixture(fd)
    eShape.dispose
  
    'Ground body right
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(100*x-1*y, -49*y), V2.set(100*x-1*y, 49*y))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundR = World.CreateBody(bd)
    bdyGroundR.createFixture(fd)
    eShape.dispose
  
    'Ground body up
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(1*y, 49*y), V2.set(100*x-1*y, 49*y))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundU = World.CreateBody(bd)
    bdyGroundU.createFixture(fd)
    eShape.dispose
  
    'Ground body down
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(1*y, -49*y), V2.set(100*x-1*y, -49*y))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundD = World.CreateBody(bd)
    bdyGroundD.createFixture(fd)
    eShape.dispose

    'Starting area where the bodies appear
    Dim xLo As Float = 30*x
    Dim xHi As Float = 60*x
    Dim yLo As Float = -20*y
    Dim yHi As Float = 20*y

  
    Dim MU As lgMathUtils
  
    'Small circle
    Dim circle As lgBox2DCircleShape
    circle.Radius = 3*y

    Dim circleShapeDef As lgBox2DFixtureDef
    circleShapeDef.shape = circle
    circleShapeDef.restitution=1
    circleShapeDef.Density = 10

    Dim circleBodyDef As lgBox2DBodyDef
    circleBodyDef.Type = World.BODYTYPE_Dynamic
    circleBodyDef.position.set(MU.RandomFloat2(xLo, xHi), MU.RandomFloat2(yLo, yHi))


    Dim body5 As lgBox2DBody = World.createBody(circleBodyDef)
    body5.setLinearVelocity2(2000,2000)
    body5.createFixture(circleShapeDef)

    'Large circle
    circle.Radius = circle.Radius * 2
    circleBodyDef.position.set(MU.RandomFloat2(xLo, xHi), MU.RandomFloat2(yLo, yHi))

    Dim body6 As lgBox2DBody = World.createBody(circleBodyDef)
    body6.setLinearVelocity2(-2000,2000)
    body6.createFixture(circleShapeDef)
  
    circle.dispose

    'List of contacts
    lstContacts.Initialize
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport and position
    Camera.Initialize2(Width , Height)
    Camera.Position.set(Width*0.5, 0 , 0)
End Sub

Sub LG_Render
    'Clears the screen
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)

    'Updates the matrices of the camera
    Camera.Update

    'Updates and draws the simulation
    LG_Update
    Renderer.render(World.InternalObject, Camera.Combined)
End Sub

Sub LG_Update
    World.Step(1/60, 8, 3)

    'Some bodies are going to be destroyed according to the contact list.
    'The bodies to destroy have to be buffered because they may be involved in multiple contacts.
    lstNuke.Initialize

    'Traverses the contact results and destroys bodies that are touching heavier bodies
    For i = 0 To lstContacts.size - 1
        Dim C As typContact = lstContacts.Get(i)
        If C.Body2.Mass > C.Body1.Mass Then
            If Not(List_Contains(C.Body1)) Then lstNuke.Add(C.Body1)
        Else
            If Not(List_Contains(C.Body2)) Then lstNuke.Add(C.Body2)
        End If
    Next

    'Destroys the bodies
    'For i = 0 To lstNuke.Size - 1
        'Dim b As lgBox2DBody = lstNuke.Get(i)
        'World.DestroyBody(b)
    'Next

    'Clears the list of contacts
    lstContacts.Clear
End Sub

Sub List_Contains(Body As lgBox2DBody) As Boolean
    For Each b As lgBox2DBody In lstNuke
        If b = Body Then Return True
    Next
    Return False
End Sub

Sub Box2D_BeginContact(Contact As lgBox2DContact)
    'The ground is discarded
    'If Contact.FixtureA.Body <> (bdyGroundL) And Contact.FixtureB.Body <> (bdyGroundL)Then
    '    'The Contact instance is going to be reused, so its data have to be copied
    '    Dim thisContact As typContact
    '    thisContact.Body1 = Contact.FixtureA.Body
    '    thisContact.Body2 = Contact.FixtureB.Body
    '    lstContacts.Add(thisContact)
    'End If
  
    'If Contact.FixtureA.Body <> (bdyGroundR) And Contact.FixtureB.Body <> (bdyGroundR)Then
        'The Contact instance is going to be reused, so its data have to be copied
    '    Dim thisContact As typContact
    '    thisContact.Body1 = Contact.FixtureA.Body
    '    thisContact.Body2 = Contact.FixtureB.Body
    '    lstContacts.Add(thisContact)
    'End If
    'If Contact.FixtureA.Body <> (bdyGroundU) And Contact.FixtureB.Body <> (bdyGroundU)Then
    '    'The Contact instance is going to be reused, so its data have to be copied
    '    Dim thisContact As typContact
    '    thisContact.Body1 = Contact.FixtureA.Body
    '    thisContact.Body2 = Contact.FixtureB.Body
    '    lstContacts.Add(thisContact)
    'End If
  
    'If Contact.FixtureA.Body = (bdyGroundU)Then
        'Contact.FixtureB.Body.applyAngularImpulse(100,True)

    '    Dim V1, V2 As lgMathVector2

    '    Contact.FixtureA.Body.applyForce(V1.Set(-100,-100),Contact.FixtureA.Body.LocalCenter,True)
    'End If
    'If Contact.FixtureB.Body = (bdyGroundU)Then
    '    Contact.FixtureB.Body.applyAngularImpulse(100,True)
    'End If
    'If Contact.FixtureA.Body <> (bdyGroundD) And Contact.FixtureB.Body <> (bdyGroundD)Then
    '    'The Contact instance is going to be reused, so its data have to be copied
    '    Dim thisContact As typContact
    '    thisContact.Body1 = Contact.FixtureA.Body
    '    thisContact.Body2 = Contact.FixtureB.Body
    '    lstContacts.Add(thisContact)
    'End If
End Sub

Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    'Disposes all resources
    Renderer.dispose
    World.dispose
End Sub

' all the touch controls
Sub IP_KeyDown(KeyCode As Int) As Boolean
    'Log("ip_keydown, " & KeyCode)
   Return False
End Sub

Sub IP_KeyUp(KeyCode As Int) As Boolean
    '    Log("ip_keyup, " & KeyCode)
   Return False
End Sub

Sub IP_KeyTyped(Character As Char) As Boolean
    '    Log("IP_KeyTyped, " & Character)
   Return False
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchDown, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchDragged, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub IP_TouchUp(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchUp, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub GD_TouchDown(xx As Float, yy As Float, Pointer As Int) As Boolean
        Log("GD_TouchDown, " & xx & " , " & yy & " , " & Pointer )
        x0=xx
        y0=-yy+50*y
   Return False
End Sub

Sub GD_Fling(VelocityX As Float, VelocityY As Float) As Boolean
        Log("GD_Fling, " & VelocityX  & " , " & VelocityY)
      
        'creating a circule
            Dim circle As lgBox2DCircleShape
            circle.Radius = Rnd(1*y ,5*y)

            Dim circleShapeDef As lgBox2DFixtureDef
            circleShapeDef.shape = circle
            circleShapeDef.restitution=1
            circleShapeDef.Density = 0.1

            Dim circleBodyDef As lgBox2DBodyDef
            circleBodyDef.Type = World.BODYTYPE_Dynamic
            circleBodyDef.position.set(x0, y0)

            ballbody = World.createBody(circleBodyDef)
            ballbody.createFixture(circleShapeDef)
          

            'body.setLinearVelocity2(2000,2000)
            vx0=VelocityX
            vy0=-VelocityY
          
            'ballbody.applyLinearImpulse2(vx0,vy0,x0,y0,True)
            Dim k As Float
            k=1000.0
            'ballbody.setLinearVelocity2(vx0*k,vy0*k)
                Dim V1 As lgMathVector2
            ballbody.LinearVelocity=V1.set(vx0*k,vy0*k)
            Log("vx0*k= " & (vx0*k) & " , vy0*k= " & (vy0*k))
            timertime=10
            timer1.Initialize("Timer1",timertime)
            timer1.Enabled=True

      

   Return False
End Sub

Sub timer1_tick
    timertime=timertime+10
    If timertime>700 Then
       timer1.Enabled=False  
    End If
    Dim k As Float
    k=100.0
    ballbody.applyForceToCenter2(vx0*k,vy0*k,True)
    'ballbody.setLinearVelocity2(vx0*k,vy0*k)
  
End Sub
Sub GD_LongPress(xx As Float, yy As Float) As Boolean
        Log("GD_LongPress, " & xx & " , " & yy)

   Return False
End Sub

Sub GD_Pan(xx As Float, yy As Float, DeltaX As Float, DeltaY As Float) As Boolean
        'Log("GD_Pan, " & xx & " , " & yy & " , " & DeltaX & " , " & DeltaY)
   Return False
End Sub

Sub GD_Pinch(InitialPointer1 As lgMathVector2, InitialPointer2 As lgMathVector2, Pointer1 As lgMathVector2, Pointer2 As lgMathVector2) As Boolean
        Log("GD_Pinch, " & InitialPointer1 & " , " & InitialPointer2 & " , " & Pointer1 & " , " & Pointer2)
   Return False
End Sub

Sub GD_Tap(xx As Float, yy As Float, Count As Int) As Boolean
        Log("GD_Tap, " & xx & " , " & yy)
   Return False
End Sub

Sub GD_Zoom(InitialDistance As Float, Distance As Float) As Boolean
        Log("GD_Zoom, " & InitialDistance & " , " & Distance)
    Return False
End Sub
 

Informatix

Expert
Licensed User
Longtime User
Thank you for the link, however the problem is same, I put the velocity higher but the result is the same, the velocity of the objet in the screem does not change... it seems there is a limit...
Yes, it's always limited by the linear damping but there's none here so I don't know why it's limited. When I use Box2D, I set myself the velocity or I apply a force before each World.Step() so I have no experience of this problem.

Note that you should avoid timers with libGDX. Create a duration variable instead and add lgdx.Graphics.DeltaTime to this variable in the Render event to store the elapsed time. Then trigger your actions according to the value. It's far more accurate and run in the same thread.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The problem is due to the unit used in your program to position objects and set velocities. You use pixels while the Box2D engine is designed to handle meters. So 2000 in your case means 2000 m/s which is too big for the engine. You have to use meters and not pixels (moreover the simulation will be more realistic as all settings of the engine are based on this unit). To avoid computations, set your camera viewport to the maximum size in meters needed by your scene and use only the meter unit everywhere.
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
The problem is due to the unit used in your program to position objects and set velocities. You use pixels while the Box2D engine is designed to handle meters. So 2000 in your case means 2000 m/s which is too big for the engine. You have to use meters and not pixels (moreover the simulation will be more realistic as all settings of the engine are based on this unit). To avoid computations, set your camera viewport to the maximum size in meters needed by your scene and use only the meter unit everywhere.

ok I untherstand how to use the units, for example 2m/s will be enough for the app. However I dont know how set the Width and Height in m, could you give me any clue, please?

If I would like to have 10m of width and proportionally the height, will it be like this (#SupportedOrientations: landscape)?

B4X:
Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport and position
    Dim a0, b0 As Float
    b0=10.0 'metres
    a0=Height*b0/Width
    Camera.Initialize2(a0 , b0)
    Camera.Position.set(a0/2.0, b0/2.0 , 0)
End Sub
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
ok I untherstand how to use the units, for example 2m/s will be enough for the app. However I dont know how set the Width and Height in m, could you give me any clue, please?

If I would like to have 10m of width and proportionally the height, will it be like this (#SupportedOrientations: landscape)?

B4X:
Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport and position
    Dim a0, b0 As Float
    b0=10.0 'metres
    a0=Height*b0/Width
    Camera.Initialize2(a0 , b0)
    Camera.Position.set(a0/2.0, b0/2.0 , 0)
End Sub
Yes it's exactly that. You set the maximum width in meters, compute the height with the screen ratio, and everything else now can be expressed in meters: positions, sizes, velocities (m/s)...
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Yes it's exactly that. You set the maximum width in meters, compute the height with the screen ratio, and everything else now can be expressed in meters: positions, sizes, velocities (m/s)...


Thank you for your help and the clue, finally the speed is high and the movements are fluence, please find here after the final code, I hope it will be a good example for the forum :)

B4X:
#Region  Project Attributes
    #ApplicationLabel: xp_prueba01
    #VersionCode: 1
    #VersionName: v.01
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: true
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'Defining new structures
    Type typContact(Body1 As lgBox2DBody, Body2 As lgBox2DBody)
   

End Sub

Sub Globals
    Dim lGdx As LibGDX
    Dim GL As lgGL
    Dim Camera As lgOrthographicCamera
    Dim Renderer As lgBox2DDebugRenderer
    Dim World As lgBox2DWorld
    Dim bdyGroundL,bdyGroundR,bdyGroundD,bdyGroundU,ballbody As lgBox2DBody
    Dim lstContacts As List
    Dim lstNuke As List
   
    Dim x,y,a As Float
    Dim x0, y0, vx0, vy0 As Float
   
    Dim a0, b0 As Float
   
    Dim timer1 As Timer
    Dim timertime As Int
   
        Dim lGdx_IP As lgInputProcessor
        Dim lGdx_GD As lgGestureDetector
       
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Disables the accelerometer and the compass
    Dim Config As lgConfiguration
    Config.useAccelerometer = False
    Config.useCompass = False

    'Creates the libGDX surface
    lGdx.Initialize2(Config, "LG")
   
    lGdx_IP.Initialize("IP")
    lGdx_GD.Initialize("GD")
   
End Sub

Sub Activity_Resume
    'Informs libGDX of Resume events
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    'Informs libGDX of Pause events
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub LG_Create
    'Debug renderer
    Renderer.Initialize2(True, False, False, True, False, True)
   
    'World
    Dim vGravity As lgMathVector2
    World.Initialize(vGravity.set(0, 0), True, "Box2D")
    World.SetContinuousPhysics(True)
    World.SetWarmStarting(True)


    b0=10.0 'metres, is the total length of width
    a0=Activity.Width*b0/Activity.Height

    'Ground body left
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(-a0*0.5+0.1, -b0*0.5), V2.set(-a0*0.5+0.1, b0*0.5))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1
   
    Dim bd As lgBox2DBodyDef
    bdyGroundL = World.CreateBody(bd)
    bdyGroundL.createFixture(fd)
    eShape.dispose
   
    'Ground body right
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(a0*0.5-0.1, -b0*0.5), V2.set(a0*0.5-0.1, b0*0.5))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundR = World.CreateBody(bd)
    bdyGroundR.createFixture(fd)
    eShape.dispose
   
    'Ground body up
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(-a0*0.5+0.1, b0*0.5-0.1), V2.set(a0*0.5-0.1, b0*0.5-0.1))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundU = World.CreateBody(bd)
    bdyGroundU.createFixture(fd)
    eShape.dispose
   
    'Ground body down
    Dim eShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    eShape.set(V1.set(-a0*0.5+0.1, -b0*0.5+0.1), V2.set(a0*0.5-0.1, -b0*0.5+0.1))

    Dim fd As lgBox2DFixtureDef
    fd.shape = eShape
    fd.restitution=1

    Dim bd As lgBox2DBodyDef
    bdyGroundD = World.CreateBody(bd)
    bdyGroundD.createFixture(fd)
    eShape.dispose

    'Starting area where the bodies appear
    Dim xLo As Float = -a0*0.25
    Dim xHi As Float = a0*0.25
    Dim yLo As Float = -b0*0.25
    Dim yHi As Float = b0*0.25

   
    Dim MU As lgMathUtils
   
    'Small circle
    Dim circle As lgBox2DCircleShape
    circle.Radius = 0.1

    Dim circleShapeDef As lgBox2DFixtureDef
    circleShapeDef.shape = circle
    circleShapeDef.restitution=1
    circleShapeDef.Density = 1

    Dim circleBodyDef As lgBox2DBodyDef
    circleBodyDef.Type = World.BODYTYPE_Dynamic
    circleBodyDef.position.set(MU.RandomFloat2(xLo, xHi), MU.RandomFloat2(yLo, yHi))


    Dim body5 As lgBox2DBody = World.createBody(circleBodyDef)
    body5.setLinearVelocity2(3,-4)
    body5.createFixture(circleShapeDef)

    'Large circle
    circle.Radius = circle.Radius * 5.0
    circleBodyDef.position.set(MU.RandomFloat2(xLo, xHi), MU.RandomFloat2(yLo, yHi))

    Dim body6 As lgBox2DBody = World.createBody(circleBodyDef)
    body6.setLinearVelocity2(4,-3)
    body6.createFixture(circleShapeDef)
   
    circle.dispose

    'List of contacts
    lstContacts.Initialize
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport and position

    Camera.Initialize2(a0 , b0)
    Camera.Position.set(0, 0, 0)
    Log(a0)
    Log(b0)
   
End Sub

Sub LG_Render
    'Clears the screen
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)

    'Updates the matrices of the camera
    Camera.Update

    'Updates and draws the simulation
    LG_Update
    Renderer.render(World.InternalObject, Camera.Combined)
End Sub

Sub LG_Update
    World.Step(1/(50), 8, 3)

    'Some bodies are going to be destroyed according to the contact list.
    'The bodies to destroy have to be buffered because they may be involved in multiple contacts.
    lstNuke.Initialize

    'Traverses the contact results and destroys bodies that are touching heavier bodies
    'For i = 0 To lstContacts.size - 1
    '    Dim C As typContact = lstContacts.Get(i)
    '    If C.Body2.Mass > C.Body1.Mass Then
    '        If Not(List_Contains(C.Body1)) Then lstNuke.Add(C.Body1)
    '    Else
    '        If Not(List_Contains(C.Body2)) Then lstNuke.Add(C.Body2)
    '    End If
    'Next

    'Destroys the bodies
    'For i = 0 To lstNuke.Size - 1
        'Dim b As lgBox2DBody = lstNuke.Get(i)
        'World.DestroyBody(b)
    'Next

    'Clears the list of contacts
    lstContacts.Clear
End Sub

Sub List_Contains(Body As lgBox2DBody) As Boolean
    For Each b As lgBox2DBody In lstNuke
        If b = Body Then Return True
    Next
    Return False
End Sub

Sub Box2D_BeginContact(Contact As lgBox2DContact)
    'The ground is discarded
    'If Contact.FixtureA.Body <> (bdyGroundL) And Contact.FixtureB.Body <> (bdyGroundL)Then
    '    'The Contact instance is going to be reused, so its data have to be copied
    '    Dim thisContact As typContact
    '    thisContact.Body1 = Contact.FixtureA.Body
    '    thisContact.Body2 = Contact.FixtureB.Body
    '    lstContacts.Add(thisContact)
    'End If

End Sub

Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    'Disposes all resources
    Renderer.dispose
    World.dispose
End Sub

' all the touch controls
Sub IP_KeyDown(KeyCode As Int) As Boolean
    'Log("ip_keydown, " & KeyCode)
   Return False
End Sub

Sub IP_KeyUp(KeyCode As Int) As Boolean
    '    Log("ip_keyup, " & KeyCode)
   Return False
End Sub

Sub IP_KeyTyped(Character As Char) As Boolean
    '    Log("IP_KeyTyped, " & Character)
   Return False
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchDown, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchDragged, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub IP_TouchUp(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    '    Log("IP_TouchUp, " & ScreenX & " , " & ScreenY & " , " & Pointer)
   Return False
End Sub

Sub GD_TouchDown(xx As Float, yy As Float, Pointer As Int) As Boolean
        Log("GD_TouchDown, " & xx & " , " & yy & " , " & Pointer )
        x0=xx*a0/Activity.Width-a0*0.5
        y0=-yy*b0/Activity.Height+b0*0.5
        Log("x0= " & x0 & " , y0= " & y0 )
   Return False
End Sub

Sub GD_Fling(VelocityX As Float, VelocityY As Float) As Boolean
        Log("GD_Fling, " & VelocityX  & " , " & VelocityY)
       
        'creating a circule
            Dim circle As lgBox2DCircleShape
            circle.Radius = Rnd(1 ,6)*0.05


            Dim circleShapeDef As lgBox2DFixtureDef
            circleShapeDef.shape = circle
            circleShapeDef.restitution=1.0
            circleShapeDef.Density = 0.01

            Dim circleBodyDef As lgBox2DBodyDef
            circleBodyDef.Type = World.BODYTYPE_Dynamic
            circleBodyDef.position.set(x0, y0)
            Log("x0= " & x0 & " , y0= " & y0 )
            ballbody = World.createBody(circleBodyDef)
            ballbody.createFixture(circleShapeDef)
           

        '    'body.setLinearVelocity2(2000,2000)
            vx0=VelocityX/Activity.Width
            vy0=-VelocityY/Activity.Height
           
            ballbody.applyLinearImpulse2(vx0,vy0,x0,y0,True)
            Dim k As Float
            k=6.0
            'ballbody.setLinearVelocity2(vx0*k,vy0*k)
                Dim V1 As lgMathVector2
            ballbody.LinearVelocity=V1.set(vx0*k,vy0*k)
            Log("vx0*k= " & (vx0*k) & " , vy0*k= " & (vy0*k))
            'timertime=10
            'timer1.Initialize("Timer1",timertime)
            'timer1.Enabled=True

       

   Return False
End Sub

Sub timer1_tick
    timertime=timertime+10
    If timertime>100 Then
       timer1.Enabled=False   
    End If
    Dim k As Float
    k=0.01
    ballbody.applyForceToCenter2(vx0*k,vy0*k,True)
    'ballbody.setLinearVelocity2(vx0*k,vy0*k)
   
End Sub
Sub GD_LongPress(xx As Float, yy As Float) As Boolean
        Log("GD_LongPress, " & xx & " , " & yy)

   Return False
End Sub

Sub GD_Pan(xx As Float, yy As Float, DeltaX As Float, DeltaY As Float) As Boolean
        'Log("GD_Pan, " & xx & " , " & yy & " , " & DeltaX & " , " & DeltaY)
   Return False
End Sub

Sub GD_Pinch(InitialPointer1 As lgMathVector2, InitialPointer2 As lgMathVector2, Pointer1 As lgMathVector2, Pointer2 As lgMathVector2) As Boolean
        Log("GD_Pinch, " & InitialPointer1 & " , " & InitialPointer2 & " , " & Pointer1 & " , " & Pointer2)
   Return False
End Sub

Sub GD_Tap(xx As Float, yy As Float, Count As Int) As Boolean
        Log("GD_Tap, " & xx & " , " & yy)
   Return False
End Sub

Sub GD_Zoom(InitialDistance As Float, Distance As Float) As Boolean
        Log("GD_Zoom, " & InitialDistance & " , " & Distance)
    Return False
End Sub
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Hello,

Please, could you tell me how to know which are the balls in contact?, I dont know how to know the real contacts in each moment, I would like to destroy the smaller balls when two balls with diferent size are in contact

Thank you very much in advance
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hello,

Please, could you tell me how to know which are the balls in contact?, I dont know how to know the real contacts in each moment, I would like to destroy the smaller balls when two balls with diferent size are in contact

Thank you very much in advance
Contacts are solved when you call World.Step. A BeginContact event is raised for each new contact. Look at the CollisionProcessing demo.
To identify a Box2D object without ambiguity, you can use the UserData property of its Body to store an ID or any other data allowing you to identify it.
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Sorry, but I tried to do it without lucky...

The event:
Box2D_BeginContact(Contact As lgBox2DContact), detects the contacts between the ballbody and the laterals, however not between
ballbody. While the program is runing, the contacts between all bodies are perfectly right, but this event gives "null" (except ballbody and the laterals).

I would like to know which bodies are in contact, and the propieties of this two bodies, but I dont know how to do it. Please, could you give us an example (with my code), to know this information??

Thank you

Best regards
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Sorry, but I tried to do it without lucky...

The event:
Box2D_BeginContact(Contact As lgBox2DContact), detects the contacts between the ballbody and the laterals, however not between
ballbody. While the program is runing, the contacts between all bodies are perfectly right, but this event gives "null" (except ballbody and the laterals).

I would like to know which bodies are in contact, and the propieties of this two bodies, but I dont know how to do it. Please, could you give us an example (with my code), to know this information??

Thank you

Best regards
Your code works fine. There's nothing to change. If you log the contact data, you'll see that the balls collision is detected. Give a name to each body by setting its UserData and you'll be able to say which body in the contact event is the small ball (or make your variables body5 and body6 global and compare the bodies of the two fixtures in contact to these variables). Here's an example:
 

Attachments

  • Box2D_Test.zip
    3.4 KB · Views: 240
Upvote 0
Top