From 0b68953f0d7b4c60e83c2be4e3c3cb4715c0178c Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 18 Jan 2013 21:40:47 -0800 Subject: [PATCH] Scale animation speed using the direction length The direction length doesn't currently give a good speed, but it's something. --- apps/openmw/mwmechanics/character.cpp | 24 ++++++++++++++---------- apps/openmw/mwmechanics/character.hpp | 3 +-- apps/openmw/mwrender/animation.cpp | 2 ++ apps/openmw/mwrender/animation.hpp | 4 ++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index f3a1f2fba7..ad784d34fb 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -118,6 +118,17 @@ void CharacterController::markerEvent(float time, const std::string &evt) } +void CharacterController::setDirection(const Ogre::Vector3 &dir) +{ + // HACK: The direction length we get is too large. + float mult = dir.length() / 32.0f; + mult = std::max(1.0f, mult); + if(mAnimation) + mAnimation->setSpeedMult(mult); + mDirection = dir.normalisedCopy(); +} + + Ogre::Vector3 CharacterController::update(float duration) { Ogre::Vector3 movement = Ogre::Vector3::ZERO; @@ -125,17 +136,10 @@ Ogre::Vector3 CharacterController::update(float duration) movement += mAnimation->runAnimation(duration); mSkipAnim = false; - if(getState() == CharState_SpecialIdle || getState() == CharState_Idle || - getState() == CharState_Dead) + if(!(getState() == CharState_SpecialIdle || getState() == CharState_Idle || + getState() == CharState_Dead)) { - // FIXME: mDirection shouldn't influence the movement here. - movement += mDirection; - } - else - { - // FIXME: mDirection should be normalized after setting the speed of - // the animation in setDirection, rather than here. - movement = mDirection.normalisedCopy() * movement.length(); + movement = mDirection * movement.length(); } return movement; diff --git a/apps/openmw/mwmechanics/character.hpp b/apps/openmw/mwmechanics/character.hpp index f2651b4cfc..9a82f890d4 100644 --- a/apps/openmw/mwmechanics/character.hpp +++ b/apps/openmw/mwmechanics/character.hpp @@ -54,8 +54,7 @@ public: void playGroup(const std::string &groupname, int mode, int count); void skipAnim(); - void setDirection(const Ogre::Vector3 &dir) - { mDirection = dir; } + void setDirection(const Ogre::Vector3 &dir); void setState(CharacterState state); CharacterState getState() const diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index 4d8f9586fb..a86e14c83b 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -26,6 +26,7 @@ Animation::Animation(const MWWorld::Ptr &ptr) , mLastPosition(0.0f) , mCurrentKeys(NULL) , mAnimState(NULL) + , mAnimSpeedMult(1.0f) { } @@ -207,6 +208,7 @@ void Animation::play(const std::string &groupname, const std::string &start) Ogre::Vector3 Animation::runAnimation(float timepassed) { Ogre::Vector3 movement = Ogre::Vector3::ZERO; + timepassed *= mAnimSpeedMult; while(mAnimState && timepassed > 0.0f) { float targetTime = mAnimState->getTimePosition() + timepassed; diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index 349871f0a6..6c8357d59e 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -31,6 +31,7 @@ protected: NifOgre::TextKeyMap *mCurrentKeys; NifOgre::TextKeyMap::const_iterator mNextKey; Ogre::AnimationState *mAnimState; + float mAnimSpeedMult; /* Updates the animation to the specified time, and returns the movement * vector since the last update or reset. */ @@ -54,6 +55,9 @@ public: // should be on the scale of 0 to 1. void setAccumulation(const Ogre::Vector3 &accum); + void setSpeedMult(float speedmult) + { mAnimSpeedMult = speedmult; } + void play(const std::string &groupname, const std::string &start); virtual Ogre::Vector3 runAnimation(float timepassed); };