Vector2

2D vector value type mirroring 's .Vector2.

Namespace:

namespace Lenga\Engine\Core;

class Vector2

Methods

down

public static function down()

Unit vector pointing down (0, -1).

up

public static function up()

Unit vector pointing up (0, 1).

left

public static function left()

Unit vector pointing left (-1, 0).

right

public static function right()

Unit vector pointing right (1, 0).

one

public static function one()

Vector with all components set to 1.

zero

public static function zero()

Vector with all components set to 0.

negativeInfinity

public static function negativeInfinity()

Shorthand for Vector2(-INF, -INF).

positiveInfinity

public static function positiveInfinity()

Shorthand for Vector2(INF, INF).

offsetExists

public function offsetExists(mixed $offset)

Checks whether an array offset maps to a valid vector component.

offsetGet

public function offsetGet(mixed $offset)

Gets a vector component by index: 0|'x' => x, 1|'y' => y.

offsetSet

public function offsetSet(mixed $offset, mixed $value)

Sets a vector component by index: 0|'x' => x, 1|'y' => y.

offsetUnset

public function offsetUnset(mixed $offset)

Unsets a vector component by index.

add

public function add(Vector2 $other)

Adds another vector to this vector in place.

subtract

public function subtract(Vector2 $other)

Subtracts another vector from this vector in place.

multiply

public function multiply(Vector2 $other)

Multiplies this vector component-wise by another vector in place.

divide

public function divide(Vector2 $other)

Divides this vector component-wise by another vector in place.

scale

public function scale(float $scalar)

Scales this vector by a scalar in place.

normalize

public function normalize()

Normalizes this vector to have a magnitude of 1 in place.

set

public function set(float $x, float $y)

Sets the x and y components of this vector.

equals

public function equals(Vector2 $other)

Returns true if this vector is exactly equal to the other vector.

__toString

public function __toString()

Returns a formatted string representation of this vector.

toVector3

public function toVector3(float $z = 0.0)

Converts this Vector2 to a Vector3 with an optional z component.

fromVector4

public static function fromVector4(Vector4 $v)

Converts a Vector4 to a Vector2 by dropping the z and w components.

fromVector3

public static function fromVector3(Vector3 $v)

Converts a Vector3 to a Vector2 by dropping the z component.

toVector4

public function toVector4(float $z = 0.0, float $w = 0.0)

Converts this Vector2 to a Vector4 with optional z and w components.

sum

public static function sum(Vector2 $a, Vector2 $b)

Returns the sum of two vectors.

difference

public static function difference(Vector2 $a, Vector2 $b)

Returns the difference of two vectors.

scaleNew

public static function scaleNew(Vector2 $v, float $scalar)

Returns a new vector scaled by a scalar.

product

public static function product(Vector2 $a, Vector2 $b)

Multiplies two vectors component-wise and returns the result.

dot

public static function dot(Vector2 $a, Vector2 $b)

Returns the dot product of two vectors.

angle

public static function angle(Vector2 $from, Vector2 $to)

Returns the unsigned angle in degrees between two vectors.

signedAngle

public static function signedAngle(Vector2 $from, Vector2 $to)

Returns the signed angle in degrees between two vectors.

distance

public static function distance(Vector2 $a, Vector2 $b)

Returns the distance between two points.

lerp

public static function lerp(Vector2 $a, Vector2 $b, float $t)

Linearly interpolates between two vectors by t, clamped to [0, 1].

lerpUnclamped

public static function lerpUnclamped(Vector2 $a, Vector2 $b, float $t)

Linearly interpolates between two vectors by t, without clamping.

max

public static function max(Vector2 $a, Vector2 $b)

Returns a vector made from the largest components of two vectors.

min

public static function min(Vector2 $a, Vector2 $b)

Returns a vector made from the smallest components of two vectors.

clampMagnitude

public static function clampMagnitude(Vector2 $vector, float $maxLength)

Returns a copy of $vector with its magnitude clamped to $maxLength.

moveTowards

public static function moveTowards(Vector2 $current, Vector2 $target, float $maxDelta)

Moves $current towards $target by at most $maxDelta.

perpendicular

public static function perpendicular(Vector2 $inDirection)

Returns the 2D vector perpendicular to the given direction,

reflect

public static function reflect(Vector2 $inDirection, Vector2 $inNormal)

Reflects a vector off the surface defined by a normal.

smoothDamp

public static function smoothDamp(Vector2 $current,
        Vector2 $target,
        Vector2 &$currentVelocity,
        float $smoothTime,
        float $maxSpeed = PHP_FLOAT_MAX,
        float $deltaTime = 0.0,)

Gradually changes a vector towards a desired goal over time using a spring-damper.

fromVector3

public static function fromVector3(Vector3 $v)

Converts a Vector3 to a Vector2 by dropping the z component.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Vector2;
use Lenga\Engine\Core\MathUtil;

class VelocityController extends Behaviour
{
    public function update(): void
    {
        // Create a vector
        $velocity = new Vector2(5.0, 3.0);

        // Get magnitude and normalize
        $speed = $velocity->magnitude();
        $direction = $velocity->normalized();

        // Vector operations
        $acceleration = new Vector2(1.0, 2.0);
        $newVelocity = Vector2::add($velocity, $acceleration);

        // Dot product and angle
        $dot = Vector2::dot($velocity, $acceleration);
        $distance = Vector2::distance($velocity, $acceleration);

        // Lerp between two vectors
        $start = new Vector2(0, 0);
        $end = new Vector2(10, 10);
        $midpoint = Vector2::lerp($start, $end, 0.5);
    }
}