Skip to main content
added 24 characters in body
Source Link
spectacularbob
  • 1.4k
  • 1
  • 10
  • 12

I assume that you are calling update() once every frame, before you draw the frame.

Your problem is that you are doing either the collision correction beforeor the movement every frame. You need to do both. What's happening is:

  1. Your character checks if it is below the screen bounds, it isn't
  2. So it moves below the screen bounds, draws for one frame below the screen bounds
  3. Then, on the next frame checks again if it is below the screen bounds, it is, so it corrects itself

Try this

public void update(float dt) {
    //Let the player intially move wherever they want
    velocity.add(0, GRAVITY, 0);
    velocity.scl(dt);
    location.add(MOVEMENT * dt, velocity.y, 0);
    velocity.scl(1 / dt);

    //If they ended up below the screen, move them up and cancel out gravity
    if (location.y < screenBounds.y) {
        velocity.y = 0;
        location.y = screenBounds.y;
    }
}

Also, you probably don't need the scl() calls. Instead, just multiply the y velocity by dt

location.add(MOVEMENT * dt, velocity.y * dt, 0);

I assume that you are calling update() once every frame, before you draw the frame.

Your problem is that you are doing the collision correction before the movement every frame. What's happening is:

  1. Your character checks if it is below the screen bounds, it isn't
  2. So it moves below the screen bounds, draws for one frame below the screen bounds
  3. Then, on the next frame checks again if it is below the screen bounds, it is, so it corrects itself

Try this

public void update(float dt) {
    //Let the player intially move wherever they want
    velocity.add(0, GRAVITY, 0);
    velocity.scl(dt);
    location.add(MOVEMENT * dt, velocity.y, 0);
    velocity.scl(1 / dt);

    //If they ended up below the screen, move them up and cancel out gravity
    if (location.y < screenBounds.y) {
        velocity.y = 0;
        location.y = screenBounds.y;
    }
}

Also, you probably don't need the scl() calls. Instead, just multiply the y velocity by dt

location.add(MOVEMENT * dt, velocity.y * dt, 0);

I assume that you are calling update() once every frame, before you draw the frame.

Your problem is that you are doing either the collision correction or the movement every frame. You need to do both. What's happening is:

  1. Your character checks if it is below the screen bounds, it isn't
  2. So it moves below the screen bounds, draws for one frame below the screen bounds
  3. Then, on the next frame checks again if it is below the screen bounds, it is, so it corrects itself

Try this

public void update(float dt) {
    //Let the player intially move wherever they want
    velocity.add(0, GRAVITY, 0);
    velocity.scl(dt);
    location.add(MOVEMENT * dt, velocity.y, 0);
    velocity.scl(1 / dt);

    //If they ended up below the screen, move them up and cancel out gravity
    if (location.y < screenBounds.y) {
        velocity.y = 0;
        location.y = screenBounds.y;
    }
}

Also, you probably don't need the scl() calls. Instead, just multiply the y velocity by dt

location.add(MOVEMENT * dt, velocity.y * dt, 0);
Source Link
spectacularbob
  • 1.4k
  • 1
  • 10
  • 12

I assume that you are calling update() once every frame, before you draw the frame.

Your problem is that you are doing the collision correction before the movement every frame. What's happening is:

  1. Your character checks if it is below the screen bounds, it isn't
  2. So it moves below the screen bounds, draws for one frame below the screen bounds
  3. Then, on the next frame checks again if it is below the screen bounds, it is, so it corrects itself

Try this

public void update(float dt) {
    //Let the player intially move wherever they want
    velocity.add(0, GRAVITY, 0);
    velocity.scl(dt);
    location.add(MOVEMENT * dt, velocity.y, 0);
    velocity.scl(1 / dt);

    //If they ended up below the screen, move them up and cancel out gravity
    if (location.y < screenBounds.y) {
        velocity.y = 0;
        location.y = screenBounds.y;
    }
}

Also, you probably don't need the scl() calls. Instead, just multiply the y velocity by dt

location.add(MOVEMENT * dt, velocity.y * dt, 0);