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.

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.

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):

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?

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org