Jump to content

  • Log In with Google      Sign In   
  • Create Account


Nick Joseph

Member Since 05 Jan 2012
Offline Last Active Mar 15 2013 09:05 AM
-----

#5025794 Slerping the Kool Aid - Help!

Posted by Nick Joseph on 26 January 2013 - 11:14 AM

I don't even know what to say, every point you made was valid.  Thank you so much!  To anyone interested here's the updated code (though it's not perfect, the rotation slows down as the actor gets closer to his desired rotation on account of the SLERP calculations so that'll be the next thing I look into)...

 

 

TransformComponent transformComponent = _actor.GetComponent<TransformComponent>();
 
            Vector3 position = transformComponent.Transform.Translation;
 
            Vector3 currentPatrolPoint = _patrolPoints.ElementAt(_currentPatrolPoint);
 
            Vector3 difference = (currentPatrolPoint - position);
 
            if (_hasFinishedRotating == false)
            {
                Vector3 lookAtDirection = Vector3.Normalize(difference);
                
                Vector3 forward = Vector3.Normalize(transformComponent.Transform.Forward);
 
                float forwardDot = (float)Math.Round(
                    Vector3.Dot(
                        forward,
                        lookAtDirection),
                    DOT_PRECISION);
 
                if (forwardDot != 1)
                {
                    Vector3 rotationAxis = Vector3.Normalize(
                        Vector3.Cross(
                            lookAtDirection, 
                            forward));
 
                    float rotationAngle = MathUtilities.GetUnsignedAngle(
                        forward,
                        lookAtDirection);
 
                    Vector3 right = Vector3.Normalize(transformComponent.Transform.Right);
 
                    float rightDot = (float)Math.Round(
                        Vector3.Dot(
                            right,
                            lookAtDirection),
                        DOT_PRECISION);
 
                    if (rightDot > 0)
                    {
                        rotationAngle *= -1;
                    }
 
                    float slerpAmount = (float)gameTime.ElapsedGameTime.Milliseconds / 1000f;
 
                    if (forwardDot > .98f)
                    {
                        slerpAmount = 1.0f;
                    }
 
                    Quaternion qStart = Quaternion.CreateFromRotationMatrix(transformComponent.Transform);
 
                    Quaternion qEnd = Quaternion.CreateFromAxisAngle(rotationAxis, rotationAngle);
 
                    Quaternion qSlerp = Quaternion.Slerp(
                        qStart,
                        qStart * qEnd,
                        slerpAmount);
 
                    Matrix rotation = Matrix.CreateFromQuaternion(qSlerp);
 
                    rotation.Translation = position;
 
                    PhysicsComponent physicsComponent = _actor.GetComponent<PhysicsComponent>();
 
                    physicsComponent.KinematicMove(rotation);
                }
                else
                {
                    _hasFinishedRotating = true;
                }
            }



PARTNERS