Android Question libGDX Please Help

abhishek007p

Active Member
Licensed User
Longtime User
hello! Its me again im not sure if anyone can help but here is my code:

B4X:
Sub MovePlayer
    If TouchPos.x <> Position.x AND TouchPos.y <> Position.y Then
       
        If TouchPos.x > Position.x Then
            'move right
            Position.x = (Position.x + 200 * LibGDX.Graphics.DeltaTime)
        Else If TouchPos.x < Position.x Then
            'move left
            Position.x = (Position.x - 200 * LibGDX.Graphics.DeltaTime)
        Else
            Position.x = TouchPos.x - bgBalloon.Width / 2
            TouchPos.x = Position.x
        End If

        If TouchPos.y > Position.y Then
            'move up
            Position.y = (Position.y + 200 * LibGDX.Graphics.DeltaTime)
        Else If TouchPos.y < Position.y Then
            'move down
            Position.y = (Position.y - 200 * LibGDX.Graphics.DeltaTime)
        Else
            Position.y = TouchPos.y - bgBalloon.Height / 2
            TouchPos.y = Position.y
        End If
       
    End If
End Sub

Sub InputProc_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
   'transform x and y location
   TouchPos.x = vpWidth * (ScreenX / LibGDX.Graphics.Width)
   TouchPos.y = vpHeight - (vpHeight * (ScreenY / LibGDX.Graphics.Height)) 'flip Y coords
   Log("TouchPos.x: " & TouchPos.x)
   Log("TouchPos.y: " & TouchPos.y)
   Return False
End Sub

You can also download the attached project file.

what im trying to do is move the balloon to where the screen was last touched.

Can anyone help me on how will i fix it? the only problem is the logic on how it will move smoothly.

@Erel , @Informatix, @anyone who knows libgdx
 

Attachments

  • libgdx.zip
    476.3 KB · Views: 202

eps

Expert
Licensed User
Longtime User
Do you mean that you want the balloon to move in a straight line as opposed to 45 degrees and then in an orthogonal way once one of the axis is matched?

I'll take a look later, but surely it's a case of working out the relationship between the two differences on the axis.. i.e. if it's 20 units for X and 10 for Y, then move 2 of X for every Y moved. Something like that anyway.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
Do you mean that you want the balloon to move in a straight line as opposed to 45 degrees and then in an orthogonal way once one of the axis is matched?

I'll take a look later, but surely it's a case of working out the relationship between the two differences on the axis.. i.e. if it's 20 units for X and 10 for Y, then move 2 of X for every Y moved. Something like that anyway.

please take a look at the attached libgdx project. thanks,
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Thinking about it a little more, I would do the following..

X difference + (or -) 20

Y difference + (or -) 10

For every movement cycle add 1 to X and 0.5 to Y.. Obviously this is a simple example, but shows you where you should be heading. I would work out how the two are related first and then move. If you are able to work out the difference then you should just be able to use one sum as opposed to having two in your code...
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
I have, I'm just trying to make sure I understand the issue you want resolved.

What i want is this; example this is the start screen
1.png

then i touch the screen (on that x spot)

2.png

the balloon or object should move to that position.

3.png

something like this: http://gamesalad.com/featured-games/star-daze-hd
this is the gameplay:
hope this is clear.. hehehe..
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
what if i want to learn the code behind the library? i want to learn the logic behind it which maybe improve my codes.
-----
The library works great, but for me as that still starting in creating games, i want to learn on how i can move the object without the fancy variables like mass, max force, etc.... just using a plain vector2 object and maybe adding or subtracting something on X and Y just like the code i posted.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
i want to learn on how i can move the object without the fancy variables like mass, max force, etc.... just using a plain vector2 object and maybe adding or subtracting something on X and Y just like the code i posted.
A good point for you. Note that the SB library is based on this paper. There are plenty of useful explanations on locomotion.
In its most basic form, a move position is a linear interpolation between an origin and a target. So if you have a distance of 20 units on the X axis, and a distance of 10 units on the Y axis, then the move will go from 0 to 20 for X, and from 0 to 10 for Y. To reach the target with a linear move, you will increase X by 2 and Y by 1 (= 20/2 / 10) each step, for example.
Formula ("percent" is the progress to the target and ranges from 0 to 1):
X = startX + (endX - startX) * percent
Y = startY + (endY - startY) * percent
 
  • Like
Reactions: eps
Upvote 0

eps

Expert
Licensed User
Longtime User
Which is exactly what I was saying :) but maybe not quite as clearly put :)

This alteration to your code works... HTH

B4X:
#Region  Project Attributes 

    #ApplicationLabel: B4A Example

    #VersionCode: 1

    #VersionName: 

    'SupportedOrientations possible values: unspecified, landscape or portrait.

    #SupportedOrientations: portrait

    #CanInstallToExternalStorage: False

#End Region



#Region  Activity Attributes 

    #FullScreen: False

    #IncludeTitle: False

#End Region



Sub Process_Globals

    'These global variables will be declared once when the application starts.

    'These variables can be accessed from all modules.

    DimGameStateAsByte

    DimGameState_LoadingAsByte = 0

    DimGameState_RunningAsByte = 1

    DimGameState_PlayingAsByte = 2

End Sub



Sub Globals

    'These global variables will be redeclared each time the activity is created.

    'These variables can only be accessed from this module.

    DimStatetimeAsFloat = 0

    DimMovementSpeedAsFloat = 200

   

    DimTouchPosAslgMathVector2

    DimPositionAslgMathVector2

    DimbgBackgroundAslgTexture

    DimbgLoader    AslgTexture

    DimbgBalloon    AslgTexture

    DimbgFontAslgBitmapFont



    DimLibGDXAsLibGDX

    Dim GL As lgGL 

    DimGameViewAsView

    DimbatchAslgSpriteBatch

    DimcameraAslgOrthographicCamera  

    DimInputProcAslgInputProcessor

   

    Dim vpWidth As Float = 480

    DimvpHeightAsFloat = 800

   

    Dimmovementx, movementyAsInt

   

    DimsmoothingAsFloat

End Sub



Sub Activity_Create(FirstTime As Boolean)

    'Do not forget to load the layout file created with the visual designer. For example:

    'Activity.LoadLayout("Layout1")

    Dim config AslgConfiguration

    config.maxSimultaneousSounds = 4

    config.useAccelerometer = False

    config.useCompass = False

    config.useWakelock = True

    GameView = LibGDX.InitializeView2(config, "LibGDX")   

    Activity.AddView(GameView, 0dip, 0dip, Activity.Width, Activity.Height)

End Sub



Sub Activity_Resume

    If LibGDX.IsInitialized Then LibGDX.Resume 

End Sub



Sub Activity_Pause (UserClosed As Boolean)

    If LibGDX.IsInitialized Then LibGDX.Pause 

End Sub



Sub LibGDX_Create   

    'init sprite batch, will be use in rendering

    batch.Initialize



    'initialize asset

    bgBackground.InitializeWithFile(LibGDX.Files.internal("bg/bg.png"))

    bgLoader.InitializeWithFile(LibGDX.Files.internal("bg/default.png"))

    bgBalloon.InitializeWithFile(LibGDX.Files.internal("balloons/baloon_blue.fw.png"))



    bgFont.Initialize   

    bgFont.Scale(15)

   

    InputProc.Initialize("InputProc")

   

    Position.Set(vpWidth / 2, vpHeight / 2)

    TouchPos.Set(vpWidth / 2, vpHeight / 2)

   

    GameState = GameState_Loading

End Sub



Sub LibGDX_Dispose

    bgBackground.dispose 

End Sub



Sub LibGDX_Pause

   

End Sub



Sub LibGDX_Render

    'clear screen

    GL.glClearColor(0, 0, 0, 1)

    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)

   

    'Updates the matrices of the camera

    camera.Update

    'Uses the coordinate system specified by the camera

    batch.ProjectionMatrix = camera.Combined

   

    IfGameState = GameState_LoadingThen

       

        batch.Begin 

            Statetime = Statetime + LibGDX.Graphics.DeltaTime 

            If Statetime <= 5 Then

                batch.DrawTex2(bgLoader, 0, 0, vpWidth, vpHeight)

            Else

                GameState = GameState_Running           

            EndIf

        batch.End

       

        Return

    EndIf   

   

   

    'draw

    batch.Begin

        'elapse seconds

        batch.DrawTex2(bgBackground, 0, 0, vpWidth, vpHeight)

       

        MovePlayer

        batch.DrawTex(bgBalloon, Position.x, Position.y)

       

'        If TouchPos.x >= 0 AND TouchPos.y >= 0 Then

'            batch.DrawTex(bgBalloon, TouchPos.x - bgBalloon.Width / 2, TouchPos.y - bgBalloon.Height / 2)

'        Else

'            batch.DrawTex(bgBalloon, Position.x - bgBalloon.Width / 2, Position.y - bgBalloon.Height / 2)

'        End If



    batch.End

   

End Sub



Sub MovePlayer

    IfTouchPos.x <> Position.x ANDTouchPos.y <> Position.y Then

       

        Dim smoothingx, smoothingy As Float

       

        Ifmovementx > movementyThen

            smoothingx = 1

            smoothingy = smoothing

        Else

            smoothingx = smoothing

            smoothingy = 1

        EndIf

       

        'Log("smoothingx " & smoothingx)

        'Log("smoothingy " & smoothingy)

       

        IfTouchPos.x > Position.x Then

            'move right

            Position.x = (Position.x + (MovementSpeed * smoothingx) * LibGDX.Graphics.DeltaTime)

            'Position.x = (Position.x + smoothingy * LibGDX.Graphics.DeltaTime)

            'Position.x = (Position.x + MovementSpeed * smoothingx)

        ElseIfTouchPos.x < Position.x Then

            'move left

            Position.x = (Position.x - (MovementSpeed * smoothingx) * LibGDX.Graphics.DeltaTime)

            'Position.x = (Position.x - smoothingy * LibGDX.Graphics.DeltaTime)

            'Position.x = (Position.x - MovementSpeed * smoothingx)

        Else

            Position.x = TouchPos.x - bgBalloon.Width / 2

            TouchPos.x = Position.x

        EndIf



        IfTouchPos.y > Position.y Then

            'move up

            Position.y = (Position.y + (MovementSpeed * smoothingy) * LibGDX.Graphics.DeltaTime)

            'Position.y = (Position.y + smoothingx * LibGDX.Graphics.DeltaTime)

            'Position.y = (Position.y + MovementSpeed * smoothingy)

        ElseIfTouchPos.y < Position.y Then

            'move down

            Position.y = (Position.y - (MovementSpeed * smoothingy) * LibGDX.Graphics.DeltaTime)

            'Position.y = (Position.y - smoothingx * LibGDX.Graphics.DeltaTime)

            'Position.y = (Position.y - MovementSpeed * smoothingy)

        Else

            Position.y = TouchPos.y - bgBalloon.Height / 2

            TouchPos.y = Position.y

        EndIf

       

    EndIf

End Sub



Sub LibGDX_Resize(Width As Int, Height As Int)

    'init camera

    camera.Initialize 

    camera.SetToOrtho2(False, vpWidth, vpHeight)

End Sub



Sub LibGDX_Resume

   

End Sub



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

    'transform x and y location

    TouchPos.x = vpWidth * (ScreenX / LibGDX.Graphics.Width)

    TouchPos.y = vpHeight - (vpHeight * (ScreenY / LibGDX.Graphics.Height)) 'flip Y coords

    Log("TouchPos.x: " & TouchPos.x)

    Log("TouchPos.y: " & TouchPos.y)

   

    IfPosition.x > TouchPos.x Then

        movementx = Position.x - TouchPos.x

    Else

        movementx = TouchPos.x - Position.x

    EndIf

   

    IfPosition.y > TouchPos.y Then

        movementy = Position.y - TouchPos.y

    Else

        movementy = TouchPos.y - Position.y

    EndIf

       

   

    'Log("MovementX " & movementx)

    'Log("MovementY " & movementy)

   

    Ifmovementx > movementyThen

        smoothing = movementy / movementx

    Else

        smoothing = movementx / movementy

    EndIf

   

    'Log("smoothing " & smoothing)

   

    ReturnFalse

End Sub
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
A good point for you. Note that the SB library is based on this paper. There are plenty of useful explanations on locomotion.
In its most basic form, a move position is a linear interpolation between an origin and a target. So if you have a distance of 20 units on the X axis, and a distance of 10 units on the Y axis, then the move will go from 0 to 20 for X, and from 0 to 10 for Y. To reach the target with a linear move, you will increase X by 2 and Y by 1 (= 20/2 / 10) each step, for example.
Formula ("percent" is the progress to the target and ranges from 0 to 1):
X = startX + (endX - startX) * percent
Y = startY + (endY - startY) * percent

thanks, i wish we can have a small tutorial for this.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
Certainly, attached.

haha.. thanks, i tried it but the result is the same. there are position where it still shakes (the balloon) like it does not know where to go.

I think this is a problem because of the fix increment of movement. But i can't figure out on how i should change it. hahaha..
 
Upvote 0

eps

Expert
Licensed User
Longtime User
haha.. thanks, i tried it but the result is the same. there are position where it still shakes (the balloon) like it does not know where to go.

I think this is a problem because of the fix increment of movement. But i can't figure out on how i should change it. hahaha..

So that's the problem? Using my code the balloon now moves in a straight line to it's destination. I thought as did @Informatix that this was the problem you were having... If it's shaking which is the problem, then you need a tolerance in place, which will then probably resolve that issue.

:)
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
will just recheck my codes thanks,.

btw, @Informatix, how do we handle msgbox dialogs when using libgdx?

B4X:
Sub lTouchControl_KeyUp(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        'check gamestate if back or quit
        'ERROR: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        If GameState = GameState_STARTED Then
            If Msgbox2("Do you want to go back to mainscreen?", "Back", "OK", "Cancel", "", Null) = DialogResponse.POSITIVE Then
                GameState = GameState_HOME
                Return True
            End If
        End If
    End If
    Return False
End Sub
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
will just recheck my codes thanks,.

btw, @Informatix, how do we handle msgbox dialogs when using libgdx?

B4X:
Sub lTouchControl_KeyUp(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        'check gamestate if back or quit
        'ERROR: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        If GameState = GameState_STARTED Then
            If Msgbox2("Do you want to go back to mainscreen?", "Back", "OK", "Cancel", "", Null) = DialogResponse.POSITIVE Then
                GameState = GameState_HOME
                Return True
            End If
        End If
    End If
    Return False
End Sub
If you want to display a dialog, you have to create it. A MsgBox will crash your app and would look a bit strange in a game. The simplest way to create a dialog is to use a Window (a Scene2D container).
 
  • Like
Reactions: eps
Upvote 0
Top