示例#1
0
// per frame simulation update
void LowSpeedTurn::update(const float currentTime, const float elapsedTime){
  // apply force to vehicle
  applySteeringForce(steering(), elapsedTime);
  // update Irrlicht node
  setPosition(position().vector3df());
  irr::scene::ISceneNode::setRotation(forward().vector3df().getHorizontalAngle());
  // update motion trail
  recordTrailVertex(currentTime, position());
}
	//-----------------------------------------------------------------------------
	// per frame simulation update
	void PhysicsVehicle::update( const float currentTime, const float elapsedTime )
	{
		// prevent any updates with zero or negative delta time

		// prevent any updates where the elapsed time is > 5 sec
		// this might be a result of debugging
		// or is plainly very very bad

		if( ( elapsedTime <= 0.0f )	|| ( elapsedTime > 5.0f ) ) {
			return;
		}

		bool ticked(false);

		// just update the tick structure
		_physicsUpdateTick.setAnimationInterval(getPhysicsUpdateFrameTime());
		SLSize ticks(_physicsUpdateTick.update(elapsedTime, ticked));
		if(ticked == false)	{
			// just do nothing
			// a feature is missing here ...
			// state interpolation
			return;
		}

		// now read the internal motion state in case needed
		if(_dirtyLocalSpaceData)	{
			_internalMotionState.readLocalSpaceData(*this);
			_dirtyLocalSpaceData = false;
		}

		// set the updateTick counter to the right value
		// this is a very interesting value for networking
		// assuming the physics frame rate does not change

		_updateTicks = ticks;

		// following code should be stable enough to change the physics frame rate during runtime

		const float deltaTime(static_cast<float>(_physicsUpdateTick.getAnimationInterval()));

		SLSize deltaTicks(ticks - _physicsUpdateTick.getLastAnimationTick());
		float tickDeltaTime(static_cast<float>(deltaTicks) * deltaTime);

		float accumTime(static_cast<float>(_physicsUpdateTick.getTickedAccumTime()));
		accumTime -= tickDeltaTime;

		for(SLSize i(0); i < deltaTicks; ++i)	{
			updateTick(accumTime, deltaTime);
			accumTime += deltaTime;
		}

		// pass over the internal 'fixed frame time' motion state to the 'render state'
		_motionState = _internalMotionState;

		SLTimeInterval missedDeltaTime(_physicsUpdateTick.getAccumTime() - _physicsUpdateTick.getTickedAccumTime());
		if(missedDeltaTime != 0.0)	{
#if 0
			printf("Missed dt(%f)\n", missedDeltaTime);
#endif
			// integrate with given linear and angular velocity
			_motionState.integrateMotionState(static_cast<float>(missedDeltaTime));
		}
		
		// and finally write the motion state back to the local space
		_motionState.writeLocalSpaceData(*this);

		if( _proximityToken != NULL )	{
			// notify proximity database that our position has changed
			_proximityToken->updateForNewPosition( position() );
		}

		// annotation
		annotationVelocityAcceleration (5, 0);
		recordTrailVertex(static_cast<float>(_physicsUpdateTick.getAccumTime()), position());
	}