Quaternion
Namespace:
namespace Lenga\Engine\Core;
class Quaternion
Quaternion is Lenga's 3D rotation value type.
Use it when you want to:
- store a 3D rotation safely
- combine rotations without depending on Euler-angle addition
- rotate a direction vector such as
Vector3::back() - work with
Transform::$rotationandTransform::$localRotation
Common Methods
identity
public static function identity(): self
Returns the identity rotation.
fromEulerAngles
public static function fromEulerAngles(Vector3 $degrees): self
Creates a quaternion from Euler angles in degrees.
toEulerAngles
public function toEulerAngles(): Vector3
Converts the quaternion back to Euler angles in degrees.
multiply
public function multiply(Quaternion $other): self
Combines this rotation with another rotation.
inverse
public function inverse(): self
Returns the inverse rotation.
rotateVector
public function rotateVector(Vector3 $vector): Vector3
Rotates a direction or offset vector by this quaternion.
Example
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Quaternion;
use Lenga\Engine\Core\Vector3;
class TurretAim extends Behaviour
{
public function update(): void
{
$yaw = Quaternion::fromEulerAngles(new Vector3(0.0, 90.0, 0.0));
$pitch = Quaternion::fromEulerAngles(new Vector3(-15.0, 0.0, 0.0));
$this->transform->localRotation = $yaw->multiply($pitch);
$forward = $this->transform->rotation->rotateVector(Vector3::back());
}
}
Usage Notes
- Use
Transform::$rotationfor world rotation. - Use
Transform::$localRotationfor rotation relative to the parent. - Use
fromEulerAngles(...)when you want to start from readable degree values. - Use
toEulerAngles()when you need to present a quaternion as Euler angles.