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:
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