Android Question Box2D world scale

Discussion in 'Android Questions' started by coldteam, Feb 25, 2018.

  1. coldteam

    coldteam Member Licensed User

    Hi,
    i try to start use Box2D and need some help:

    1. set camera like this:
    Code:
    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:
    Code:
    Dim vGravity As lgMathVector2
        World.Initialize(vGravity.set(
    0, -300), True"World")
        World.SetContinuousPhysics(
    True)
        World.SetWarmStarting(
    True)
    3. create ground:
    Code:
    Dim bd As lgBox2DBodyDef
        
    Dim ground As lgBox2DBody = World.createBody(bd)
        
    Dim edgShape As lgBox2DEdgeShape
        
    Dim V1, V2 As lgMathVector2
        edgShape.set(V1.set(-
    1900), V2.set(1900))
        ground.createFixture2(edgShape, 
    0)
        edgShape.dispose
    4. create Dynamic body
    Code:
    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(
    0100)
        CI = World.createBody(circleBodyDef)
        CI.createFixture(circleShapeDef)
        
    circle.dispose
    5. try applyLinearImpulse Y
    Code:
    CI.applyLinearImpulse2(0,100000,CI.WorldCenter.x,CI.WorldCenter.y,True)
    6. add text
    Code:
    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 ?
     

    Attached Files:

    • rrr.png
      rrr.png
      File size:
      4.4 KB
      Views:
      149
  2. coldteam

    coldteam Member Licensed User

    i try

    Code:
    scale = 50
    bitfont = FG.CreateFont(
    "font2.ttf",10*Density"0123456789.,")
    but it looks bad
     

    Attached Files:

  3. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed 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
     
    coldteam likes this.
  4. coldteam

    coldteam Member Licensed 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?
     
  5. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    camera positions and screen positions are different things.
    Tomorrow I'll post a small example.
     
    coldteam likes this.
  6. coldteam

    coldteam Member Licensed User

    nice, link me pls
     
  7. coldteam

    coldteam Member Licensed User

  8. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    Today I will check a small example that I made and put here. Please wait.
     
  9. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    Again. I am not an expert and there must be better ways of doing this.
    I hope this is helpful

    Code:
    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(TrueFalseFalseTrueTrueTrue)

        
    '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(35)
        
    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/6083)
    End Sub

    Sub LG_Dispose
     
        
    'Disposes all resources
        Renderer.dispose
        World.dispose
     
    End Sub
     
    Last edited: Mar 3, 2018
    coldteam likes this.
  10. coldteam

    coldteam Member Licensed 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: Mar 3, 2018
  11. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

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

    Code:
    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: Mar 3, 2018
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice