Android Question LibGDX- Camera, zoom in and out, scroll function

jtare

Active Member
Licensed User
Longtime User
Hi, I started to use the LibGDX library a few days ago and I didn´t find in the examples how to zoom in and out the camera and how to scroll(up,down,left to right), Im working in the ShapeRender example, this one create basic objects(circles, squares) and you can rotate them, I remove the rotate function and tried to add some type of scroll function but it doesn´t work all the time, when the camera is really far away from the (0,0,0) (big objects) the scroll is really slow and when the camera is near the (0,0,0) the scroll is very fast. Im using this code:
B4X:
Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False
    LastX = ScreenX
    LastY = ScreenY
    Return True
End Sub

Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean

    Point.Set(Camera.Position.X, Camera.Position.Y, Camera.Position.Z)
    Camera.Position.Set(Point.X, Point.y, Point.Z)

    Dim speedx, speedy As Double
    speedx = Abs(ScreenX-LastX)/100
    speedy = Abs(ScreenY-LastY)/100
    If Pointer <> 0 Then Return False

    If ScreenX > LastX Then
       
        Camera.Translate(-speedx, 0, 0)

    Else
        Camera.Translate(speedx, 0, 0)

    End If
   
    If ScreenY > LastY Then
        Camera.Translate(0,speedy,0)
    Else
        Camera.Translate(0,-speedy,0)
   
    End If
   
    Camera.Update

    LastX = ScreenX
    LastY = ScreenY
    Return True  
End Sub

And for the multitouch function (zoom in and out) I could not come up with something.
My question is how to enable some type of function in which I can zoom in and out and scroll up-down,left-right. Just like any photo in a touchscreen display.
Thanks
 

jtare

Active Member
Licensed User
Longtime User
Thanks, I looked at the example and got it working, now I can zoom in, out and scroll,
I changed my camera from lgPerspectiveCamera to lgOrthographicCamera and added this code:
B4X:
Sub GD_Pan(X As Float, Y As Float, DeltaX As Float, DeltaY As Float) As Boolean
    'Moves the camera
    Camera.Translate(-DeltaX * Camera.Zoom, DeltaY * Camera.Zoom)
    Return True
End Sub

Sub GD_Zoom(InitialDistance As Float, Distance As Float) As Boolean
    'Gets the initial zoom of the camera if new pair of pointers
    If InitialDistance <> InitialDist Then
        InitialZoom = Camera.Zoom
        InitialDist = InitialDistance
    End If

    'Zooms in/out
    Camera.Zoom = InitialZoom * (InitialDistance / Distance)
    Return True   
End Sub

by default the camera zoom is
B4X:
camera.zoom = 1
what means that camera.zoom = 1?
When I was using perspective camera my objects fit the screen very well but now with camera.zoom = 1 the objects are very tiny, if I set the camera.zoom = 0.013 I can make the objects fit in the screen. How is this zoom measured? If it's necessary I can upload an example.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks, I looked at the example and got it working, now I can zoom in, out and scroll,
I changed my camera from lgPerspectiveCamera to lgOrthographicCamera and added this code:
B4X:
Sub GD_Pan(X As Float, Y As Float, DeltaX As Float, DeltaY As Float) As Boolean
    'Moves the camera
    Camera.Translate(-DeltaX * Camera.Zoom, DeltaY * Camera.Zoom)
    Return True
End Sub

Sub GD_Zoom(InitialDistance As Float, Distance As Float) As Boolean
    'Gets the initial zoom of the camera if new pair of pointers
    If InitialDistance <> InitialDist Then
        InitialZoom = Camera.Zoom
        InitialDist = InitialDistance
    End If

    'Zooms in/out
    Camera.Zoom = InitialZoom * (InitialDistance / Distance)
    Return True  
End Sub

by default the camera zoom is
B4X:
camera.zoom = 1
what means that camera.zoom = 1?
When I was using perspective camera my objects fit the screen very well but now with camera.zoom = 1 the objects are very tiny, if I set the camera.zoom = 0.013 I can make the objects fit in the screen. How is this zoom measured? If it's necessary I can upload an example.
The camera acts like the camera of your phone. So Camera.zoom = 1 means no zoom (zoom factor = 1).
If your objects are too tiny, then resize them or change the viewport size of the camera. Use only the zoom if the user wants to zoom in/out.
 
Upvote 0
Top