0

I have been following tutorial from this link for implementing touchpad in LIBGDX. I want to set the linear velocity for a body using touchpad . I tried updating the position as per this tutorial but the body is not moving smoother.

This is my code for setting up linear velocity,

public void knobinput(float dt)
{
 if(touchpad.getKnobPercentX()>0)
{
    gamehero.heroBody.setLinearVelocity(1.4f, 0);
}
else
{
    gamehero.heroBody.setLinearVelocity(-1.4f, 0);
}
}

When I implement this logic, the body started moving though I didn't give any inputs through touchpad. I want to set the linear velocity as per the above code when the knob is turned right and left but, I didn't know how to check if the knob is turned right or left. Please help. Thanks in advance.

Anusha
  • 939
  • 3
  • 13
  • 31

1 Answers1

2

You do not handle the situation when the touchpad is in it's zero position - I mean you have no code for stopping body there. Take a look at this fragment:

    else
    {
        gamehero.heroBody.setLinearVelocity(-1.4f, 0);
    }

Even if you do not move the touchpad body has some velocity set.

The best approach would be to set velocity directly based on touchpad position without any conditions like:

    gamehero.heroBody.setLinearVelocity(SPEED * touchpad.getKnobPercentX(), 
                                        SPEED * touchpad.getKnobPercentY());

It will handle zero position of touch pad (and body will have (0, 0) velocity set as it should) and it's speed will be based on touchpad's position value also (what does mean that if you move a touchpad a little body will move slowly and if you will move touchpad to the edge it will move with maximum speed - also as it should I guess).

SPEED variable should be the max speed that you need. In this case you can set SPEED = 1.4f for example.

m.antkowicz
  • 13,268
  • 18
  • 37
  • Thanks a lot.!! It worked @m.antkowicz and could you please tell whether using this "touchpad" has an effect on gravity ? Because body falls slowly and it takes more time to reach the ground . This problem didn't arise when I didn't use "touchpad" – Anusha Apr 14 '16 at 15:57
  • Yup - if there are another forces that are modifying body's position/velocity you should handle them by using current body's velocity somehow. I'm afraid that simple `gamehero.heroBody.getLinearVelocity().x + SPEED * touchpad.getKnobPercentX()` will not produce satisfying effect - you have to try how to use current body's velocity due to characteristic of your game – m.antkowicz Apr 14 '16 at 16:01
  • Could you give me an idea of how to change the gravity accordingly ? I seriously have no idea and there are less tutorials for this touchpad !! Help would be great @m.antkowicz – Anusha Apr 15 '16 at 12:10