Skip to content

Home > @esengine/ecs-framework-monorepo > Vector2

Vector2 class

2D向量类

提供完整的2D向量运算功能,包括: - 基础运算(加减乘除) - 向量运算(点积、叉积、归一化) - 几何运算(距离、角度、投影) - 变换操作(旋转、反射、插值)

Signature:

typescript
export declare class Vector2

Constructors

Constructor

Modifiers

Description

(constructor)(x, y)

创建2D向量

Properties

Property

Modifiers

Type

Description

angle

readonly

number

获取向量角度(弧度)

DOWN

static

readonly

Vector2

下方向向量 (0, -1)

isUnit

readonly

boolean

检查是否为单位向量

isZero

readonly

boolean

检查是否为零向量

LEFT

static

readonly

Vector2

左方向向量 (-1, 0)

length

readonly

number

获取向量长度(模)

lengthSquared

readonly

number

获取向量长度的平方

ONE

static

readonly

Vector2

单位向量 (1, 1)

RIGHT

static

readonly

Vector2

右方向向量 (1, 0)

UP

static

readonly

Vector2

上方向向量 (0, 1)

x

number

X分量

y

number

Y分量

ZERO

static

readonly

Vector2

零向量 (0, 0)

Methods

Method

Modifiers

Description

add(other)

向量加法

add(a, b)

static

向量加法(静态方法)

angleTo(other)

计算与另一个向量的夹角(弧度)

clamp(min, max)

限制向量分量

clampLength(maxLength)

限制向量长度

clone()

克隆当前向量

copy(other)

复制另一个向量的值

cross(other)

计算与另一个向量的叉积(2D中返回标量)

cross(a, b)

static

向量叉积(静态方法)

distance(a, b)

static

计算两点间距离(静态方法)

distanceTo(other)

计算到另一个向量的距离

distanceToSquared(other)

计算到另一个向量的距离平方

divide(scalar)

向量数除

dot(other)

计算与另一个向量的点积

dot(a, b)

static

向量点积(静态方法)

equals(other, epsilon)

检查两个向量是否相等

exactEquals(other)

检查两个向量是否完全相等

fromAngle(angle)

static

从角度创建单位向量(静态方法)

fromPolar(length, angle)

static

从极坐标创建向量(静态方法)

lerp(target, t)

线性插值

lerp(a, b, t)

static

线性插值(静态方法)

max(a, b)

static

获取两个向量中的最大分量向量(静态方法)

min(a, b)

static

获取两个向量中的最小分量向量(静态方法)

multiply(scalar)

向量数乘

multiply(vector, scalar)

static

向量数乘(静态方法)

negate()

向量取反

normalize()

向量归一化(转换为单位向量)

normalized()

获取归一化后的向量(不修改原向量)

perpendicular()

获取垂直向量(逆时针旋转90度)

projectOnto(onto)

计算向量在另一个向量上的投影

projectOntoLength(onto)

计算向量在另一个向量上的投影长度

reflect(normal)

反射向量(关于法线)

reflected(normal)

获取反射后的向量(不修改原向量)

rotate(angle)

向量旋转

rotateAround(center, angle)

围绕一个点旋转

rotated(angle)

获取旋转后的向量(不修改原向量)

set(x, y)

设置向量分量

subtract(other)

向量减法

subtract(a, b)

static

向量减法(静态方法)

toArray()

转换为数组

toObject()

转换为普通对象

toString()

转换为字符串

基于 MIT 许可证发布