(Quaternion based) Trouble moving foward based on model rotation
- by ChocoMan
Using quaternions, I'm having trouble moving my model in its facing direction. Currently the model moves can move in all cardinal directions with no problems. The problem comes when I rotate the move as it still travelling in the direction of world space. Meaning, if I'm moving forward, backward or any other direction while rotating the model, the model acts like its a figure skater spinning while traveling in the same direction.
How do I update the direction of travel proper with the facing direction of the model?
Rotates model on Y-axis:
Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(speedAngleMAX);
AddRotation = Quaternion.CreateFromYawPitchRoll(yaw, 0, 0);
ModelLoad.MRotation *= AddRotation;
MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);
Moves model forward:
// Move Forward
if (pController.IsButtonDown(Buttons.LeftThumbstickUp))
{
SpeedX = (float)(Math.Sin(ModelLoad.ModelRotation)) * FWDSpeedMax * pController.ThumbSticks.Left.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
SpeedZ = (float)(Math.Cos(ModelLoad.ModelRotation)) * FWDSpeedMax * pController.ThumbSticks.Left.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
// Update model position
ModelLoad._modelPos += Vector3.Forward * SpeedZ;
ModelLoad._modelPos += Vector3.Left * SpeedX;
}