Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (748)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (834)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java/LWJGL Need advice using linear interpolation for easing/acceleration  (Read 1545 times)
0 Members and 1 Guest are viewing this topic.
Offline cybrmynd

Junior Devvie


Medals: 5



« Posted 2018-01-06 14:11:40 »

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.

https://www.youtube.com/watch?v=0MHkgPqc-P4

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:
Quote
public class Main {

    public static void main(String[] args) throws IOException {
    init();
}

public static void init() {

   Vector2f currentPosition = new Vector2f(70f,68f);
   Vector2f newPosition = new Vector2f(57f,80f);

    Vector2f lerpPos = new Vector2f().lerpT(currentPosition, newPosition, 0.5f);

    System.out.println("Testing LERP: " + "5/01/2018" + "\n");
    System.out.println(lerpPos.x);
    System.out.println(lerpPos.y);


This is what gets printed out into the console.

Quote
x: 63.5
y:74.0

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.
Offline KaiHH

JGO Kernel


Medals: 501



« Reply #1 - Posted 2018-01-06 14:25:58 »

(this looks like the corresponding Stackoverflow question: https://stackoverflow.com/questions/48004878/using-lerp-in-lwjgl3)
Offline cybrmynd

Junior Devvie


Medals: 5



« Reply #2 - Posted 2018-01-06 14:43:05 »

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.

https://www.youtube.com/watch?v=zLh0K1PdUbc&list=PL7wAPgl1JVvVJabcmRyEwg8k9rFxCRU9x
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 501



« Reply #3 - Posted 2018-01-06 14:47:06 »

Is there something wrong with that?
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.
Offline KaiHH

JGO Kernel


Medals: 501



« Reply #4 - Posted 2018-01-06 15:42:07 »

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.
Offline cybrmynd

Junior Devvie


Medals: 5



« Reply #5 - Posted 2018-01-07 12:30:17 »

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.
Offline KaiHH

JGO Kernel


Medals: 501



« Reply #6 - Posted 2018-01-07 18:36:32 »

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):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
// 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;
Offline cybrmynd

Junior Devvie


Medals: 5



« Reply #7 - Posted 2018-01-16 12:37:04 »

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

Junior Devvie


Medals: 5



« Reply #8 - Posted 2018-01-23 09:16:03 »

Update:

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);
      
      //position.length()
      
      //Limit
      //if(velocity.length() > maxVelocity) {
         this.velocity.y = maxVelocity;
      //}
   }


cubes pic
https://ibb.co/jmVFWw

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

Quote
if (velocity.length() > maxVelocity) {
 this.velocity = velocity.normalize * maxVelocity;
else if (velocity.dot(direction) < 0.0) {
this.velocity = new Velocity(0,0,0);

revised

if (velocity.length() > maxVelocity) {
   this.velocity = velocity.normalize().scale(maxVelocity);
} else if (velocity.dot(direction) < 0.0) {
   this.velocity = new Vector3f(0,0,0);
   }
}

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?
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (294 views)
2017-12-31 17:17:51

xxMrPHDxx (88 views)
2017-12-31 17:15:51

xxMrPHDxx (144 views)
2017-12-28 18:11:33

Ecumene (308 views)
2017-09-30 02:57:34

theagentd (490 views)
2017-09-26 18:23:31

cybrmynd (519 views)
2017-08-02 12:28:51

cybrmynd (461 views)
2017-08-02 12:19:43

cybrmynd (443 views)
2017-08-02 12:18:09

Sralse (402 views)
2017-07-25 17:13:48

Archive (1304 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!