void dNewton::ResetTimer() { #ifdef _MSC_VER LARGE_INTEGER baseCount; LARGE_INTEGER frequency; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter (&baseCount); m_baseCount = dLong (baseCount.QuadPart); m_frequency = dLong (frequency.QuadPart); #endif /* #if (defined (_POSIX_VER) || defined (_POSIX_VER_64)) timespec ts; clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux //baseCount = ts.tv_nsec / 1000; baseCount = unsigned64 (ts.tv_sec) * 1000000 + ts.tv_nsec / 1000; #endif #ifdef _MACOSX_VER timeval tp; gettimeofday(&tp, NULL); unsigned64 microsecunds = unsigned64 (tp.tv_sec) * 1000000 + tp.tv_usec; baseCount = microsecunds; #endif */ m_microseconds = GetTimeInMicrosenconds(); }
void OgreNewtonWorld::Update () { //NewtonSerializeToFile (GetNewton(), "xxxx.bin"); dLong lastTime = m_lastPhysicTimeInMicroseconds; m_lastPhysicTimeInMicroseconds = GetTimeInMicrosenconds (); Real applicationTime = Real (m_lastPhysicTimeInMicroseconds - lastTime) * 1.0e-6f; if (m_concurrentUpdateMode) { dNewton::UpdateAsync (m_timestep); } else { dNewton::Update (m_timestep); } dFloat param = GetInterpolationParam(m_timestep); dAssert (applicationTime > 0.0f); OnNodesTransformBegin (param); // iterate over all physics bodies and get the tranformtaion matrix; for (dNewtonBody* body = GetFirstBody(); body; body = GetNextBody(body)) { SceneNode* const node = (SceneNode*) body->GetUserData(); if (node) { dMatrix matrix; body->GetVisualMatrix (param, &matrix[0][0]); dQuaternion rot (matrix); Vector3 posit (matrix.m_posit.m_x, matrix.m_posit.m_y, matrix.m_posit.m_z); Quaternion rotation (rot.m_q0, rot.m_q1, rot.m_q2, rot.m_q3); node->setPosition (posit); node->setOrientation (rotation); // update the application user data (that need to be update at rendering time, ex animations, particles emmitions, etc) body->OnApplicationPostTransform (applicationTime); } } OnNodesTransformEnd (param); m_physicUpdateTimestepInMocroseconds = GetTimeInMicrosenconds () - m_lastPhysicTimeInMicroseconds; }
int main (int argc, char* argv[]) { _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF)); // initialize graphics system if (!InitGraphicsSystem (800, 600)) { exit (0); } // set a callback for destroying the world ate termination atexit (ShutDown); // Create a Simple Scene Manager g_sceneManager = new SceneManager(); // set the memory allocators NewtonSetMemorySystem (AllocMemory, FreeMemory); // create the Newton World g_world = NewtonCreate (); // use the standard x87 floating point model NewtonSetPlatformArchitecture (g_world, 0); // set a fix world size // dVector minSize (-500.0f, -500.0f, -500.0f); // dVector maxSize ( 500.0f, 500.0f, 500.0f); // NewtonSetWorldSize (g_world, &minSize[0], &maxSize[0]); // initialize Newton Visual Debugger #ifdef USE_VISUAL_DEBUGGER g_newtonDebugger = NewtonDebuggerCreateServer (); #endif // configure the Newton world to use iterative solve mode 0 // this is the most efficient but the less accurate mode NewtonSetSolverModel (g_world, 1); // now populate the world with Graphic and physical entities CreateScene(g_world, g_sceneManager); // run the main application loop until the user terminate the app for (;;) { // Draw the screen. AdvanceSimulation (GetTimeInMicrosenconds ()); } // Never reached. return 0; }
void dNewton::UpdateAsync (dFloat timestepInSecunds) { dLong timestepMicrosecunds = (dLong) (double (timestepInSecunds) * 1000000.0f); dLong currentTime = GetTimeInMicrosenconds (); dLong nextTime = currentTime - m_microseconds; int loops = 0; while ((nextTime >= timestepMicrosecunds) && (loops < m_maxUpdatePerIterations)) { loops ++; NewtonUpdateAsync (m_world, timestepInSecunds); nextTime -= timestepMicrosecunds; m_microseconds += timestepMicrosecunds; } }
OgreNewtonWorld::OgreNewtonWorld (int updateFramerate) :dNewton() ,m_materialMap() ,m_gravity (0.0f, -9.8f, 0.0f) ,m_concurrentUpdateMode(true) ,m_lastPhysicTimeInMicroseconds(GetTimeInMicrosenconds ()) ,m_physicUpdateTimestepInMocroseconds(0) { SetUpdateFPS (Real (updateFramerate), 3); // add some of the essential managers, in order of execution m_triggerManager = new OgreNewtonTriggerManager(this); m_localTransformManager = new OgreNewtonArticulationManager (this); m_playerManager = new OgreNewtonPlayerManager (this); m_rayPickerManager = new OgreNewtonRayPickManager (this, 0); m_inputManager = new OgreNewtonInputManager(this); // when debugging it is good to set thread count to 1 //SetNumberOfThreads(1); }
void AdvanceSimulation (int timeInMilisecunds) { // do the physics simulation here int deltaTime; int physicLoopsTimeAcc; dFloat fps; dFloat physicTime; // get the time step deltaTime = timeInMilisecunds - g_currentTime; g_currentTime = timeInMilisecunds; g_timeAccumulator += deltaTime; physicTime = 0; // advance the simulation at a fix step int loops = 0; physicLoopsTimeAcc = 0; while ((loops < MAX_PHYSICS_LOOPS) && (g_timeAccumulator >= DEMO_FPS_IN_MICROSECUNDS)) { loops ++; // Process incoming events. ProcessEvents (g_world); // sample time before the Update g_physicTime = GetTimeInMicrosenconds (); // run the newton update function NewtonUpdate (g_world, (1.0f / DEMO_PHYSICS_FPS)); // calculate the time spent in the physical Simulation g_physicTime = GetTimeInMicrosenconds () - g_physicTime; // call the visual debugger to show the physics scene #ifdef USE_VISUAL_DEBUGGER NewtonDebuggerServe (g_newtonDebugger, g_world); #endif // subtract time from time accumulator g_timeAccumulator -= DEMO_FPS_IN_MICROSECUNDS; physicTime ++; physicLoopsTimeAcc += g_physicTime; } if (loops > MAX_PHYSICS_LOOPS) { g_physicTime = physicLoopsTimeAcc; g_timeAccumulator = DEMO_FPS_IN_MICROSECUNDS; } // Clear the color and depth buffers. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // calculate the interpolation parameter for smooth rendering g_sceneManager->SetIntepolationParam(dFloat (g_timeAccumulator) / dFloat(DEMO_FPS_IN_MICROSECUNDS)); // do the rendering here entire Scene g_sceneManager->Render (); // display the frame rate // smooth the fps by averaging the last few rendering time steps; int dtAcc = 0; static int fpsIndex; static long int smoothFPS[128]; smoothFPS[fpsIndex] = deltaTime; fpsIndex = (fpsIndex + 1) % (sizeof (smoothFPS) / sizeof (smoothFPS[0])); for (int i = 0; i < (sizeof (smoothFPS) / sizeof (smoothFPS[0])); i ++) { dtAcc += smoothFPS[i]; } dtAcc /= (sizeof (smoothFPS) / sizeof (smoothFPS[0])); fps = 1000000.0f / dFloat (dtAcc); physicTime = g_physicTime * dFloat(1000.0f / 1000000.0f); Print (dVector (1.0f, 1.0f, 0.0f, 0.0f), 10, 10, "fps: %6.2f", fps); Print (dVector (1.0f, 1.0f, 0.0f, 0.0f), 10, 22, "physic time (milliseconds): %5.2f", physicTime); // show GL rendered Scene glFlush(); SDL_GL_SwapBuffers( ); }
void dNewton::UpdateOffLine (dFloat timestepInSecunds) { NewtonUpdate (m_world, timestepInSecunds); m_microseconds += GetTimeInMicrosenconds (); }
dFloat dNewton::GetInterpolationParam(dFloat timestepInSecunds) const { dLong timeStep = GetTimeInMicrosenconds () - m_microseconds; dFloat param = (dFloat (timeStep) * 1.0e-6f) / timestepInSecunds; return dClamp (param, dFloat(0.0f), dFloat(1.0f)); }