- Weird Issue Quaternion Rotation in XNA Space Simulator
- public void MoveShip(List<InputAction> InputActionList, GameTime gameTime)
- {
- float second = (float)gameTime.ElapsedGameTime.TotalSeconds;
- float currentTurningSpeed = second * TurningSpeed;
- float leftRightRotation = 0;
- float upDownRotation = 0;
- float linearLeftRightRotation = 0;
- foreach(InputAction action in InputActionList)
- {
- switch(action)
- {
- case InputAction.Left:
- leftRightRotation -= currentTurningSpeed;
- break;
- case InputAction.Right:
- leftRightRotation += currentTurningSpeed;
- break;
- case InputAction.Up:
- upDownRotation += currentTurningSpeed;
- break;
- case InputAction.Down:
- upDownRotation -= currentTurningSpeed;
- break;
- case InputAction.IncreaseSpeed:
- if (ShipSpeed < MaxShipSpeed)
- ShipSpeed += Acceleration;
- break;
- case InputAction.DecreaseSpeed:
- if (ShipSpeed > MinShipSpeed)
- ShipSpeed -= Acceleration;
- break;
- case InputAction.LinearLeft:
- linearLeftRightRotation += currentTurningSpeed;
- break;
- case InputAction.LinearRight:
- linearLeftRightRotation -= currentTurningSpeed;
- break;
- case InputAction.Fire1:
- WeaponSystem2D.RequestFire(ShipPosition, ShipRotation);
- break;
- }
- }
- Quaternion currentRotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, 1), leftRightRotation) *
- Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRotation) *
- Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), linearLeftRightRotation);
- currentRotation.Normalize();
- ShipRotation *= currentRotation;
- ShipPosition *= Vector3.Transform(new Vector3(0, 0, 1), ShipRotation) * (ShipSpeed * second);
- ShipWorld = Matrix.CreateFromQuaternion(ShipRotation) * Matrix.CreateTranslation(ShipPosition);
- }