@pixi/3d
    Preparing search index...

    Class Vector2

    A point or direction in 2D, such as a UV coordinate, a screen position or stick input

    Math methods change the vector they're called on and return it, so calls chain. Pass an out point as the last argument to write the result there instead and leave this one as it is; Vector3 explains why to reuse out points in code that runs every frame. Where a method takes a point, any pixi.js PointData works, such as a pixi Point or a plain { x, y } object.

    const input = new Vector2(stick.x, stick.y);

    if (input.magnitudeSquared() > 1) input.normalize();
    player.position.x += input.x * speed * dt;
    player.position.z += input.y * speed * dt;

    Implements

    Index
    • Creates a new Vector2

      Parameters

      • x: number = 0

        The x component

      • y: number = 0

        The y component

      Returns Vector2

    x: number = 0

    The x component

    cursor.x = event.global.x;
    
    y: number = 0

    The y component

    cursor.y = event.global.y;
    
    • get shared(): Vector2

      A static Vector2 reset to (0, 0) on every access, for a result used straight away

      Important

      This vector is shared and temporary. Do not store references to it.

      Returns Vector2

      const dragged = Vector2.shared.copyFrom(event.global).distanceTo(dragStart) > dragThreshold;
      
    • Adds v to this vector

      Parameters

      • v: PointData

        The point to add

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      scroll.add(scrollStep);
      anchor.add(labelOffset, labelPosition);
    • Adds v * s to this vector, such as a speed times the frame time

      Parameters

      • v: PointData

        The point to scale and add

      • s: number

        The scalar to multiply v by

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      uvOffset.addScaledVector(scrollSpeed, dt);
      
    • Computes the angle from the +x axis to this vector, turning toward +y

      Returns number

      The angle in radians, from -π to π

      cursor.subtract(arrow.position, toCursor);
      arrow.rotation = toCursor.angle();
    • Transforms this point by m as a 2D transform: rotation, scale and translation

      The matrix's bottom row is not used, so there is no divide.

      Parameters

      • m: Matrix3

        The matrix to apply

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      const uvTransform = new Matrix3().setUvTransform(0.25, 0, 2, 2, 0, 0.5, 0.5);

      uv.applyMatrix3(uvTransform);
    • Clamps each component of this vector between the matching components of min and max

      Assumes min <= max componentwise.

      Parameters

      • min: PointData

        The lower bound per component

      • max: PointData

        The upper bound per component

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      cursor.clamp(minimapMin, minimapMax, minimapMarker);
      
    • Creates a copy of this vector

      Returns Vector2

      const dragStart = cursor.clone();
      
    • Copies x, y from the given point into this vector

      Parameters

      Returns this

      dragStart.copyFrom(event.global);
      
    • Copies this vector's x, y into the given point

      Type Parameters

      Parameters

      • p: T

        The point to copy into

      Returns T

      The given point with values updated

      cursor.copyTo(crosshair.position);
      
    • Computes x * v.y - y * v.x, the z of the 3D cross product with both vectors at z = 0

      The sign says which way to turn from this vector to v: positive is the way +x turns toward +y (counter-clockwise when y points up, clockwise on a pixi screen, where y points down), negative the other way, and zero when they are parallel.

      Parameters

      Returns number

      const turnDirection = Math.sign(heading.cross(toTarget));
      
    • Computes the distance from this point to v

      Parameters

      Returns number

      if (cursor.distanceTo(dragStart) > dragThreshold) startDrag();
      
    • Computes the squared distance from this point to v

      Cheaper than Vector2#distanceTo and enough to compare distances.

      Parameters

      Returns number

      const hovered = cursor.distanceToSquared(button.center) < button.radius * button.radius;
      
    • Divides each component of this vector by the matching component of v

      Parameters

      • v: PointData

        The point to divide by

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      pixel.divide({ x: texture.width, y: texture.height }, uv);
      
    • Divides each component of this vector by the scalar s

      Parameters

      • s: number

        The scalar to divide by

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      thumbOffset.divideScalar(stickRadius, stick);
      
    • Computes the dot product of this vector and v

      For unit vectors it's the cosine of the angle between them: 1 when they point the same way, 0 at right angles and -1 when opposite.

      Parameters

      Returns number

      const targetIsAhead = heading.dot(toTarget) > 0;
      
    • Checks whether x and y both match p exactly, with no tolerance

      Parameters

      Returns boolean

      if (!cursor.equals(lastCursor)) updateHover(cursor);
      
    • Reads x, y from array starting at offset

      Parameters

      • array: Float32Array<ArrayBufferLike> | number[]

        The array to read from

      • offset: number = 0

        The index of the first component

      Returns this

      uv.fromArray(uvs, vertexIndex * 2);
      
    • Computes the length of this vector

      Returns number

      const stickTilt = stick.magnitude();
      
    • Computes the squared length of this vector

      Cheaper than Vector2#magnitude, since it skips the square root, and enough to compare lengths.

      Returns number

      if (stick.magnitudeSquared() < deadZone * deadZone) stick.set(0);
      
    • Sets each component to the larger of this vector's and v's

      Parameters

      • v: PointData

        The point to compare with

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      selectionMax.max(cursor);
      
    • Sets each component to the smaller of this vector's and v's

      Parameters

      • v: PointData

        The point to compare with

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      selectionMin.min(cursor);
      
    • Multiplies each component of this vector by the matching component of v

      Parameters

      • v: PointData

        The point to multiply by

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      uv.multiply(tiling);
      
    • Multiplies each component of this vector by the scalar s

      Parameters

      • s: number

        The scalar to multiply by

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      input.multiplyScalar(speed * dt, step);
      
    • Negates each component of this vector, reversing its direction

      Parameters

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      hitDirection.negate(knockback);
      
    • Scales this vector to unit length, keeping its direction

      A zero vector stays zero.

      Parameters

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      input.normalize();
      
    • Rotates this vector around the origin by angle radians, turning +x toward +y

      Parameters

      • angle: number

        The rotation angle in radians

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      stick.rotate(-cameraYaw, moveDirection);
      
    • Rotates this point around center by angle radians, turning +x toward +y

      Parameters

      • center: PointData

        The point to rotate around

      • angle: number

        The rotation angle in radians

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      moon.rotateAround(planet, orbitSpeed * dt);
      
    • Sets the x and y components

      Parameters

      • x: number = 0

        The x component

      • y: number = x

        The y component, defaults to x

      Returns this

      uvOffset.set(0.5, 0);
      tiling.set(4); // (4, 4)
    • Scales this vector so its length equals length, keeping its direction

      A zero vector stays zero.

      Parameters

      • length: number

        The target length

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      dashDirection.setLength(dashDistance, dashOffset);
      
    • Subtracts v from this vector

      Parameters

      • v: PointData

        The point to subtract

      • out: PointLike = ...

        The target to write the result into, defaults to this vector

      Returns this

      The vector the result was written into

      cursor.subtract(dragStart, dragDelta);
      
    • Writes this vector's values into array starting at offset

      Allocates a new array when one is not supplied.

      Type Parameters

      • T extends Float32Array<ArrayBufferLike> | number[] = number[]

      Parameters

      • array: T = ...

        The array to write into

      • offset: number = 0

        The index to write the first component at

      Returns T

      The array the values were written into

      uv.toArray(uvs, vertexIndex * 2);
      
    • Returns a readable string representation of this vector

      Returns string

      console.log(cursor.toString());