# Using Lerp and TimeDelta to move objects over time

### » Tue Sep 06, 2011 3:12 am

Ok, I pretty much get Lerp (thanks to some awesome posts covering it) and TimeDelta is easy, but I'm missing something crucial for using them together...

I basically want to move two objects each a set, but different, distance, over the course of say 1500ms. I've been trying to use lerp and TimeDelta to achieve this, and I can get them to work individually, but I can't seem to synch them up for some reason...

is lerp the wrong algorithm to use?

It should be something like...

//object1.X <> ScrollX//
set object1. X
lerp(object1.x, destinationX, x*TimeDelta)
set ScrollX
lerp(ScrollX, destinationX, x*TimeDelta)

How do I change x*TimeDelta for both objects so that no matter how different their X coords are, they'll still end in the same X at the same time?

B
70
S
10
G
9
Posts: 300
Reputation: 8,962

### » Tue Sep 06, 2011 3:14 am

OR is there something that will work something like...

mathcommand(a, b, x)//change a into b over x ms?//
B
70
S
10
G
9
Posts: 300
Reputation: 8,962

### » Tue Sep 06, 2011 3:29 am

wait... did I just ask for ((b-a)/x)... are you serious...
ok, before I try it, someone please tell me if what I just asked for is ((b-a/x)!! I hate sucking at math...
B
70
S
10
G
9
Posts: 300
Reputation: 8,962

### » Tue Sep 06, 2011 3:33 am

I guess I could just use that formula at the beginning of the movement to determine the distances/increments per ms and save it to a private variable for each object, and then use those variables as the distance to move each object per tick *TimeDelta... Ok, someone weigh in here, did I just solve my own stupid problem with an equally stupid solution? Or am I WAAAAAAY off, which given my math skill is probably the more likely answer xD
B
70
S
10
G
9
Posts: 300
Reputation: 8,962

### » Tue Sep 06, 2011 3:55 am

[QUOTE=brent_hamel]
//object1.X <> ScrollX//
set object1. X
lerp(object1.x, destinationX, x*TimeDelta)
set ScrollX
lerp(ScrollX, destinationX, x*TimeDelta)

How do I change x*TimeDelta for both objects so that no matter how different their X coords are, they'll still end in the same X at the same time?

Don't think too complicated.
So you want two objects with different distances to destinationX to reach that point at the same time. lerp is perfect for that, because wherever you start, as soon as the t-value reaches 1, both will be at destinationX. All you need to do is raising the t-value from 0 to 1 over the period of time you want.

If you want a number to grow over time, you add n * timedelta to it. The maximum value of t is 1.0. How long do you want it last until 1.0 is reached? 'n * timedelta' equals 'amount per second'

To have it last 2 seconds, you'd add 0.5 * timedelta (2 * 0.5 = 1)
To have it last 5 seconds, you'd add 0.2 * timedelta (5 * 0.2 = 1)
etc.

+ some condition
-> myT = myT + 0.5 * TimeDelta
-> lerp(object1.x, destinationX, myT)
-> lerp(ScrollX, destinationX, myT)

Just make sure, myT isn't raised anymore, after 1.0 is reached.
B
24
S
8
G
10
Posts: 1,822
Reputation: 8,291

### » Tue Sep 06, 2011 4:17 am

Brilliant lol, thank you so much!! I knew I was missing something!!
B
70
S
10
G
9
Posts: 300
Reputation: 8,962