Java/LWJGL Need advice using linear interpolation for easing/acceleration

So I’ve been spending a few hours a day trying to understand linear interpolation. My understanding of the concept has improved, as I didn’t understand the math behind it previously. I decided to get graph paper so I could understand it a better, and Jamie King and bunch of other guys have some hands on demonstration about how it works which was handy.

The demonstration gave me a clue as to how linear interpolation works, so I decided to give it a shot. I wrote a new Vector2f method like so.

[quote] public Vector2f lerpT(Vector2f currentPos, Vector2f newPos, float alpha)
return currentPos.scale(1f - alpha).add(newPos.scale(alpha));
//return Vector3f point1 + alpha * (point2 - point1);
And my main class is like so:

This is what gets printed out into the console.

[quote]x: 63.5
I calculated the formula from a graph I made and with a calculator and I get the same values in the x and y’s lerpPos vector that is calculated in the code. I also changed it to Vector2f to make it simpler. And instead of LWJGL I decided to make a simple console program in java.

While I understand it a bit more, I’m unsure how I’m supposed to use a velocity vector with the lerp function so I can give something some acceleration(?). I see how the alpha in the formula is important, but I’m unsure how to utilize it.

If anything, I’d like to tryout some easing in and easing out, but the witchcraft needed seems a little bit over my head just at this moment.

Any help or suggestions would be much appreciated.

(this looks like the corresponding Stackoverflow question:

yes it is, im the same person. Is there something wrong with that? I’mma bit impatient, seeing as if its been a few days since I made that post on stack overflow. To be honest though I found this video and made me realize I should calculate the distance of each vector like in this video.

No, nothing wrong with that. It’s totally fine. Just wanted to reference it for people to catch up on/sync with what might have already been answered on it. And maybe people would rather prefer to answer there for larger visibility.

You simply have to know more about Classical Mechanics (the relations between position, velocity and acceleration) and with it a bit about the “Calculus” branch of mathematics, which concerns itself with rates of change, also called “differentials” to do this properly.

But let’s start fresh. What is a position? When in two-dimensional space we can simply define it as the pair/vector (x, y) denoting the cartesian coordinates of that position in 2D space.

Next: What is velocity? Velocity is the rate of change of position over time, or in math words “Velocity is the first-order derivative of position with respect to time”. In calculus, we can write it like so V(t) = dP/dt, that just means: Velocity at any given point in time t is the change of position P over an infinitesimal change of time dt.

When velocity is constant we can compute the position with: P(t) = P(0) + V*t, where P(0) is the initial position and V the velocity. So the position is just a linear function of velocity and time.

Next: What is acceleration? Just like velocity is the rate of change of position over time, acceleration is the rate of change of velocity over time. So: A(t) = dV/dt.
Likewise, when acceleration is constant, we can compute the velocity using: V(t) = V(0) + A*t, where V(0) is the initial velocity and A is the acceleration.

Now, when we have that down, the question to ask is: What do you want to do?
It seems like you want to accelerate your character when it starts moving and decelerate it when it stops moving. So your character should not reach a constant velocity immediately, but instead you want to linearly interpolate the velocity from zero to a maximum velocity Vmax when accelerating and the other way around to 0 when decelerating.

The easiest way to do this is by choosing a constant acceleration/deceleration for your character, say 1 “unit” per square second, so A = 1. When you have this, you can compute the velocity at any given point in time that the character is moving via: V(t) = A*t.

Since your acceleration will never be truly constant (only one single value) but your character can start and stop moving at any given time when you e.g. press the cursor keys, you need to “integrate” the velocity over time. Since you also likely have a “game/logic loop” that runs between some time interval dt, you can now use that to numerically approximate the integral by summation:

V(t+dt) = V(t) + A*dt

That means: At any game loop iteration time t+dt your velocity is the old velocity at t plus your acceleration A times the elapsed time between the last and this iteration, denoted by dt.
So, this applies always. Now, in order to accelerate you simply set A to 1, and to decelerate you set it to -1. And you should cap your velocity using some minimum = 0 and maximum = Vmax values.
That should be what you wanted to do with lerp() in the first place.

Now, at every given moment you know the velocity, and computing the new position based on the velocity should be easy for you now.

Thank you for your reply. I’m going to work on more calculus in the future, some of that math seems intimidating and confusing to me. But I really appreciate your descriptions as they help me understand it better. Could you help with a few things just to make things clearer?

[quote]V(t) = dP/dt
So V is the Velocity vector, t is time? I’m unsure what dP refers to, I can only guess it means destination position. dt is delta time?

update: does it mean delta position?

[quote]P(t) = P(0) + V*t
Whats the difference between P(t) and P(0). If t is time, what is it when its P(t)? Whats the difference between time and delta time?

Please excuse my difficulties, I intend to get better at understanding the math and calculus.

I’ve worked with vectors and implementing position and velocity but I’d like to get closer in thought with the math and whats happening behind the scenes.

Really appreciate your help. Now I’m gonna go study a bit and see what happens.

Don’t let it scare you. I assume that you haven’t yet had an introduction to Algebra 1 and “functions” in school. It is practically impossible to explain everything to you here.
Please also note that Calculus is usually taught in 12th grade at schools, and the very related Pre-Calculus/Algebra 2 including “Analysis” (where you learn how to calculate the “slope” of a function at a given point) in 10th/11th grade.
So, if you are currently below such grades, are attending a school and are planning to reach such grades, you probably should not concern yourself with it yet.
I just wanted to tell you above how things are related, without knowing your current level of math and physics education.

For all practical reasons, you can just use this pseudo-code per frame (haven’t tested it, though not necessarily correct):

// Compute new velocity and position (assume unit vectors when not specified otherwise)
float dt = number of seconds elapsed since last frame;
vec2 direction = this is the direction along which the character would move if it were moving, so probably the direction it is looking at;
vec2 acceleration = if character should move then this is `direction`, otherwise it is `-direction`;
vec2 newVelocity = oldVelocity + acceleration * dt;
float maxVelocity = the maximum velocity (such as 5.0f);
if (lengthOf(newVelocity) > maxVelocity)
  newVelocity = normalize(newVelocity) * maxVelocity;
else if (dotProductOf(newVelocity, direction) < 0.0)
  newVelocity = vec2(0.0);
vec2 newPosition = oldPosition + newVelocity * dt;
// Do something with newPosition
// ...
// Set old = new
oldVelocity = newVelocity;
oldPosition = newPosition;

Much appreciated!! Excuse my impatience, I’m gonna give it another go. I’ll post something when I get the desired effect. Thanks


So I’ve been slowly implementing some entities with velocity and accelertion: the code looks like this:

[quote]public Entity(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ,
float scaleX, float scaleY, float scaleZ) {
this.model = model;
this.position = position;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
this.scaleX = scaleX;
this.scaleY = scaleY;
this.scaleZ = scaleZ;

	position = new Vector3f(0,0,0);
	velocity = new Vector3f(0,0,0);
	acceleration = new Vector3f(0,0,0);
	direction = new Vector3f(0,0,0);

public void update() {
	velocity = velocity.add(acceleration);
	position = position.add(velocity);
	//if(velocity.length() > maxVelocity) {
		this.velocity.y = maxVelocity;

cubes pic

at the moment they’re just flying upward into the sky and respawning at the bottom, and the acceleration just keeps increasing

psuedo code

[quote]if (lengthOf(newVelocity) > maxVelocity)
newVelocity = normalize(newVelocity) * maxVelocity;
else if (dotProductOf(newVelocity, direction) < 0.0)
newVelocity = vec2(0.0);
tested code

I’m assuming my attempt is wrong, just posting this to see if it makes sense to anyone.

I get the velocity and acceleration part, but I’m having issues with limiting velocity. Any suggestions? Thanks

Update: So I’ve got the limit running, which seems to work fine. Now I need a suitable input method to get a platformer type vibe. How can I apply interpolation to this? Is it neccesary if you want the camera to drag behind the player?