Announcement

Collapse
No announcement yet.

Quaternion.Slerp vehicle turn example help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Quaternion.Slerp vehicle turn example help

    I am trying to emulate a vehicle taking a left turn in an intersection and from what I can tell Quaternion.Slerp is what I want but I can't seem to figure out how to use it correctly. My biggest difficulty is figuring out how to convert from Vector3 to Quaternion and back again. I also am not sure what the 3rd parameter is in Quaternion.Slerp. I'm using a Lerp for the position and attempting a slerp for the rotation. Although I probably want a slerp for the position as well. Here is my example:

    Code:
     
          protected override void Initialize()
          {
             base.Initialize();
             PosA = EntityManager.Find(PositionA).FindComponent<Transform3D>();
             PosB = EntityManager.Find(PositionB).FindComponent<Transform3D>();
             PosC = EntityManager.Find(PositionC).FindComponent<Transform3D>();
             PosD = EntityManager.Find(PositionD).FindComponent<Transform3D>();
             PosE = EntityManager.Find(PositionE).FindComponent<Transform3D>();
             Waypoints = new Transform3D[] { PosA, PosB, PosC, PosD, PosE };
          }
          private float totalGameTime = 0;
          float currentTimeOnPath = 0;
          protected override void Update(TimeSpan gameTime)
          {
             // keep track of game time as well as path time
             totalGameTime += (float)gameTime.TotalSeconds;
             currentTimeOnPath += (float) gameTime.TotalSeconds;
    
             Vector3 startPosition = Waypoints[CurrentWaypoint].LocalPosition;
             Vector3 endPosition = Waypoints[CurrentWaypoint + 1].LocalPosition;
             // lerp to destination
             float pathLength = Vector3.Distance(startPosition, endPosition);
             float totalTimeForPath = pathLength / Speed;
             Transform.LocalPosition = Vector3.Lerp(startPosition, endPosition, (float)(currentTimeOnPath / totalTimeForPath));
    
             Vector3 startRotation = Waypoints[CurrentWaypoint].LocalRotation;
             Vector3 endRotation = Waypoints[CurrentWaypoint].LocalRotation;
             Quaternion startR;
             Vector3 up1 = Vector3.Up;
             Quaternion endR;
             Vector3 up2 = Vector3.Up;
             Quaternion.CreateFromLookAt(ref startRotation,ref up1,out startR);
             Quaternion.CreateFromLookAt(ref endRotation, ref up2, out endR);
             Quaternion result = Quaternion.Slerp(startR, endR, 1);
             Vector3 resultV;
             Vector3.FromQuaternion(ref result, out resultV);
             Transform.LocalRotation = resultV;
             if(currentTimeOnPath / totalTimeForPath >= 1 && CurrentWaypoint < Waypoints.Length - 2)
             {
                CurrentWaypoint++;
                currentTimeOnPath = 0;
             }
          }
    Please refer me to an example or correct my code. Thank you.
    Last edited by longlostbro; 08-02-2017, 03:53 PM. Reason: Quaternion, Vector3, Rotation, Slerp
Working...
X