Mim Hufford
Mim Hufford

Reputation: 609

Thrust movement on single axis, very strange behaviour

I've noticed some very strange behaviour with a simple piece of code, I've stripped it down to remove any variables, time-step, acceleration, etc.

I have a shape on the screen, pressing the left side of the screen thrusts the shape right, pressing the right side thrusts left. For some reason the shape favours moving left (speed < 0). It seems to be that it decelerates properly when moving right but when moving left it finishes decelerating then continues for maybe 0.3 seconds before stopping.

float speed;
int x;

Update method for the shape:

speed *= 0.9f;

if (Math.abs(speed) < 0.1f)
    speed = 0;

if (Gdx.input.isTouched()) {
    if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
        speed = 10;
    } else {
        speed = -10;
    }
}

x += speed;

Upvotes: 0

Views: 68

Answers (1)

Mim Hufford
Mim Hufford

Reputation: 609

x should be defined as float. I used int because the viewport is set to the screen size but finally realised it needs to have the precision.

Upvotes: 2

Related Questions