rec.autos.simulators

Car Physics : Constant Timestep

Ashley McConnel

Car Physics : Constant Timestep

by Ashley McConnel » Sun, 30 Dec 2001 19:22:20

Hi Physics blokes,

I am currently messing about with some rigid body stuff referenced from
Ruud's code (Richard Chaney), problem is that what I have been doing so far
is getting the timestep from the difference between the last update and the
current one.  This seems to suggest that it is better to have a constant
timestep.  How can I time this accurately so that the physics is only
updated after x millisecs?  Also, how do i make sure that there is useful
work being done when it isnt time for the physics to be updated.

Thanks guys!

All the best,
Ash

PS. Happy New Year!

Jens H. Kruus

Car Physics : Constant Timestep

by Jens H. Kruus » Sun, 30 Dec 2001 19:26:07



Wouldn't it be possible to have your physics-thread high priority (so that it
will not miss a calculation) and just let it sleep when not needed?

/Jens

Ashley McConnel

Car Physics : Constant Timestep

by Ashley McConnel » Sun, 30 Dec 2001 20:02:49

Ahh Multithreading :).  I was wondering if that was the way to go.  I havent
done that sort of thing before (except at college in Java).  Will need to
look into it.

How should it be timed - the QueryperformanceCounter functions?

Thanks
All the best,
Ash

Petri Blomqvis

Car Physics : Constant Timestep

by Petri Blomqvis » Mon, 31 Dec 2001 05:51:19


Here's roughly the way I've implemented a constant timestep simulation:

timeStep = 0.002; // Or whatever you wish to use
t = getTime();
do
{
  while (t < getTime())
  {
    updatePhysics(timeStep);
    t += timeStep;
  }
  drawFrame();

In other words, run the simulation with a constant timestep until it catches
up with real time, then draw your graphics, and repeat.

This way, you get a constant timestep, the simulation will be in sync with
real time when it's time to draw the frame, and you're always either
updating the physics or drawing the graphics (and whatever else you add into
the main loop). The only problem is if your physics calculations are too
slow to be done in realtime, it'll get stuck into the physics loop, trying
in vain to catch up to real time. But that's a problem with any fixed
timestep simulation. Just check if the code stays too long inside the
physics loop (like half a second), and handle that as you wish (like switch
to a larger timestep, or throw an exception that tells the user it's time
for a CPU upgrade ;-) ).

Definitely!

Petri Blomqvist

Jens H. Kruus

Car Physics : Constant Timestep

by Jens H. Kruus » Mon, 31 Dec 2001 07:00:10

Petri,

(Caveat: I haven't done any programming simulations in a loooong time).

It seems to me, that your method would result in some bad prioritizing of
resources. If the drawFrame() bit (contains AI cars etc.) is significantly
slower than the physics, you will have a staggered simulation, where you are
either doing everything else or updating your physics repeatedly. In other
words, why not do the physics as needed and "draw" around those calculations?

Hope I make myself clear here. ;-)

/Jens





> > Hi Physics blokes,

> > I am currently messing about with some rigid body stuff referenced from
> > Ruud's code (Richard Chaney), problem is that what I have been doing so
> far
> > is getting the timestep from the difference between the last update and
> the
> > current one.  This seems to suggest that it is better to have a constant
> > timestep.  How can I time this accurately so that the physics is only
> > updated after x millisecs?  Also, how do i make sure that there is useful
> > work being done when it isnt time for the physics to be updated.

> Here's roughly the way I've implemented a constant timestep simulation:

> timeStep = 0.002; // Or whatever you wish to use
> t = getTime();
> do
> {
>   while (t < getTime())
>   {
>     updatePhysics(timeStep);
>     t += timeStep;
>   }
>   drawFrame();
> } while (!done);

> In other words, run the simulation with a constant timestep until it catches
> up with real time, then draw your graphics, and repeat.

> This way, you get a constant timestep, the simulation will be in sync with
> real time when it's time to draw the frame, and you're always either
> updating the physics or drawing the graphics (and whatever else you add into
> the main loop). The only problem is if your physics calculations are too
> slow to be done in realtime, it'll get stuck into the physics loop, trying
> in vain to catch up to real time. But that's a problem with any fixed
> timestep simulation. Just check if the code stays too long inside the
> physics loop (like half a second), and handle that as you wish (like switch
> to a larger timestep, or throw an exception that tells the user it's time
> for a CPU upgrade ;-) ).

> > How should it be timed - the QueryperformanceCounter functions?

> Definitely!

> Petri Blomqvist

Petri Blomqvis

Car Physics : Constant Timestep

by Petri Blomqvis » Mon, 31 Dec 2001 07:51:56



Ummm... yes, you do. :-) But I don't see why the staggered simulation would
be a problem, as the simulation only needs to coincide with realtime when
you draw the graphics (or does network play require some other way?). I'd be
happy to learn other ways of handling it though (multithreading?)

Petri

Paul Laidla

Car Physics : Constant Timestep

by Paul Laidla » Mon, 31 Dec 2001 10:53:17




> > Petri,

> > (Caveat: I haven't done any programming simulations in a loooong time).

> > It seems to me, that your method would result in some bad prioritizing
of
> > resources. If the drawFrame() bit (contains AI cars etc.) is
significantly
> > slower than the physics, you will have a staggered simulation, where you
> are
> > either doing everything else or updating your physics repeatedly. In
other
> > words, why not do the physics as needed and "draw" around those
> calculations?

> > Hope I make myself clear here. ;-)

> Ummm... yes, you do. :-) But I don't see why the staggered simulation
would
> be a problem, as the simulation only needs to coincide with realtime when
> you draw the graphics (or does network play require some other way?). I'd
be
> happy to learn other ways of handling it though (multithreading?)

It needs to coincide with realtime when you read the controller.

Long time since I've programmed windoze code but I believe u use a
multimedia timer
for this, if I remember correctly.

You may get away with simulating the computer cars like this however.

    Paul

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.303 / Virus Database: 164 - Release Date: 24/11/01

Petri Blomqvis

Car Physics : Constant Timestep

by Petri Blomqvis » Mon, 31 Dec 2001 19:00:49


I read the controller once per animation frame, and I update sound once per
animation frame, so that's not a problem.

You could, but the performance counter has a better resolution.

I don't have any AI cars in my sim yet, and this method works perfectly for
the player's car. Too bad the sim isn't in a releasable condition yet (no
configuration options or error checking), otherwise you could try it for
yourself and see that it works. :-)

Petri

Ashley McConnel

Car Physics : Constant Timestep

by Ashley McConnel » Mon, 31 Dec 2001 22:29:00

Thanks for the replies guys :)

Its good to get a discussion on this sort of thing going, helps all the
sim-programmers out there :)

Jens, could you elaborate a little on your physics thread method?

Thanks alot
Ash

alex

Car Physics : Constant Timestep

by alex » Tue, 01 Jan 2002 00:18:56




>> Hi Physics blokes,

>> I am currently messing about with some rigid body stuff referenced
>> from Ruud's code (Richard Chaney), problem is that what I have been
>> doing so far is getting the timestep from the difference between the
>> last update and the current one.  This seems to suggest that it is
>> better to have a constant timestep.  How can I time this accurately so
>> that the physics is only updated after x millisecs?  Also, how do i
>> make sure that there is useful work being done when it isnt time for
>> the physics to be updated.

> Here's roughly the way I've implemented a constant timestep simulation:

> timeStep = 0.002; // Or whatever you wish to use
> t = getTime();
> do
> {
>   while (t < getTime())
>   {
>     updatePhysics(timeStep);
>     t += timeStep;
>   }
>   drawFrame();
> } while (!done);

> In other words, run the simulation with a constant timestep until it
> catches up with real time, then draw your graphics, and repeat.

> This way, you get a constant timestep, the simulation will be in sync
> with real time when it's time to draw the frame, and you're always
> either updating the physics or drawing the graphics (and whatever else
> you add into the main loop). The only problem is if your physics
> calculations are too slow to be done in realtime, it'll get stuck into
> the physics loop, trying in vain to catch up to real time. But that's a
> problem with any fixed timestep simulation. Just check if the code
> stays too long inside the physics loop (like half a second), and handle
> that as you wish (like switch to a larger timestep, or throw an
> exception that tells the user it's time for a CPU upgrade ;-) ).

>> How should it be timed - the QueryperformanceCounter functions?

> Definitely!

> Petri Blomqvist

I would argue for varying step updates, meaning that physics
should be calculated as functional
F(t0, t1): {car1, ... carN, input1, ..., inputN } -> { car1~, ... carN~}
Here's why:
- it will make your physics more efficient, because otherwise
you might have had to call fixed step increment several times.
- more important this approach will give your calculation time
that is not proportional to the duration of the time passed.
This will protect you against hopelessly lagging behind whatever
computer (or rather Windows) did at the moment.
- this does not complicate the code, because in any case you
need to have your algorithms working using dt=t1-t0.
- in the network code you will need to be able to have this
functional to be able to recalculate cars position basing on
the received input from remote computer. In regular physics updates
you recalculate using input prediction algorithm instead of missing
part of the input, when you get input for the particular car,
you recalculate car position from the using that functional from
the last known state. This will prevent cheating as the server
will be responsible for physics handling.

If you're thinking about threads, from my experience threads
on Windows (especially Win9x) are not scheduled very good when
you have intensive processing going on. Though threads for
polling should be fine (polling controller input and posting it
for the main thread, same for polling for network events).

Alex.

Jens H. Kruus

Car Physics : Constant Timestep

by Jens H. Kruus » Tue, 01 Jan 2002 01:12:52



Good point about Windows and threads. I'm thinking a lot about network code at
the present time which could explain my infatuation with multi-threading. :-)

I can see how using a variable delta-t instead of a constant would make a lot of
sense, assuming that the delta-t is significantly less than whatever (converging
or diverging) oscilations are taking place in the mechanical bits. Insuring that
could be interesting as it must take presendence over eg. graphics.

/Jens

Petri Blomqvis

Car Physics : Constant Timestep

by Petri Blomqvis » Tue, 01 Jan 2002 02:38:13


Efficient, yes, but potentially also inaccurate and unstable, unless you're
using simple 'canned' physics instead of an actual rigid body simulation. If
your graphics are being updated at 60 FPS, your physics will also be updated
at 60 Hz or less, and that's far from sufficient.

However, it will give you an accuracy and stability that IS dependent on the
timestep.

Note that I'm talking about a solution that doesn't use multithreading -
using multithreading is of course a different case altogether, and one I
have no experience with. If the physics thread is capable of carrying on
updates while the graphics are being drawn, then the timesteps might remain
sufficiently small. But how do you guarantee that?

Petri Blomqvist

alex

Car Physics : Constant Timestep

by alex » Tue, 01 Jan 2002 06:08:12




>> I would argue for varying step updates, meaning that physics
>> should be calculated as functional
>> F(t0, t1): {car1, ... carN, input1, ..., inputN } -> { car1~, ...
>> carN~} Here's why:
>> - it will make your physics more efficient, because otherwise
>> you might have had to call fixed step increment several times.

> Efficient, yes, but potentially also inaccurate and unstable, unless
> you're using simple 'canned' physics instead of an actual rigid body
> simulation. If your graphics are being updated at 60 FPS, your physics
> will also be updated at 60 Hz or less, and that's far from sufficient.

I understand what you're taking is the step of the differential net
(not sure I am giving correct translation of the term, that's a method
of approximation of continuous process with discrete one) and not
periodicity of updates. If you're capable of resolving your model
analytically, you won't have inaccuracies at all (except caused
by floating point calculations). Example: to calculate position of
the linearly moving point you don't need to calculate it at t1, t2, .. tN,
you can calculate it exactly at tN directly.
In car physics simulation you are not likely to have complete
analytical solution (equations are too complex), however you may
use analytical solutions in some parts, in other parts you have to
calculate approximately you may use differentials net with various
steps as well as other approximate methods.

Stability is only applicable to approximate methods and has
nothing to do with the frequence of updates. Strictly speaking
instability is only an issue if you're going to implement AI
drivers which will act as human (provide input and observe results).
Otherwise,  it is not a control system.
What you are more interested in is convergence and precision.
The faster your approximate methods converge, faster you will get
your update made. This factor more depends on the choice of
approximation method. Precision is influenced by the step of the
differential net and by the approximation method selected.
In principle, multithreading is not related with the issues covered
above. I start to believe that you actually trying to use approximation
method based on discretisation(spelling?) of time and synchronize the
step of the net with the other components of the program.

Let's say we're considering only one car driven by human. To display
current state to the driver you need to update physics first to
learn this state. Drawing will take some time and as long as you
can calculate next car state from the previous one you don't need
to know intermediate states. This can be done as multi-thread which
I don't think is an efficient solution under Windows, or you can make
it from one thread in a loop like:

        while (true)
        {
                t = currentTime();
                nextState = updatePhysics(currentState,driverInput);
                currentState = nextState;
                displayState(currentState);
        }

One reason why you may need another thread is to collect driver input
while you're updating physics and drawing screen. I am not sure
how noticable is it, but if for example, driver presses throttle
smoothly or abruptly, it should be taken into account by your
simulation, which may not happen if you're reading controllers only
once per update.

Alex.

Petri Blomqvis

Car Physics : Constant Timestep

by Petri Blomqvis » Tue, 01 Jan 2002 08:30:23


Umm... possibly, but I'm not sure I understand what you're saying here. :-)

True, but I can't think of any part of the equations of motion of a rigid
body that could be solved analytically under randomly varying forces, so
numerical integration would seem to be necessary.

All the numerical integration methods I know of (euler, midstep, 4th-order
runge-kutta, etc.) depend on a sufficiently small timestep for stability. If
I have a tire in my simulation that has a vertical stiffness of 500,000 N/m,
it'll very easily start to oscillate and eventually fly all the way to
Jupiter and back, if the timestep doesn't stay sufficiently small, so
stability definitely has everything to do with how often I update the
physics! Or maybe I'm misunderstanding what you're saying.

Are you saying there's some sort of iterative way to integrate the equations
of motion? Does that have something to do with implicit integration? That's
something I'll have to look into, I guess.

Could be, but I'm still not sure I understand what you mean by the "net" in
this context. :-)

The problem is, how do I calculate the next car state from the previous one
without those intermediate states, without inducing instability? Right now I
calculate the physics at a fixed 500 Hz - if there really is some way to
calculate the physics at rates as low as 30 Hz so that it remains stable, I
definitely want to learn it! :-)

Hmm. I guess that could be an issue.

Petri Blomqvist

Paul Laidla

Car Physics : Constant Timestep

by Paul Laidla » Tue, 01 Jan 2002 09:55:07

And if your frame rate is bad? or variable?

It doesn't generate an interrupt however........

You can do it the way you say, and it does work, but Jens has a valid point,
it is swings and roundabouts.

    Paul

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.303 / Virus Database: 164 - Release Date: 25/11/01


rec.autos.simulators is a usenet newsgroup formed in December, 1993. As this group was always unmoderated there may be some spam or off topic articles included. Some links do point back to racesimcentral.net as we could not validate the original address. Please report any pages that you believe warrant deletion from this archive (include the link in your email). RaceSimCentral.net is in no way responsible and does not endorse any of the content herein.