Free rotation in 3D in Raylib
Work-in-progress as I'm creating the blog site while adding the content
Something about rotation in 3D using RotateByAxis
float pitchSpeed = 1.0f * frameTime; // Pitch rotation speed
float yawSpeed = 1.0f * frameTime; // Yaw rotation speed
float moveSpeed = 10.0f * frameTime; // Movement speed
// Increase yaw (Y rotation)
if (IsKeyDown(KeyboardKey.Kp4))
{
Forward = Vector3RotateByAxisAngle(Forward, Up, yawSpeed);
Right = Vector3.Cross(Forward, Up);
}
// Decrease yaw (Y rotation)
if (IsKeyDown(KeyboardKey.Kp6))
{
Forward = Vector3RotateByAxisAngle(Forward, Up, -yawSpeed);
Right = Vector3.Cross(Forward, Up);
}
// Increase pitch (X rotation)
if (IsKeyDown(KeyboardKey.Kp8))
{
Up = Vector3RotateByAxisAngle(Up, Right, -pitchSpeed);
Forward = Vector3.Cross(Up, Right);
}
// Decrease pitch (X rotation)
if (IsKeyDown(KeyboardKey.Kp5))
{
Up = Vector3RotateByAxisAngle(Up, Right, +pitchSpeed);
Forward = Vector3.Cross(Up, Right);
}
// Roll
if (IsKeyDown(KeyboardKey.Kp7))
{
Right = Vector3RotateByAxisAngle(Right, Forward, -pitchSpeed);
Up = Vector3.Cross(Right, Forward);
}
// Roll
if (IsKeyDown(KeyboardKey.Kp9))
{
Right = Vector3RotateByAxisAngle(Right, Forward, pitchSpeed);
Up = Vector3.Cross(Right, Forward);
}
Forward = Vector3.Normalize(Forward);
Up = Vector3.Normalize(Up);
Right = Vector3.Normalize(Right);
// Move forward
if (IsKeyDown(KeyboardKey.W)) // ||IsKeyDown(KeyboardKey.W)
Movement += Forward * moveSpeed;
// Move backward
if (IsKeyDown(KeyboardKey.S)) // || IsKeyDown(KeyboardKey.S)
Movement -= Forward * moveSpeed;
// Apply movement
Position += Movement * frameTime;
// Dampen the movement
Movement *= 0.99f;
// Update the model's transform matrix with the new rotation
model.Transform = //MatrixLookAt(Vector3.Zero, Forward * 2, Up);
new Matrix4x4(
Right.X, Up.X, Forward.X, 0.0f,
Right.Y, Up.Y, Forward.Y, 0.0f,
Right.Z, Up.Z, Forward.Z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}