From: Markus R. <rol...@us...> - 2007-03-31 13:34:41
|
Update of /cvsroot/simspark/simspark/spark/oxygen/sceneserver In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv31055 Modified Files: fpscontroller.cpp fpscontroller.h Log Message: - added method UpdateStatic() that updates the managed body statically, i.e. without using ODE. This is useful to move an associated body while the simulation is paused - added method NeedStaticUpdate() to query if a call to UpdateStatic() would change the state of the managed body - factored setup of rotation matirx and forward vector out into method PrepareUpdate() - added members mHAngleDelta and mVAngleDelta that accumulate the applied angle deltas since the last update of the controller Index: fpscontroller.h =================================================================== RCS file: /cvsroot/simspark/simspark/spark/oxygen/sceneserver/fpscontroller.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** fpscontroller.h 15 Mar 2007 07:26:28 -0000 1.2 --- fpscontroller.h 31 Mar 2007 13:23:30 -0000 1.3 *************** *** 80,84 **** --- 80,100 ---- float GetAcceleration() const; + /** updates the managed body statically, i.e. without using the + dynamics engine; this is useful to move an associated camera + when the simulation is paused + */ + void UpdateStatic(float deltaTime); + + /** returns true if a call to UpdateStatic will alter the state of + the managed body + */ + bool NeedStaticUpdate() const; + protected: + /** sets up the rotation matrix and directio vector used to update + the managed body + */ + void PrepareUpdate(salt::Matrix& matrix, salt::Matrix& fwd, salt::Vector3f& vec); + /** calculates and applies the force needed to perfom the * activated movements */ *************** *** 95,101 **** --- 111,123 ---- float mHAngle; + /** applied horizontal delta since last update */ + float mHAngleDelta; + /** the current vertical angle in degrees */ float mVAngle; + /** applied horizontal delta since last update */ + float mVAngleDelta; + // event states Index: fpscontroller.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/oxygen/sceneserver/fpscontroller.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** fpscontroller.cpp 15 Mar 2007 07:26:28 -0000 1.2 --- fpscontroller.cpp 31 Mar 2007 13:23:30 -0000 1.3 *************** *** 27,34 **** --- 27,44 ---- using namespace salt; + // define some magic number here for the static fps controller update + // (while the simulation is paused). These should be made + // configurable.n + static const float STATIC_MIN_ANGLE_DELTA = 0.1f; + static const float STATIC_MIN_VELOCITY = 1e-3; + static const float STATIC_VELOCITY_DECAY = 0.8f; + static const float STATIC_ACCELERATION = 0.01f; + FPSController::FPSController() : BodyController() { mHAngle = 0.0f; + mHAngleDelta = 0.0f; mVAngle = 0.0f; + mVAngleDelta = 0.0f; mForward = false; mBackward = false; *************** *** 44,56 **** } ! void FPSController::PrePhysicsUpdateInternal(float /*deltaTime*/) { - if (mBody.get() == 0) - { - return; - } - // determine force direction ! Vector3f vec(0.0f,0.0f,0.0f); if (mForward) vec.y() += 1.0f; --- 54,61 ---- } ! void FPSController::PrepareUpdate(Matrix& matrix, Matrix& fwd, Vector3f& vec) { // determine force direction ! vec = Vector3f(0.0f,0.0f,0.0f); if (mForward) vec.y() += 1.0f; *************** *** 70,78 **** } ! Matrix matrix; float hAngle = gDegToRad(-mHAngle); float vAngle = gDegToRad(-mVAngle); matrix.RotationZ(hAngle); matrix.RotateX(vAngle); mBody->SetRotation(matrix); --- 75,100 ---- } ! matrix.Identity(); float hAngle = gDegToRad(-mHAngle); float vAngle = gDegToRad(-mVAngle); matrix.RotationZ(hAngle); matrix.RotateX(vAngle); + + fwd.Identity(); + fwd.RotationZ(hAngle); + } + + void FPSController::PrePhysicsUpdateInternal(float /*deltaTime*/) + { + if (mBody.get() == 0) + { + return; + } + + Matrix matrix; + Matrix fwd; + Vector3f vec; + PrepareUpdate(matrix, fwd, vec); + mBody->SetRotation(matrix); *************** *** 82,95 **** vec *= force; - Matrix fwd; - fwd.RotationZ(hAngle); mBody->AddForce(vec.y() * fwd.Up()); - - Matrix side; - side.RotationX(vAngle); mBody->AddForce(vec.x() * fwd.Right()); - mBody->AddForce(vec.z() * Vector3f(0,0,1)); } } --- 104,174 ---- vec *= force; mBody->AddForce(vec.y() * fwd.Up()); mBody->AddForce(vec.x() * fwd.Right()); mBody->AddForce(vec.z() * Vector3f(0,0,1)); } + + mHAngleDelta = 0.0; + mVAngleDelta = 0.0; + } + + bool FPSController::NeedStaticUpdate() const + { + if (mBody.get() == 0) + { + return false; + } + + return ( + (mForward) || + (mBackward) || + (mRight) || + (mLeft) || + (mUp) || + (mDown) || + (gAbs(mHAngleDelta) >= STATIC_MIN_ANGLE_DELTA) || + (gAbs(mVAngleDelta) >= STATIC_MIN_ANGLE_DELTA) || + (mBody->GetVelocity().Length() > STATIC_MIN_VELOCITY) + ); + } + + void FPSController::UpdateStatic(float deltaTime) + { + if (mBody.get() == 0) + { + return; + } + + Matrix matrix; + Matrix fwd; + Vector3f vec; + PrepareUpdate(matrix, fwd, vec); + + mBody->SetRotation(matrix); + + Vector3f bodyVel = mBody->GetVelocity() + (deltaTime * vec * STATIC_ACCELERATION); + bodyVel *= STATIC_VELOCITY_DECAY; + + Vector3f velocity = + vec.y() * fwd.Up() + + vec.x() * fwd.Right() + + vec.z() * Vector3f(0,0,1); + + matrix.Pos() = mBody->GetPosition() + velocity * bodyVel.Length(); + + mBody->SetPosition(matrix.Pos()); + mBody->SetVelocity(velocity); + + shared_ptr<BaseNode> bodyParent = shared_static_cast<BaseNode> + (make_shared(mBody->GetParent())); + + if (bodyParent.get() != 0) + { + mBody->SynchronizeParent(); + bodyParent->UpdateHierarchy(); + } + + mHAngleDelta = 0.0; + mVAngleDelta = 0.0; } *************** *** 97,100 **** --- 176,180 ---- { mHAngle += delta; + mHAngleDelta += delta; } *************** *** 102,105 **** --- 182,186 ---- { mVAngle += delta; + mVAngleDelta += delta; } *************** *** 107,110 **** --- 188,192 ---- { mHAngle = angleDeg; + mHAngleDelta = 0.0; } *************** *** 112,115 **** --- 194,198 ---- { mVAngle = angleDeg; + mVAngleDelta = 0.0; } |