3
$\begingroup$

We are game developers currently working on a new game. We are looking to solve the following problem:

We start with the following variables:

$x = 0, h = 1;$

The variable x should always increase over time depending on $h$—the larger the $h$ the larger the rate that $x$ increases. On the other hand $h$ decreases over time with a steady velocity. $h$ ranges from 0 to 1.

After time $t$ the variable $x$ will become:

$x := x + \sum( C_1 \cdot ( h + V \cdot n ) ) \mathrm{\ for\ } n = [0, t]. $

$C_1$ is a constant. $V$ is the change rate of $h$.

Whenever we calculate $x$ we need to calculate the new value of $h$ like so:

$h := h + V \cdot t$

So what happens in order is this:

  • $x := x + \sum( C_1 \cdot ( h + V \cdot n ) ) \mathrm{\ for\ } n = [0,t]$
  • $h := h + V \cdot t$

Both of these formulas could be wrong of course, this is simply what we are trying to achieve.

The variable t is essentially time in seconds. The problem is that if say $t = 2$ then $x$ will have a certain value if we calculate it like so:

  • $x := x + \sum( C_1 \cdot ( h + V \cdot n ) ) \mathrm{\ for\ } n = [0,2]$
  • $h := h + V \cdot 2$

and a different value if we calculate it like so:

  • $x := x + \sum( C_1 \cdot ( h + V \cdot n ) ) \mathrm{\ for\ } n = [0,1]$
  • $h := h + V \cdot 1$
  • $x := x + \sum( C_1 \cdot ( h + V \cdot n ) ) \mathrm{\ for\ } n = [0,1]$
  • $h := h + V \cdot 1$

Essentially the values are different if we break up the calculation into more steps.

We need the values to be the same in both cases. We cannot do this with these formulas so I was wondering if there is another way to do this. I understand that my explanation might be a little difficult to understand but it's not very easy for me to explain this!

Thank you

2 Answers 2

3

You can use the differential equations to get an exact solution. You have

$x'=C_1h$ where the prime is the derivative with respect to time

$h'=-C_2t$ Then

$h=h(0)-C_2t$

$x=x(0)+C_1h(0)t-\frac 12 C_2 t^2$

In your case $x(0)=0,\ h(0)=1$, so

$h=1-C_2t$

$x=C_1t-\frac 12 C_2 t^2$

  • 0
    Thank you very much for respond$i$ng. Please look at my answer below...2012-07-04
0

Thanks a lot for responding. These look like the equations of motion ( they are the equations of motion! ) and this makes sense. However this is also an approximation isn't it? If you calculate a value by adding a time unit to $t$ each time then you will come up with a different number than if you calculated the value of $x$ by using a big amount of $t$.

In our case $t$ is in milliseconds so if we calculate $x$ after 3000 milliseconds we will get a different value than if we calculated $x$ after 1000 milliseconds, then another 1000 and then another 1000.

Try it out if you will with $C_1 = 0.001$ and $C_2 = 0.0001$.

The result for $t = 3000$ is $x = -147.3$ if we calculate it like so:

$x(3000) = x(0) + x(1000) + x(2000) + C_1h(2000)*1000−0.5*C_2 * 1000 ^ 2 $

and $x=-447$ if we calculate it like so:

$x(3000) = x(0) + C_1h(0)*3000−0.5*C_2 * 3000 ^ 2$

However the values converge if we make $C_2$ smaller and smaller. So is the solution to just use small values for $C_2$ ?

  • 0
    Hey Ross this last formula solves my problem! I was making the mistake of not calculating the quadratic term correctly that's why my results were different if there were timesteps in the mix.2012-07-05