void RigidBodyWorld::UpdatePhysics () { RigidBodyWorldDesc* const desc = (RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); float timestep = 1.0f / desc->m_minFps; if (timestep > 1.0f / 60.0f) { timestep = 1.0f / 60.0f; } desc->m_updateRigidBodyMatrix = false; NewtonUpdate(desc->m_newton, timestep); TimeValue t (GetCOREInterface()->GetTime()); float scale = 1.0f / float (GetMasterScale(UNITS_METERS)); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; body = NewtonWorldGetNextBody(desc->m_newton, body)) { dMatrix matrix; INode* const node = (INode*)NewtonBodyGetUserData(body); NewtonBodyGetMatrix(body, &matrix[0][0]); matrix = desc->m_systemMatrix * matrix * desc->m_systemMatrixInv; matrix.m_posit = matrix.m_posit.Scale (scale); Matrix3 maxMatrix (GetMatrixFromdMatrix (matrix)); node->SetNodeTM(t, maxMatrix); } UpdateViewPorts (); desc->m_updateRigidBodyMatrix = true; }
// DrawScene() void DrawScene () { dFloat timeStep; // get the time step timeStep = timer.GetElapsedSeconds(); // updtate the Newton physics world NewtonUpdate (nWorld, timeStep); // read the keyboard Keyboard (); // move the camera dVector target (cameraEyepoint + cameraDir); SetCamera (cameraEyepoint, target); // render the scene GraphicManager::Iterator iter (GraphicManager::GetManager()); for (iter.Begin(); iter; iter ++) { glPushMatrix(); iter.GetNode()->GetInfo()->Render(); glPopMatrix(); } }
void NzPhysWorld::Step(float timestep) { m_timestepAccumulator += timestep; while (m_timestepAccumulator >= m_stepSize) { NewtonUpdate(m_world, m_stepSize); m_timestepAccumulator -= m_stepSize; } }
void PhysWorld3D::Step(float timestep) { m_timestepAccumulator += timestep; std::size_t stepCount = 0; while (m_timestepAccumulator >= m_stepSize && stepCount < m_maxStepCount) { NewtonUpdate(m_world, m_stepSize); m_timestepAccumulator -= m_stepSize; stepCount++; } }
// update int World::update( Ogre::Real t_step ) { int realUpdates = 0; // clean up all pending bodies for update for( BodyVectorVector::iterator it = m_bodyUpdateNodeRequests.begin(); it != m_bodyUpdateNodeRequests.end(); it++ ) { for( BodyVector::iterator body = it->begin(); body != it->end(); body++ ) { (*body)->SetNodeUpdateState (false); } it->clear(); } #ifdef _DEBUG // Ogre::LogManager::getSingleton().logMessage(" Newton Frame Listener... m_elapsed: "+Ogre::StringConverter::toString( t_step)+ // " m_update:"+Ogre::StringConverter::toString(m_update)); #endif // clamp thE step if necessary if (t_step > (m_timestep * m_maxTicksPerFrames)) { t_step = m_timestep * m_maxTicksPerFrames; } // advance the accumulator; m_timeAcumulator += t_step; while (m_timeAcumulator >= m_timestep) { NewtonUpdate (m_world, m_timestep); m_timeAcumulator -= m_timestep; realUpdates++; } #ifdef _DEBUG // Ogre::LogManager::getSingleton().logMessage(" Newton updates this loop: "+Ogre::StringConverter::toString(count)); #endif Ogre::Real param = m_timeAcumulator * m_invTimestep; //param = 1.0f; for( BodyVectorVector::iterator it = m_bodyUpdateNodeRequests.begin(); it != m_bodyUpdateNodeRequests.end(); it++ ) { for( BodyVector::iterator body = it->begin(); body != it->end(); body++ ) { (*body)->updateNode(param); } } return realUpdates; }
void dNewton::Update (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 ++; NewtonUpdate (m_world, timestepInSecunds); nextTime -= timestepMicrosecunds; m_microseconds += timestepMicrosecunds; } }
void DemoEntityManager::UpdatePhysics(float timestep) { // update the physics if (m_world) { dFloat timestepInSecunds = 1.0f / MAX_PHYSICS_FPS; unsigned64 timestepMicrosecunds = unsigned64 (timestepInSecunds * 1000000.0f); unsigned64 currentTime = dGetTimeInMicrosenconds (); unsigned64 nextTime = currentTime - m_microsecunds; int loops = 0; while ((nextTime >= timestepMicrosecunds) && (loops < MAX_PHYSICS_LOOPS)) { loops ++; dTimeTrackerEvent(__FUNCTION__); // run the newton update function if (!m_reEntrantUpdate) { m_reEntrantUpdate = true; if (m_physicsUpdate && m_world) { ClearDebugDisplay(m_world); // update the physics world if (!m_mainWindow->m_physicsUpdateMode) { NewtonUpdate (m_world, timestepInSecunds); } else { NewtonUpdateAsync(m_world, timestepInSecunds); } } m_reEntrantUpdate = false; } nextTime -= timestepMicrosecunds; m_microsecunds += timestepMicrosecunds; } if (loops) { m_physicsTime = dFloat (dGetTimeInMicrosenconds () - currentTime) / 1000000.0f; if (m_physicsTime >= MAX_PHYSICS_LOOPS * (1.0f / MAX_PHYSICS_FPS)) { m_microsecunds = currentTime; } } } }
void Physics::update( float deltaTime ) { if ( !_p_world ) return; // Newton must be updated with fixed timesteps static float time_elapsed = 0.0f; time_elapsed += deltaTime; if ( time_elapsed > FIX_PHYSICS_UPDATE_PERIOD ) { do { NewtonUpdate( _p_world, FIX_PHYSICS_UPDATE_PERIOD ); time_elapsed -= FIX_PHYSICS_UPDATE_PERIOD; } while( time_elapsed > FIX_PHYSICS_UPDATE_PERIOD ); } }
void world::tick(real dt) { const real MIN_FPS = 10.0; if (dt > 1.0 / MIN_FPS) dt = 1.0 / MIN_FPS; _lastStep = 0; for (_accum += dt; _accum >= _freq; _accum -= _freq, _lastStep += _freq) NewtonUpdate(_world, _freq); // Iterate over each body and lerp it NewtonBody *_body = NewtonWorldGetFirstBody(_world); while (_body) { bodyCast(_body)->lerp(dt); _body = NewtonWorldGetNextBody(_world, _body); } }
void CEffectsGame::RunPhysics(float frametime) { NewtonUpdate(pWorld, frametime); }
bool PhysicMap::update() { NewtonUpdate(m_world, 0.01f); return (true); }
// update int World::update( Ogre::Real t_step ) { int realUpdates = 0; /* // clean up all pending bodies for update for( BodyVectorVector::iterator it = m_bodyUpdateNodeRequests.begin(); it != m_bodyUpdateNodeRequests.end(); it++ ) { for( BodyVector::iterator body = it->begin(); body != it->end(); body++ ) { (*body)->setNodeUpdateNeeded (false); } it->clear(); } */ #ifdef _DEBUG // Ogre::LogManager::getSingleton().logMessage(" Newton Frame Listener... m_elapsed: "+Ogre::StringConverter::toString( t_step)+ // " m_update:"+Ogre::StringConverter::toString(m_update)); #endif // clamp the step if necessary if (t_step > (m_timestep * m_maxTicksPerFrames)) { t_step = m_timestep * m_maxTicksPerFrames; } // advance the accumulator; m_timeAcumulator += t_step; while (m_timeAcumulator >= m_timestep) { NewtonUpdate (m_world, m_timestep); m_timeAcumulator -= m_timestep; realUpdates++; } #ifdef _DEBUG // Ogre::LogManager::getSingleton().logMessage(" Newton updates this loop: "+Ogre::StringConverter::toString(count)); #endif Ogre::Real param = m_timeAcumulator * m_invTimestep; //param = 1.0f; /* Julio's version, does not really work as the body is only updated once per transform-callback for( BodyVectorVector::iterator it = m_bodyUpdateNodeRequests.begin(); it != m_bodyUpdateNodeRequests.end(); it++ ) { for( BodyVector::iterator body = it->begin(); body != it->end(); body++ ) { (*body)->updateNode(param); } } */ for( Body* body = getFirstBody(); body; body = body->getNext() ) { body->updateNode(param); } return realUpdates; }
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( ); }
/* ============= CMod_PhysicsInit ============= */ void CMod_PhysicsUpdate( int time, float timestep ) { NewtonUpdate (g_world, timestep); }
void dNewton::UpdateOffLine (dFloat timestepInSecunds) { NewtonUpdate (m_world, timestepInSecunds); m_microseconds += GetTimeInMicrosenconds (); }