<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <!-- Created by texi2html 1.76 --> <!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others. Maintained by: Many creative people <dev@texi2html.cvshome.org> Send bugs and suggestions to <users@texi2html.cvshome.org> --> <head> <title>Crystal Space 1.2.1: 4.14.2 Physics using ODE</title> <meta name="description" content="Crystal Space 1.2.1: 4.14.2 Physics using ODE"> <meta name="keywords" content="Crystal Space 1.2.1: 4.14.2 Physics using ODE"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="texi2html 1.76"> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} pre.display {font-family: serif} pre.format {font-family: serif} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: serif; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: serif; font-size: smaller} pre.smalllisp {font-size: smaller} span.sansserif {font-family:sans-serif; font-weight:normal;} ul.toc {list-style: none} --> </style> </head> <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> <a name="Physics-ODE"></a> <a name="0"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="Basic-Collision-Detection.html#0" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="Animation.html#0" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="Using-Crystal-Space.html#0" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="Collision-Detection.html#0" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Working-with-Engine-Content.html#0" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="index.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="cs_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="cs_Index.html#0" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="cs_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <hr size="1"> <h3 class="subsection"> 4.14.2 Physics using ODE </h3> <p>The <samp>‘odedynam’</samp> plugin implements physics for Crystal Space using the <small>ODE</small> library. </p> <a name="1"></a> <h4 class="subsubheading"> Doing the Timed Physics Simulation </h4> <p>To make sure the physics simulation proceeds correctly in time you must tell <small>ODE</small> how much time has elapsed since previous frame so that it can calculate the collisions and physical interactions that happened during that time. The <code>iDynamics->Step()</code> function is responsable for that. As a parameter it has a delta which is a floating point number that represents the number of seconds to calculate. You should not make this number too big because that will make the calculations less accurate. A good number for accurate calculation is around <samp>‘0.01’</samp>. <samp>‘0.02’</samp> is also good and makes calculations a bit faster. However, what should you do if the elapsed number of seconds is bigger then that number? In that case you should divide the elapsed time with the delta and perform that number of steps one by one. It is also very important to make sure that the delta you pass to the <code>Step()</code> function is constant. If this number is variable then <small>ODE</small> will not behave well and you get stability errors. Here below I present a possible code snippet that will ensure that the delta is constant and also makes sure that no time is lost so that the speed of physics simulation is constant even with variable framerate: </p> <table><tr><td> </td><td><pre class="example">csRef<iVirtualClock> vc = ...; csRef<iDynamics> dynamics = ...; float delta = 0.01f; float remaining_seconds = 0.0f; ... void ProcessPhysicsEveryFrame () { csTicks elapsed_time = vc->GetElapsedTicks (); float seconds = float (elapsed_time) / 1000.0f; seconds += remaining_seconds; while (seconds >= delta) { ProcessForces (delta); dynamics->Step (delta); seconds -= delta; } remaining_seconds = seconds; } </pre></td></tr></table> <p>This snippet of code will make sure that <code>Step</code> is only called with a constant delta. If there is a small time left after the loop (less then delta) then that will be remembered (in <samp>‘remaining_seconds’</samp>) for the next frame. </p> <hr size="1"> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="Basic-Collision-Detection.html#0" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="Animation.html#0" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="Using-Crystal-Space.html#0" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="Collision-Detection.html#0" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Working-with-Engine-Content.html#0" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="index.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="cs_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="cs_Index.html#0" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="cs_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <p> <font size="-1"> This document was generated using <a href="http://texi2html.cvshome.org/"><em>texi2html 1.76</em></a>. </font> <br> </p> </body> </html>