Search code examples
c#matrixxnaquaternions

Weird Issue Quaternion Rotation in XNA Space Simulator


I have spent 8 hours trying to find a solution.

So here is my problem. My ship is rotating like it's rotating around something, like a string. When I go farther and farther from the starting position, I start rotating more weirder and weirder. Like I am attached to a string.

Here is the code for Rotation and Movement.

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);

    }

What is the problem? Why is it doing this? I want the ship to rotate on a dime, since it is space.

EDIT: -The model is not an issue.

EDIT 2: Nevermind, the rotation was actually working, it was my skybox that was broken!


Solution

  • This code is actually perfect, my skybox made it look like it was broken.