0

When working with creating games in Libgdx, I have not been using the physics engine as I dont have too many moving parts in the game. So, to have objects fall from the top of the screen to the bottom, I used what the documentation had, something like this:

projectile.y -= 200 * Gdx.graphics.getDeltaTime();

That example will make said projectile go down at 200 pixels per second (I believe). What I am trying to do is make it so after two seconds, the projectile will transition from negative 200 per second, to positive 200 per second. I've tried using loops and Thread.sleep, but that will just freeze the entire game and unfreeze with the projectiles going the other way. Any ideas?

3 Answers3

1

Linear interpolation.

All you need to do is determine the start point: x1 = -200

Determine the end point: x2 = 200

Determine the amount of seconds that it takes to reach the end point: tmax = 2.0 sec

Determine the difference that you need to add to the original to reach the end point: v = (x2-x1) = (200 - (-200)) = 400

Use the linear interpolation function: x1 + t*v = x2 where t e [0...1] //must be normalized to 0..1 interval

Thus at t = 0, the value is at x1 + 0 = x1; and at t = (tn/tmax) [which is 1], the value is at x1 + v = x2.

So all you need is a timer from 0 to 2 and the following equation:

float interpolationTimer = 0.0f;
final float interpolationTimerMax = 2.0f;

public void render()
{
    float delta = Gdx.graphics.getDeltaTime();
    interpolationTimer += delta;
    if(interpolationTimer > interpolationTimerMax )
    {
        interpolationTimer = interpolationTimerMax ;
    }
    velocity.y = -200 + (interpolationTimer/interpolationTimerMax) * (400); //x1 + t*v = x2
    projectile.y -= velocity.y * delta;
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • For the completeness of this answer, I'll mention that this is just one of many possible `easing functions` that can be used to interpolate, here is more info http://stackoverflow.com/questions/8316882/what-is-an-easing-function . Interpolation is also called `tweening` and there are libraries for it, for example http://www.aurelienribon.com/blog/projects/universal-tween-engine/ . – EpicPandaForce Aug 01 '14 at 07:53
0

To get a change in direction of y, you need a polynomial function of x. For a single direction change, use a binomial; try something like

projectile.y = projectile.y 
    - 200 * Gdx.graphics.getDeltaTime() 
    + 20 * Math.pow(Gdx.graphics.getDeltaTime(), 2);
  • 1
    Just a note: I believe that `^` is the XOR operator in Java. Perhaps the `Math.pow()` method is what you want? – Mike M. Jul 16 '14 at 02:32
  • This does slow down the projectiles, however can we make it so that over the course of about a second we get it from -200 to positive 200? So at .5 seconds it will stop and by 1 whole second it is travelling in the opposite direction? I already have a working timer class, if that is also needed. – Brandon Reed Jul 16 '14 at 02:47
  • 1
    @MikeM.: thanks, you're right & my java is very rusty –  Jul 17 '14 at 17:15
0

If you are looking for linear interpolation of the speeds, just keep track of time.

float timeElapsed = 0.0f;

void render() {
    timeElapsed += Gdx.graphics.getDeltaTime();

    projectile.y -= 200.0f * (1.0f - timeElapsed);
}

Be sure to stop after timeElapsed has reached 2 seconds (that's if(timeElapsed < 2.0f)). Time elapsed variable will start at 0.0f and will slowly increment. Until it reaches 1.0f, projectile.y will get substracted from. However, as soon as time elapsed is higher than 1.0f, projectile.y will get added to.

Juraj Pancik
  • 301
  • 2
  • 8