0

I am trying to get my player to move based off acceleration and velocity. Here is how the velocity is found and the player is moved:

vel.x += acceleration.x;
coordinates.x += vel.x;

The coordinates controls where the player is. Here is how acceleration is calculated:

if(moveRight && !(acceleration.x >= .1f)){
    acceleration.x += .1f * Gdx.graphics.getDeltaTime();
}

First question: Is this a good way to move the player if I am going to use mass and force to move the player.

I am also trying to get the player to come to a halt based off the acceleration, here is the extended version of the above code where I try to do this:

if(moveRight && !(acceleration.x >= .1f)){
    acceleration.x += .1f * Gdx.graphics.getDeltaTime();
}else if(!moveRight && (vel.x > 0)){
    acceleration.x -= .1f * Gdx.graphics.getDeltaTime();
}

However, this doesn't work. When I move forward it accelerates forward, then when I let go, it stops, then shoots backwards.

Second question: How can I get the above code to work in the way I desired.

Thanks for any help regarding these few questions.

Pookie
  • 1,239
  • 1
  • 14
  • 44
  • 2
    I'm just going to say this first: `!(acceleration.x >= .1f)` is a _lot less_ readable than simply `acceleration.x < .1f` IMHO. Second: can you be more specific than "doesn't work"? How does the output differ from what you expect? – Arc676 May 23 '16 at 08:49
  • @Arc676 I edited the question – Pookie May 23 '16 at 08:51
  • What is `movementSpeed`? – Arc676 May 23 '16 at 08:54
  • Use a debugger. It is not surprising that `vel.x` would be negative and stay negative at some point with this code. – Julien Lopez May 23 '16 at 08:59

1 Answers1

0

Have a look at this question about friction. By playing with the friction coefficient, you can get a pleasing gradual slow down in movement. It's worth asking if you are aware of existing physics engines for Java that might make this whole thing a lot simpler: JBox2d is just for 2D, and JBullet is a port of the 3D Bullet engine.

Community
  • 1
  • 1
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40