跳转至

Box3Vector3 / GameVector3 三维向量

这是一个服务端API

该API仅在服务端脚本使用

查阅官方文档
查阅官方文档(Arena)
查阅社区文档(Arena)
Box3Vector3(三维向量)是Box3中一个非常常见的类,通常指定一个位置,或者尺寸、方向等

构造函数

Box3Vector3 (x: number, y: number, z: number): Box3Vector3
GameVector3 (x: number, y: number, z: number): GameVector3
新建一个三维向量

示例
new Box3Vector3(0, 0, 0)
new Box3Vector3(64, 64, 64)
new Box3Vector3(127.5, 64, 127.5)
new Box3Vector3(11, 45, 14)
new Box3Vector3(19, 19, 810)
new GameVector3(0, 0, 0)
new GameVector3(64, 64, 64)
new GameVector3(127.5, 64, 127.5)
new GameVector3(11, 45, 14)
new GameVector3(19, 19, 810)

常用

set copy clone
scale
add sub mul div
addEq subEq mulEq divEq
equals
distance
min max

属性

属性 类型 说明
x number 三维向量的x值
y number 三维向量的y值
z number 三维向量的z值

方法

说明

下列全部[示例]的代码后面的注释为预期的运行结果,可以不用看“<~”,这只是一个提示符

set (x: number, y: number, z: number): Box3Vector3
set (x: number, y: number, z: number): GameVector3
设置三维向量的值
示例
new Box3Vector3(0, 0, 0).set(5, 5, 0) //<~ { x: 5, y: 5, z: 0 }
new Box3Vector3(1, 2, 3).set(4, 5, 6) //<~ { x: 4, y: 5, z: 6 }
new Box3Vector3().set(5, 5, 5) //<~ { x: 5, y: 5, z: 5 }
new GameVector3(0, 0, 0).set(5, 5, 0) //<~ { x: 5, y: 5, z: 0 }
new GameVector3(1, 2, 3).set(4, 5, 6) //<~ { x: 4, y: 5, z: 6 }
new GameVector3().set(5, 5, 5) //<~ { x: 5, y: 5, z: 5 }
copy (v: Box3Vector3): Box3Vector3
copy (v: GameVector3): GameVector3
v的值复制到这个三维向量中
示例
new Box3Vector3(0, 0, 0).copy(new Box3Vector3(5, 0, 0)) //<~ { x: 5, y: 0, z: 0 }
new Box3Vector3().copy(new Box3Vector3(1, 0, 0)) //<~ { x: 1, y: 0, z: 0 }
new GameVector3(0, 0, 0).copy(new GameVector3(5, 0, 0)) //<~ { x: 5, y: 0, z: 0 }
new GameVector3().copy(new GameVector3(1, 0, 0)) //<~ { x: 1, y: 0, z: 0 }
clone (): Box3Vector3
clone (): GameVector3
复制该向量
示例
new Box3Vector3(5, 5, 5).clone() //<~ { x: 5, y: 5, z: 5 }
new GameVector3(5, 5, 5).clone() //<~ { x: 5, y: 5, z: 5 }
add (v: Box3Vector3): Box3Vector3
add (v: GameVector3): GameVector3

将这个三维向量加上v,返回结果

此方法不会改变原来的向量

示例
new Box3Vector3(1, 1, 1).add(new Box3Vector3(5, 5, 5)) //<~ { x: 6, y: 6, z: 6 }
new GameVector3(1, 1, 1).add(new GameVector3(5, 5, 5)) //<~ { x: 6, y: 6, z: 6 }
sub (v: Box3Vector3): Box3Vector3
sub (v: GameVector3): GameVector3

将这个三维向量减去v,返回结果

此方法不会改变原来的向量

示例
new Box3Vector3(6, 6, 6).sub(new Box3Vector3(1, 1, 1)) //<~ { x: 5, y: 5, z: 5 }
new GameVector3(6, 6, 6).sub(new GameVector3(1, 1, 1)) //<~ { x: 5, y: 5, z: 5 }
mul (v: Box3Vector3): Box3Vector3
mul (v: GameVector3): GameVector3

将这个向量与v点乘,返回结果
也可理解为,将这个向量的xyz分别乘v.xv.yv.z

此方法不会改变原来的向量

示例
new Box3Vector3(5, 5, 5).mul(new Box3Vector3(5, 5, 5)) //<~ { x: 25, y: 25, z: 25 }
new GameVector3(5, 5, 5).mul(new GameVector3(5, 5, 5)) //<~ { x: 25, y: 25, z: 25 }
div (v: Box3Vector3): Box3Vector3
div (v: GameVector3): GameVector3

将这个三维向量除以v,返回结果
也可理解为,将这个向量的xyz分别除以v.xv.yv.z

此方法不会改变原来的向量

示例
new Box3Vector3(25, 25, 25).mul(new Box3Vector3(5, 5, 5)) //<~ { x: 5, y: 5, z: 5 }
new GameVector3(25, 25, 25).mul(new GameVector3(5, 5, 5)) //<~ { x: 5, y: 5, z: 5 }

提示

上面四个方法要注意,填写的不是三个number类型的参数,而是一个Box3Vector3 / GameVector3类型的参数
例如,应该这样写:

new Box3Vector3(6, 6, 6).add(new Box3Vector3(1, 1, 1))
new GameVector3(6, 6, 6).add(new GameVector3(1, 1, 1))

而不是这样:

new Box3Vector3(6, 6, 6).add(1, 1, 1)
new GameVector3(6, 6, 6).add(1, 1, 1)

下面四个方法同理

addEq (v: Box3Vector3): Box3Vector3
addEq (v: GameVector3): GameVector3

将这个三维向量加上v,返回结果

此方法会改变原来的向量

subEq (v: Box3Vector3): Box3Vector3
subEq (v: GameVector3): GameVector3

将这个三维向量减去v,返回结果

此方法会改变原来的向量

mulEq (v: Box3Vector3): Box3Vector3
mulEq (v: GameVector3): GameVector3

将这个向量与v点乘,返回结果
也可理解为,将这个向量的xyz分别乘v.xv.yv.z

此方法会改变原来的向量

divEq (v: Box3Vector3): Box3Vector3
divEq (v: GameVector3): GameVector3

将这个三维向量除以v,返回结果
也可理解为,将这个向量的xyz分别除以v.xv.yv.z

此方法会改变原来的向量

dot (v: Box3Vector3): number
dot (v: GameVector3): number
向量点积
也可理解为,将这个向量与v相乘,返回结果的三个方向的值的和
示例
new Box3Vector3(1, 1, 1).dot(new Box3Vector3(5, 5, 5)) //<~ 15
new GameVector3(1, 1, 1).dot(new GameVector3(5, 5, 5)) //<~ 15
cross (v: Box3Vector3): Box3Vector3
cross (v: GameVector3): GameVector3

将这个向量与v叉乘,返回结果
也可以理解为,返回与这个向量和v所在的平面垂直的向量

提示

无法理解?把下面的示例画个图就好了

示例
new Box3Vector3(1, 1, 1).cross(new Box3Vector3(5, 5, 5)) //<~ { x: 0, y: 0, z: 0 }
new Box3Vector3(1, 1, 1).cross(new Box3Vector3(1, 1, 1)) //<~ { x: 0, y: 0, z: 0 }
new Box3Vector3(1, 1, 1).cross(new Box3Vector3(1, -1, 1)) //<~ { x:2, y:0, z:-2 }
new Box3Vector3(1, 1, -1).cross(new Box3Vector3(1, -1, 1)) //<~ { x:0, y:-2, z:-2 }
new GameVector3(1, 1, 1).cross(new GameVector3(5, 5, 5)) //<~ { x: 0, y: 0, z: 0 }
new GameVector3(1, 1, 1).cross(new GameVector3(1, 1, 1)) //<~ { x: 0, y: 0, z: 0 }
new GameVector3(1, 1, 1).cross(new GameVector3(1, -1, 1)) //<~ { x:2, y:0, z:-2 }
new GameVector3(1, 1, -1).cross(new GameVector3(1, -1, 1)) //<~ { x:0, y:-2, z:-2 }
scale (n: number): Box3Vector3
scale (n: number): GameVector3

将这个向量与n数乘,返回结果
也可理解为,将这个向量缩放至原来的n

此方法不会改变原来的向量

示例
new Box3Vector3(1, 1, 1).scale(5) //<~ {x: 5, y: 5, z: 5 }
new GameVector3(1, 1, 1).scale(5) //<~ {x: 5, y: 5, z: 5 }
lerp (v: Box3Vector3, n: number): Box3Vector3
lerp (v: GameVector3, n: number): GameVector3

插值函数
可以理解为,以该向量的坐标为起点,v的坐标为终点的一段线段,取线段的第n部分的点的位置

示例
new Box3Vector3(0, 0, 0).lerp(new Box3Vector3(1, 1, 1), 0.5) // <~ { x: 0.5, y: 0.5, z: 0.5 }
new Box3Vector3(0, 0, 0).lerp(new Box3Vector3(1, 1, 1), 0.25) // <~ { x: 0.25, y: 0.25, z: 0.25 }
new Box3Vector3(0, 0, 0).lerp(new Box3Vector3(0.5, 0.5, 0.5), 0.5) // <~ { x: 0.25, y: 0.25, z: 0.25 }
new GameVector3(0, 0, 0).lerp(new GameVector3(1, 1, 1), 0.5) // <~ { x: 0.5, y: 0.5, z: 0.5 }
new GameVector3(0, 0, 0).lerp(new GameVector3(1, 1, 1), 0.25) // <~ { x: 0.25, y: 0.25, z: 0.25 }
new GameVector3(0, 0, 0).lerp(new GameVector3(0.5, 0.5, 0.5), 0.5) // <~ { x: 0.25, y: 0.25, z: 0.25 }
mag (): number

求这个向量的大小(也称模)

示例
new Box3Vector3(1, 0, 0).mag() //<~ 1
new Box3Vector3(3, 4, 5).mag() //<~ 7.0710678118654755
new Box3Vector3(11, 45, 14).mag() //<~ 48.394214530251446
new GameVector3(1, 0, 0).mag() //<~ 1
new GameVector3(3, 4, 5).mag() //<~ 7.0710678118654755
new GameVector3(11, 45, 14).mag() //<~ 48.394214530251446
sqrMag (): number

求这个向量的大小(也称模)的平方
比你用mag再用pow函数快

示例
new Box3Vector3(1, 0, 0).sqrMag() //<~ 1
new Box3Vector3(3, 4, 5).sqrMag() //<~ 50
new Box3Vector3(11, 45, 14).sqrMag() //<~ 2342
new GameVector3(1, 0, 0).sqrMag() //<~ 1
new GameVector3(3, 4, 5).sqrMag() //<~ 50
new GameVector3(11, 45, 14).sqrMag() //<~ 2342
towards (v: Box3Vector3): Box3Vector3
towards (v: GameVector3): GameVector3
返回这个向量面向v的值
示例
new Box3Vector3(6, 6, 6).sub(new Box3Vector3(1, 1, 1)) //<~ { x: 5, y: 5, z: 5 }
new Box3Vector3(-6, 6, 6).sub(new Box3Vector3(1, 1, 1)) //<~ { x: -7, y: 5, z: 5 }
new GameVector3(6, 6, 6).sub(new GameVector3(1, 1, 1)) //<~ { x: 5, y: 5, z: 5 }
new GameVector3(-6, 6, 6).sub(new GameVector3(1, 1, 1)) //<~ { x: -7, y: 5, z: 5 }
distance (v: Box3Vector3): number
distance (v: GameVector3): number
返回这个向量到v的距离
示例
new Box3Vector3(6, 0, 0).distance(new Box3Vector3(1, 0, 0)) //<~ 5
new Box3Vector3(6, 6, 6).distance(new Box3Vector3(1, 1, 1)) //<~ 8.660254037844387
new GameVector3(6, 0, 0).distance(new GameVector3(1, 0, 0)) //<~ 5
new GameVector3(6, 6, 6).distance(new GameVector3(1, 1, 1)) //<~ 8.660254037844387
normalize (): Box3Vector3
normalize (): GameVector3
归一化函数
示例
new Box3Vector3(6, 0, 0).normalize() //<~ { x: 1, y: 0, z: 0 }
new Box3Vector3(11, 45, 14).normalize() //<~ { x:0.17032272243312657, y:0.6967747735900632, z:0.6967747735900632 }
new GameVector3(6, 0, 0).normalize() //<~ { x: 1, y: 0, z: 0 }
new GameVector3(11, 45, 14).normalize() //<~ { x:0.17032272243312657, y:0.6967747735900632, z:0.6967747735900632 }
angle (v: Box3Vector3): number
angle (v: GameVector3): number

求这个向量与v的弧度

是弧度,不是角度!

弧度

另一种表示角度的方法,单位为\(rad\)\(\pi rad\)相当于\(180°\)\(2\pi rad\)相当于\(360°\)
其定义为:弧长等于圆半径的弧所对应的圆心角为\(1rad\)

示例
new Box3Vector3(6, 0, 0).angle(new Box3Vector3(-6, 0, 0)) //<~ 3.141592653589793
new Box3Vector3(1, 1, 0).angle(new Box3Vector3(1, 0, 0)) //<~ 0.7853981633974484 //约为0.25 * Math.PI
new GameVector3(6, 0, 0).angle(new GameVector3(-6, 0, 0)) //<~ 3.141592653589793
new GameVector3(1, 1, 0).angle(new GameVector3(1, 0, 0)) //<~ 0.7853981633974484 //约为0.25 * Math.PI
max (v: Box3Vector3): Box3Vector3
max (v: GameVector3): GameVector3

分别对v和该向量的每个方向的坐标值取较大值

示例
new Box3Vector3(1, 2, 1).max(new Box3Vector3(2, 1, 2)) //<~ { x:2, y:2, z:2 }
new GameVector3(1, 2, 1).max(new GameVector3(2, 1, 2)) //<~ { x:2, y:2, z:2 }
min (v: Box3Vector3): Box3Vector3
min (v: GameVector3): GameVector3

分别对v和该向量的每个方向的坐标值取较小值

示例
new Box3Vector3(1, 2, 1).min(new Box3Vector3(2, 1, 2)) //<~ { x:1, y:1, z:1 }
new GameVector3(1, 2, 1).min(new GameVector3(2, 1, 2)) //<~ { x:1, y:1, z:1 }
exactEquals (v: Box3Vector3): boolean
exactEquals (v: GameVector3): boolean

判断两个向量是否完全相等

示例
new Box3Vector3(1, 2, 3).exactEquals(new Box3Vector3(1, 2, 3)) //<~ true
new Box3Vector3(1, 2, 3).exactEquals(new Box3Vector3(4, 5, 6)) //<~ false
new Box3Vector3(1, 2, 3).exactEquals(new Box3Vector3(1.000001, 2, 3)) //<~ false
new Box3Vector3(1, 2, 3).exactEquals(new Box3Vector3(1.000000000000001, 2, 3)) //<~ false
new GameVector3(1, 2, 3).exactEquals(new GameVector3(1, 2, 3)) //<~ true
new GameVector3(1, 2, 3).exactEquals(new GameVector3(4, 5, 6)) //<~ false
new GameVector3(1, 2, 3).exactEquals(new GameVector3(1.000001, 2, 3)) //<~ false
new GameVector3(1, 2, 3).exactEquals(new GameVector3(1.000000000000001, 2, 3)) //<~ false
equals (v: Box3Vector3): boolean
equals (v: GameVector3): boolean

此处与官方API不符

该文档没有tolerance: number参数

此处与社区API不符

该文档没有tolerance: number参数

经 2024/7/18 测试:该文档内容无问题

new GameVector3(1, 2, 3).equals(new GameVector3(1.1, 2, 3), 1) // <~ false

判断两个向量是否大致相等
容差为0.000001

示例
new Box3Vector3(1, 2, 3).equals(new Box3Vector3(1, 2, 3)) //<~ true
new Box3Vector3(1, 2, 3).equals(new Box3Vector3(4, 5, 6)) //<~ false
new Box3Vector3(1, 2, 3).equals(new Box3Vector3(1.000001, 2, 3)) //<~ true
new Box3Vector3(1, 2, 3).equals(new Box3Vector3(1.000000000000001, 2, 3)) //<~ true
new GameVector3(1, 2, 3).equals(new GameVector3(1, 2, 3)) //<~ true
new GameVector3(1, 2, 3).equals(new GameVector3(4, 5, 6)) //<~ false
new GameVector3(1, 2, 3).equals(new GameVector3(1.000001, 2, 3)) //<~ true
new GameVector3(1, 2, 3).equals(new GameVector3(1.000000000000001, 2, 3)) //<~ true
toString (): string

将这个三维向量转换成字符串

示例
JSON.stringify(new Box3Vector3(114514, 1919810, 31415926535).toString()) //<~ "{ x:114514, y:1919810, z:31415926535 }"
JSON.stringify(new GameVector3(114514, 1919810, 31415926535).toString()) //<~ "{ x:114514, y:1919810, z:31415926535 }"
fromPolar (mag: number, phi: number, theta: number): Box3Vector3
fromPolar (mag: number, phi: number, theta: number): GameVector3

使用大小和方向创建向量

内容缺失

由于编者的能力有限,无法编写该内容
如果你愿意为此贡献一份力量,请加入我们

参数 类型 说明
mag 向量大小
phi \(\phi\),磁通量,单位是韦伯Wb
theta \(\theta\)
示例
Box3Vector3.fromPolar(114514, 1919810, 31415926535) //<~ { x:-89551.55210093308, y:71368.36064229338, z:729.9394121558898 }
Box3Vector3.fromPolar(1, 0, 0) //<~ { x:0, y:1, z:0 }
Box3Vector3.fromPolar(1, 0, Math.PI) //<~ { x:0, y:-1, z:1.2246467991473532e-16 }
Box3Vector3.fromPolar(1, 0, Math.PI / 2) //<~ { x:0, y:6.123233995736766e-17, z:1 }
Box3Vector3.fromPolar(1, Math.PI / 2, 0) //<~ { x:0, y:1, z:0 }
Box3Vector3.fromPolar(2, Math.PI, 0) //<~ { x:0, y:2, z:0 }
Box3Vector3.fromPolar(2, -Math.PI, 0) //<~ { x:0, y:2, z:0 }
Box3Vector3.fromPolar(1, 0, -Math.PI) //<~ { x:0, y:-1, z:-1.2246467991473532e-16 }
Box3Vector3.fromPolar(2, Math.PI / 4, Math.PI / 4) //<~ { x:0.9999999999999998, y:1.4142135623730951, z:1 }
Box3Vector3.fromPolar(2, Math.PI / 8 * 7, Math.PI / 8) //<~ { x:0.2928932188134526, y:1.8477590650225735, z:-0.7071067811865476 }
Box3Vector3.fromPolar(2, Math.PI / -4, Math.PI / 4) //<~ { x:-0.9999999999999998, y:1.4142135623730951, z:1 }
Box3Vector3.fromPolar(2, Math.PI / -4, Math.PI / -4) //<~ { x:0.9999999999999998, y:1.4142135623730951, z:-1 }
Box3Vector3.fromPolar(2, Math.PI / 4, Math.PI / -4) //<~ { x:-0.9999999999999998, y:1.4142135623730951, z:-1 }
Box3Vector3.fromPolar(100, -Math.PI, Math.PI) //<~ { x:-1.4997597826618577e-30, y:-100, z:-1.2246467991473532e-14 }
Box3Vector3.fromPolar(100, -Math.PI, Math.PI / 2) //<~ { x:-1.2246467991473532e-14, y:6.123233995736766e-15, z:-100 }
GameVector3.fromPolar(114514, 1919810, 31415926535) //<~ { x:-89551.55210093308, y:71368.36064229338, z:729.9394121558898 }
GameVector3.fromPolar(1, 0, 0) //<~ { x:0, y:1, z:0 }
GameVector3.fromPolar(1, 0, Math.PI) //<~ { x:0, y:-1, z:1.2246467991473532e-16 }
GameVector3.fromPolar(1, 0, Math.PI / 2) //<~ { x:0, y:6.123233995736766e-17, z:1 }
GameVector3.fromPolar(1, Math.PI / 2, 0) //<~ { x:0, y:1, z:0 }
GameVector3.fromPolar(2, Math.PI, 0) //<~ { x:0, y:2, z:0 }
GameVector3.fromPolar(2, -Math.PI, 0) //<~ { x:0, y:2, z:0 }
GameVector3.fromPolar(1, 0, -Math.PI) //<~ { x:0, y:-1, z:-1.2246467991473532e-16 }
GameVector3.fromPolar(2, Math.PI / 4, Math.PI / 4) //<~ { x:0.9999999999999998, y:1.4142135623730951, z:1 }
GameVector3.fromPolar(2, Math.PI / 8 * 7, Math.PI / 8) //<~ { x:0.2928932188134526, y:1.8477590650225735, z:-0.7071067811865476 }
GameVector3.fromPolar(2, Math.PI / -4, Math.PI / 4) //<~ { x:-0.9999999999999998, y:1.4142135623730951, z:1 }
GameVector3.fromPolar(2, Math.PI / -4, Math.PI / -4) //<~ { x:0.9999999999999998, y:1.4142135623730951, z:-1 }
GameVector3.fromPolar(2, Math.PI / 4, Math.PI / -4) //<~ { x:-0.9999999999999998, y:1.4142135623730951, z:-1 }
GameVector3.fromPolar(100, -Math.PI, Math.PI) //<~ { x:-1.4997597826618577e-30, y:-100, z:-1.2246467991473532e-14 }
GameVector3.fromPolar(100, -Math.PI, Math.PI / 2) //<~ { x:-1.2246467991473532e-14, y:6.123233995736766e-15, z:-100 }

评论区