Have a variable set to the past value of another variable

About advanced scenes, and the Thyme scripting language used in Algodoo.

Have a variable set to the past value of another variable

Postby UnityDogGaming04 » Sun Oct 17, 2021 12:03 am

I'm looking for a way to have one variable set to the value some other variable was at N seconds ago, without needing N * sim.frequency other variables or objects. My intuition tells me it's not possible, which is probably true, but maybe someone has found a way?
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Have a variable set to the past value of another variable

Postby FRA32 » Wed Oct 27, 2021 1:13 pm

There is no direct way to access the past if you aren't logging it yourself, but it's possible to estimate it using maths, altough this still requires variables:

If you have a variable that changes in a predictable way, say, linearly or via accelleration(gravity), you can get an approximate function of the past by recording your variable's derivatives!

[Disclaimer: Some maths incomming. The result isn't that complicated but figuring out how to use it takes some info]

A derivative is the rate of change of some number. The first derivative is commonly known as the "speed" of something and as such describes how quickly a variable changes. The second derivative is the derivative of the first derivative and describes some form of accelleration. Keep going down the bottomless hole of derivatives and you get more and more expressions of change.

You can calculate an approximate derivative(lets call it y') yourself using a differential. At each point of time, subtract the past value of your variable(lets call that "yOld") from the new value ("yNew") and divide that by the time that has passed(e.dt for postStep/Update). We can call the difference between yOld and yNew "dy".
In short: y'=(yNew-yOld)/e.dt=dy/e.dt.
This is the first derivative, a.k.a. the slope of your function. You can make a second derivative from this aswell by comparing the old value of the slope with the new one in the exact same way:
If your variable changes linearly, you only need one derivative. If it changes in an accellerated way, two will be needed. If your variable changes in a complex way, you will need many more derivatives to capture it's behaviour, and if it's too complex, you might be better off just saving the past values of your variable directly instead of calculating it.

So how do you look into the past using derivatives? You can make an estimate(!!!) copy of the underlying behaviour by making a "Taylor polynome" out of them. Sounds complicated, but since we already have the derivatives, it's fairly simple:

A taylor polynome is pieced together from a chain of pieces. If we number each piece with a number "i" starting at 0, then each piece needs 3 things:

The i'th power of your time (1,t,t²,t³,...)
The (i)'th derivative, with the current y value itself as 0th derivative (y,y',y'',y''',...)
The factorial of i (i! = 1,1,2,6,24,120,...)

One piece of the taylor polynome is then calculated like this:
t(i)=y(i) * t^i / i! (y(i) is the aforementioned derivative)
The entire taylor polynome then looks like this:

T=t(0)+t(1)+t(2)+... for as many derivatives as you have.

With this you can simply put in a negative number to get an estimate value of the past, but do know that going further back(or forwards) makes the estimate worse. Experimentation is required to see if this actually works for your needs.

If you wanna try this and have questions on how exactly to implement this, message me on our algodoo discord group(the link's in the "Ghost town" post in the living room section)
Posts: 227
Joined: Wed Dec 03, 2014 9:51 pm

Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 0 guests