Android Question Box2D world scale

coldteam

Active Member
Licensed User
Longtime User
Hi,
i try to start use Box2D and need some help:

1. set camera like this:
B4X:
Dim Scale As Int = 100
Sub LG_Resize(Width As Int, Height As Int)
    Camera.Initialize2( Scale * 4, Scale * 4 * Height / Width)
    Camera.Position.set(0, Scale, 0)
End Sub
2. set world:
B4X:
Dim vGravity As lgMathVector2
    World.Initialize(vGravity.set(0, -300), True, "World")
    World.SetContinuousPhysics(True)
    World.SetWarmStarting(True)

3. create ground:
B4X:
Dim bd As lgBox2DBodyDef
    Dim ground As lgBox2DBody = World.createBody(bd)
    Dim edgShape As lgBox2DEdgeShape
    Dim V1, V2 As lgMathVector2
    edgShape.set(V1.set(-190, 0), V2.set(190, 0))
    ground.createFixture2(edgShape, 0)
    edgShape.dispose

4. create Dynamic body
B4X:
Dim circle As lgBox2DCircleShape
    circle.Radius = 10
    Dim circleShapeDef As lgBox2DFixtureDef
    circleShapeDef.shape = circle
    circleShapeDef.Density = 1
    circleShapeDef.restitution = 0.2
    Dim circleBodyDef As lgBox2DBodyDef
    circleBodyDef.Type = World.BODYTYPE_Dynamic
    circleBodyDef.position.set(0, 100)
    CI = World.createBody(circleBodyDef)
    CI.createFixture(circleShapeDef)
    circle.dispose

5. try applyLinearImpulse Y
B4X:
CI.applyLinearImpulse2(0,100000,CI.WorldCenter.x,CI.WorldCenter.y,True)

6. add text
B4X:
bitfont = FG.CreateFont("font1.ttf", 10dip,"TEX")
Dim Str As String = "TEXT"
    bitfont.Draw(Batch, Str,Camera.Position.x-(bitfont.GetBounds(Str).Width/2),Camera.Position.y+(bitfont.GetBounds(Str).Height/2))

I wanted it to look the same on different screens

questions:
- what am I doing wrong?
- body is very large?(falls slowly)

if i do Scale = 10 its ok, but i cant draw bitmapfont(rly big)
- very large pulse value, is it ok ?
 

Attachments

  • rrr.png
    rrr.png
    4.4 KB · Views: 465

coldteam

Active Member
Licensed User
Longtime User
i try

B4X:
scale = 50
bitfont = FG.CreateFont("font2.ttf",10*Density, "0123456789.,")

but it looks bad
 

Attachments

  • rrr2.png
    rrr2.png
    5.7 KB · Views: 418
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
I am not an expert but I already did some things with Box2d.

You should think about Box2D as a world in meters.

You must understand that there is a difference between LibGDX and Box2d.

See tutorials on youtube about Box2D coordinate system
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
Thank you,
Yes, I understand that.
I created scale, and reduced camera view
but if the pixel world is so small
how to draw a font?
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Again. I am not an expert and there must be better ways of doing this.
I hope this is helpful

B4X:
Sub Process_Globals
End Sub

Sub Globals

    Dim lGdx As LibGDX
    Dim GL As lgGL
    Dim Camera, CameraFont As lgOrthographicCamera
    Dim World As lgBox2DWorld
    Dim Renderer As lgBox2DDebugRenderer
    Dim LG_Batch As lgSpriteBatch
    Dim Font As lgBitmapFont
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    lGdx.Initialize("LG")
End Sub

Sub LG_Create
     
    'Initializes the sprite batcher
    LG_Batch.Initialize
     
    'Font
    Font.Initialize
    Font.SetColorRGBA(1,0,0,1)
    Font.Scale(Density*2)
         
    'Debug renderer
    Renderer.Initialize2(True, False, False, True, True, True)

    'World
    Dim vGravity As lgMathVector2
    World.Initialize(vGravity.set(0, -10), True, "Box2D")
    World.SetContinuousPhysics(True)
    World.SetWarmStarting(True)

    'I create objects (floor 2.8x0.5 meters)
    'Floor
    Dim fd As lgBox2DFixtureDef
    Dim sd As lgBox2DPolygonShape
    sd.setAsBox(1.4,.25)
    fd.shape = sd
    Dim bd As lgBox2DBodyDef
    bd.position.set(1.5,.25)
    World.createBody(bd).createFixture(fd)
    sd.1dispose
    'Box (2x2 meters)
    Dim fd As lgBox2DFixtureDef
    Dim sd As lgBox2DPolygonShape
    sd.setAsBox(1,1)
    fd.shape = sd
    Dim bd As lgBox2DBodyDef
    bd.position.set(1.5,4.5)
    bd.type=World.BODYTYPE_Dynamic
    World.createBody(bd).createFixture(fd)
    sd.dispose

End Sub

Sub LG_Resize(Width As Int, Height As Int)
     'In my example I create a 3x5 camera
    Camera.Initialize2(3, 5)
    Camera.Position.set(1.5,2.5,0)

    'I create a second camera with actual size of the device. In this camera I draw the text.
    'Set CameraFont
    CameraFont.Initialize2(lGdx.Graphics.Width,lGdx.Graphics.Height)
    CameraFont.Position.set(lGdx.Graphics.Width/2,lGdx.Graphics.Height/2,0)

End Sub

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

    'Updates the matrices of the cameras
    Camera.Update
 
    'Updates and draws the simulation
    LG_Update
    Renderer.render(World.InternalObject, Camera.Combined) 
 
    'Font
    CameraFont.Update         
    LG_Batch.ProjectionMatrix = CameraFont.Combined
    LG_Batch.begin
    Font.Draw(LG_Batch,"B4A",lGdx.Graphics.Width*.25,lGdx.Graphics.Height*.75)
    LG_Batch.end
             
End Sub

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

Sub LG_Dispose
 
    'Disposes all resources
    Renderer.dispose
    World.dispose
 
End Sub
 
Last edited:
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
Hello, Thank you!
it works, but Informatix
write "A SpriteBatch is a pretty heavy object so you should only ever have one in your program."
and if i already use "
lgMapOrthogonalTiledMapRenderer"
With "Renderer.SpriteBatch"
I mean, is this normal?
 
Last edited:
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Hello, Thank you!
it works, but Informatix
write "A SpriteBatch is a pretty heavy object so you should only ever have one in your program."
and if i already use "
lgMapOrthogonalTiledMapRenderer"
With "Renderer.SpriteBatch"
I mean, is this normal?

I agree but you could have only one object SpriteBatch
Adjusted two times. One for the sprites and another for the font.

B4X:
LG_Batch.ProjectionMatrix = Camera.Combined
'Draw Sprites
....
LG_Batch.ProjectionMatrix = CameraFont.Combined
'Draw Text

Another solution is to adjust the coordinates and scale and use only one camera
 
Last edited:
Upvote 0
Top