Skip to content

Instantly share code, notes, and snippets.

@DenisBelmondo
Last active March 1, 2023 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisBelmondo/8aa48834cd80c28cba297a7a3cae133a to your computer and use it in GitHub Desktop.
Save DenisBelmondo/8aa48834cd80c28cba297a7a3cae133a to your computer and use it in GitHub Desktop.
Untested transform rotation shit
public struct Transform
{
public Vector3 Right;
public Vector3 Up;
public Vector3 Forward;
public Transform()
{
Right = Vector3.UnitX;
Up = Vector3.UnitY;
Forward = Vector3.UnitZ;
}
//
// note: this is the same as rotating the object using Up as the axis around which to rotate
// To rotate "about an axis" is to rotate all components BUT the axis.
// notice how there is no mention of rotation of the Up vector
// it's like going Vector3.Rotate(Axis: Up, radians: radians)
//
public void RotateYaw(float radians)
{
Right.X = Right.X * MathF.Sin(radians) - Right.Z * MathF.Cos(radians);
Right.Z = Right.X * MathF.Cos(radians) + Right.Z * MathF.Sin(radians);
Right = Vector3.Normalize(Right);
Forward.X = Forward.X * MathF.Cos(radians) - Forward.Z * MathF.Sin(radians);
Forward.Z = Forward.X * MathF.Sin(radians) + Forward.Z * MathF.Cos(radians);
Forward = Vector3.Normalize(Forward);
}
//
// note: this is the same as rotating the object using Up as the axis around which to rotate
// like sticking a kebab spit through the side (x axis) of the object
// notice how there is no mention of rotation of the Right vector
// it's like going Vector3.Rotate(Axis: Right, radians: radians)
//
public void RotatePitch(float Radians)
{
Up.Y = Up.Y * MathF.Cos(radians) - Up.Z * MathF.Sin(radians);
Up.Z = Up.Y * MathF.Sin(radians) + Up.Z * MathF.Cos(radians);
Up = Vector3.Normalize(Up);
Forward.Y = Forward.Y * MathF.Cos(radians) - Forward.Z * MathF.Sin(radians);
Forward.Z = Forward.Y * MathF.Sin(radians) + Forward.Z * MathF.Cos(radians);
Forward = Vector3.Normalize(Forward);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment