1
votes

Actually, i'm programming a little spaceshooter game (2.5D, Topdown View). The player can move along the XZ Axies and rotate the spaceship via right ministick (gamepad) or look to the cursor position (keyboard + mouse).

So, the movement and rotation (Y-Axies, Yaw) are seperated. The whole thing works fine and looks good - but now i want to do the following:

If the spaceship moves sideways, it should rotate around the X / Pitch axies / lean left and right a bit, dependent on the sideways speed.

So, i have to compute the sideways speed from the following, given input:

Velocity Vector (Movement on X and Z Axies, Y is always '0') Direction Vector (Rotation on Y Axies, X and Z are always '0')

And with the amount of sideways speed, i could rotate my spaceship around the X axies and multiply the resulting quaternion by the rotation around the y axies.

Anyone who has a solution for this?


Solution: Just "rotate" the velocity vector by the heading of the spaceship and use the "roll/z" axis as the sideways rotation about the X axis (the axis, where your ships nose points towards):

Quaternion Rotation     = Quaternion.Euler(0, mHeading.y, 0);
Vector3 RealVeloctiy    = Quaternion.Euler(0, -mHeading.y, 0) * Velocity; 
float Angle         = RealVeloctiy.z * 2.5f;        
Rotation            = Rotation *   Quaternion.Euler(Angle, 0, 0);
1

1 Answers

2
votes

A couple of possibilities:

  1. Compare the velocity vector with local left direction of the spaceship (make sure they're in the same coordinate space first). You could use the Vector3.Angle function, or a dot product to do the comparison. Scale the result appropriately, and apply a local rotation around the forward axis.

  2. Take user input directly. If the user is strafing in a direction, apply a roll value. You could use a float between -1 and +1, along with a rate of change. If they're strafing left, move the value towards -1 at that rate, or if they're strafing left, to +1. If neither key is being pressed, move the value back towards 0. (You might like to play around with the Lerp, SmoothStep and SmoothDamp functions too). Scale the value to apply an appropriate rotation about the relevant axis.