LibGdx Velocity doesnt change after threshold

Hi All,

I am using LibGdx to make a small android app using Box2D. My problem is quite odd. The idea is that if I touch the screen (or use mouse) then an impluse is applied to the player to move them toward to touch position:

Code: Select all

        if(Gdx.input.isTouched())
        {
           Vector2 delta = new Vector2(Gdx.input.getX()  - player.getWorldCenter().x, Gdx.input.getY()  - player.getWorldCenter().y);

           double angle = Math.atan2(delta.y, delta.x);
          
           Vector2 direction = new Vector2((float)Math.cos(angle), (float)-Math.sin(angle));
          float power = 100f;
               
          Vector2 impulse = new Vector2(power * direction.x, power * direction.y);
          Vector2 point = player.getWorldCenter();
          player.applyLinearImpulse(impulse, point, true);           
        }


This does work, however if I use this system for about 30 seconds, the player's velocity's y component either slowly changes to around +190 or - 190. I can apply a force in the other direction by touching the other direction, this slightly affects the velocity but then it just changes back to +- 190.

I have no idea what is going on, does anyone have any suggestions?

Regards,

Alex
What is the units you are using? I mean your bodies are created in pixel units? I guess the value you get is the maximum velocity available for the scale of your bodies. As an example, I develop a spaceship simulator where spaceships are made of 0.1x0.1 blocks, and I can tell you when I get a velocity of +10, the spaceship fly across the screen very very fast 🙂

You might want to scale down all your bodies?
I think I have been using pixels. Can you point me in the direction of a good tutorial on how to set up the distance to make my project work? I can share the code if you are interested in pointing out what I can do?
No need to paste code yet,

http://box2d.org/2011/12/pixels/

Just divide by 50 (or multiply by 0.02) to get value in meters.

Example: I have a 32x32 BALL => 0.64x0.64 BODY in BODY2D.

Create two helper functions like this:

static float toPixels(float meters) {
return meters * 50.0f;
}

static float ToMeters(float pixels) {
return pixels * 0.02f;
}

To convert your units on the fly.
I am a bit confused as to where I use "meters". I am using the Java port of Box2d and there is no "gluOrtho2D" function or anything resembling it that I can see. I have looked under the attributes of BodyDef, CircleShape, FixtureDef and Body and I can't find anything that lets me set the size or anything like that as it uses a radius (which I have set to 8).

In general I am just confused as to where meters I think.
The link I provided you was giving an example in C, not in JAVA. The gluOrtho2D function is used for setting up a orthographic projection in OPENGL, you can forget about that. LIBGDX take care of all that for you.

The two things you can scale are positions and shape size in your case.

Position is usually defined in bodyDef and in the shape attached (through the fixture). In your case I guess
you only have one shape attached to your body.

The size of the shape in your case is a radius since you are using a CircleShape.

You need to both scale bodyDef position and CircleShape radius. (multiply by 0.2f or divide by 50)

When you render your object to the screen you need to scale the values again (multiply by 50)

You also need to lower your impulse value because it would be too fast now that you have changed scale of your entire simulation.
Hmmm I think I have applied what you said however it still isn't working. I am using the debug render to draw to screen at the moment. I have attached my code so you can check that I have done what you said properly.

I really appreciate all your assistance!

Original thread: https://box2d.org/forum/viewtopic.php?t=9508