➡️ 向量(Vector)#
#bs.vector:help
向量是管理运动、力和……嗯……做物理的基础且强大的工具!
观看演示
“有了向量,物理学找到了一种优美的语言。”
—理查德·费曼(Richard Feynman)
🔧 函数#
你可以在下方找到此模块中的所有可用函数。
绝对值最大#
- #bs.vector:abs_max
获取距离零最远的数,不考虑符号。
- 输入:
分数
$vector.abs_max.[0,1,2] bs.in:向量分量。- 输出:
返回值 | 分数
$vector.abs_max bs.out:离0最远的分量值。
示例:我想获取向量(1000, 2000, 3000)的最大分量:
# Define the vector
scoreboard players set $vector.abs_max.0 bs.in 1000
scoreboard players set $vector.abs_max.1 bs.in 2000
scoreboard players set $vector.abs_max.2 bs.in 3000
# Get the max component
function #bs.vector:abs_max
# Display the result
tellraw @a [{"text":" Max component: ","color":"dark_gray"},{"score":{"name":"$vector.abs_max","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome、Leirof
绝对值最小#
- #bs.vector:abs_min
获取离零最近的数,不考虑符号。
- 输入:
分数
$vector.abs_min.[0,1,2] bs.in:向量分量。- 输出:
返回值 | 分数
$vector.abs_min bs.out:离0最近的分量值。
示例:我想获取向量(1000, 2000, 3000)的最小分量:
# Define the vector
scoreboard players set $vector.abs_min.0 bs.in 1000
scoreboard players set $vector.abs_min.1 bs.in 2000
scoreboard players set $vector.abs_min.2 bs.in 3000
# Get the min component
function #bs.vector:abs_min
# Display the result
tellraw @a [{"text":" Min component: ","color":"dark_gray"},{"score":{"name":"$vector.abs_min","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome
3D基底旋转#
- #bs.vector:basis_rot_3d {scaling:<value>}
获取传入参数向量在另一旋转基下的等效向量,可用于将绝对/相对坐标转换为局部坐标系中的位置。
- 输入:
分数
$vector.basis_rot_3d.pos.[0,1,2] bs.in:在起始基底中的向量坐标 \(=(X,Y,Z)\)。分数
$vector.basis_rot_3d.rot.[0,1] bs.in:相对于起始基底的水平角 \(=\phi\)(沿 \(=\hat{y}\) 轴)和垂直角 \(=\theta\)(沿 \(=\hat{\phi}\) 轴)旋转(以度为单位)。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
分数
$vector.basis_rot_3d.[0,1,2] bs.out:在目标基底中的向量坐标 \(=(X',Y',Z')\)。
基底系统
此系统使用Minecraft坐标系。因此:
\(\hat{y}\) 是垂直轴。
\(\phi=0\)(水平角的起点)是沿着 \(\hat{z}\) 轴的。
\(\theta=0\)(垂直角的起点)在水平平面上。
示例:一个方块相对于我的位置在~2 ~5 ~10,我想获取这个位置的局部坐标(^? ^? ^?):
# One time
# Relative coordinates (we multiply by 1000 to have more precision on the result, which will also be multiplied by 1000)
scoreboard players set $vector.basis_rot_3d.pos.0 bs.in 2000
scoreboard players set $vector.basis_rot_3d.pos.1 bs.in 5000
scoreboard players set $vector.basis_rot_3d.pos.2 bs.in 10000
# Difference between my rotation (= that of the coordinate grid ^X ^Y ^Z) and the rotation of the Minecraft blocks grid (~X ~Y ~Z)
function #bs.position:get_rot {scale:1000}
scoreboard players operation $vector.basis_rot_3d.rot.0 bs.in = @s bs.rot.h
scoreboard players operation $vector.basis_rot_3d.rot.1 bs.in = @s bs.rot.v
# Perform the basic rotation
function #bs.vector:basis_rot_3d {scaling:1000}
# See the result
tellraw @a [{"text": "X = ", "color": "dark_gray"},{"score":{"name":"$vector.basis_rot_3d.0", "objective": "bs.out"}, "color": "gold"},{"text":", Y = ", "color": "dark_gray"},{"score":{"name":"$vector.basis_rot_3d.1", "objective": "bs.out"},"color":"gold"},{"text":", Z = ","color":"dark_gray"},{"score":{"name":"$vector.basis_rot_3d.2","objective":"bs.out"},"color":"gold"}]
示例:我想要一个指向我正在看的方向的向量,但用相对坐标~X ~Y ~Z表示:
# Once
# Retrieve a vector ^ ^ ^1 corresponding to a vector directed according to the orientation of the entity (we multiply by 1000 to have more precision on the result, which will also be multiplied by 1000)
scoreboard players set $vector.basis_rot_3d.pos.0 bs.in 0
scoreboard players set $vector.basis_rot_3d.pos.1 bs.in 0
scoreboard players set $vector.basis_rot_3d.pos.2 bs.in 1000
# Get the orientation
function #bs.position:get_rot {scale:1000}
scoreboard players operation $vector.basis_rot_3d.rot.0 bs.in = @s bs.rot.h
scoreboard players operation $vector.basis_rot_3d.rot.1 bs.in = @s bs.rot.v
# Reversal of the orientation since we want to have the relative orientation of the game grid compared to the orientation of the player (unlike the previous example)
scoreboard players operation $vector.basis_rot_3d.rot.0 bs.in *= -1 bs.const
scoreboard players operation $vector.basis_rot_3d.rot.1 bs.in *= -1 bs.const
# Perform the basic rotation
function #bs.vector:basis_rot_3d {scaling:1000}
# See the result
tellraw @a [{"text": "X = ", "color": "dark_gray"},{"score":{"name":"$vector.basis_rot_3d.0", "objective": "bs.out"}, "color": "gold"},{"text":", Y = ", "color": "dark_gray"},{"score":{"name":"$vector.basis_rot_3d.1", "objective": "bs.out"},"color":"gold"},{"text":", Z = ","color":"dark_gray"},{"score":{"name":"$vector.basis_rot_3d.2","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome、Leirof
笛卡尔坐标转球坐标#
- #bs.vector:cartesian_to_spherical {scaling:<value>}
将笛卡尔坐标转换为球坐标。
- 输入:
分数
$vector.cartesian_to_spherical.[0,1,2] bs.in:表示笛卡尔坐标 \(=(X,Y,Z)\) 的向量。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
分数
$vector.cartesian_to_spherical.[0,1,2] bs.out:表示球坐标 \(=(H,V,R)\) 的向量。
示例:我想将向量(1000, 2000, 3000)转换为球坐标:
# Define the vector
scoreboard players set $vector.cartesian_to_spherical.0 bs.in 1000
scoreboard players set $vector.cartesian_to_spherical.1 bs.in 2000
scoreboard players set $vector.cartesian_to_spherical.2 bs.in 3000
# Perform the conversion
function #bs.vector:cartesian_to_spherical {scaling:1000}
# Display the result
tellraw @a [{"text":"Spherical coordinates: ","color":"dark_gray"},{"score":{"name":"$vector.cartesian_to_spherical.0","objective":"bs.out"},"color":"gold"},{"text":"°, ","color":"gold"},{"score":{"name":"$vector.cartesian_to_spherical.1","objective":"bs.out"},"color":"gold"},{"text":"°, ","color":"gold"},{"score":{"name":"$vector.cartesian_to_spherical.2","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome
叉乘#
- #bs.vector:cross_product {scaling:<value>}
计算 \(u\) 和 \(v\) 的叉乘。
- 输入:
分数
$vector.cross_product.u.[0,1,2] bs.in:第一个向量分量。分数
$vector.cross_product.v.[0,1,2] bs.in:第二个向量分量。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
分数
$vector.cross_product.[0,1,2] bs.out:操作结果 \(=u \times v\)。
示例:我想对 \(u=(1,2,3)\) 和 \(v=(4,5,6)\) 计算 \(u \times v\):
# Define the vectors
scoreboard players set $vector.cross_product.u.0 bs.in 100
scoreboard players set $vector.cross_product.u.1 bs.in 200
scoreboard players set $vector.cross_product.u.2 bs.in 300
scoreboard players set $vector.cross_product.v.0 bs.in 400
scoreboard players set $vector.cross_product.v.1 bs.in 500
scoreboard players set $vector.cross_product.v.2 bs.in 600
# Perform the operation
function #bs.vector:cross_product {scaling:100}
# Display the result
tellraw @a [{"text":"Cross product: ","color":"dark_gray"},{"score":{"name":"$vector.cross_product.0","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.cross_product.1","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.cross_product.2","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome、Majoras16
点乘#
- #bs.vector:dot_product {scaling:<value>}
计算 \(u\) 和 \(v\) 的点乘。
- 输入:
分数
$vector.dot_product.u.[0,1,2] bs.in:第一个向量分量。分数
$vector.dot_product.v.[0,1,2] bs.in:第二个向量分量。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
分数
$vector.dot_product bs.out:运算结果 \(=u · v\)。
示例:我想对 \(u=(1,2,3)\) 和 \(v=(4,5,6)\) 计算 \(u \cdot v\):
# Define the vectors
scoreboard players set $vector.dot_product.u.0 bs.in 100
scoreboard players set $vector.dot_product.u.1 bs.in 200
scoreboard players set $vector.dot_product.u.2 bs.in 300
scoreboard players set $vector.dot_product.v.0 bs.in 400
scoreboard players set $vector.dot_product.v.1 bs.in 500
scoreboard players set $vector.dot_product.v.2 bs.in 600
# Perform the operation
function #bs.vector:dot_product {scaling:100}
# Display the result
tellraw @a [{"text":"Dot product: ","color":"dark_gray"},{"score":{"name":"$vector.dot_product","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome、Majoras16
长度#
- #bs.vector:length
计算向量的范数。
- 输入:
分数
$vector.length.[0,1,2] bs.in:向量分量。- 输出:
返回值 | 分数
$vector.length bs.out:向量长度。
示例:计算向量的长度:
scoreboard players set $vector.length.0 bs.in 1000
scoreboard players set $vector.length.1 bs.in 2000
scoreboard players set $vector.length.2 bs.in 3000
function #bs.vector:length
# Display the result
tellraw @a [{"text":" Vector length: ","color":"dark_gray"},{"score":{"name":"$vector.length","objective":"bs.out"}}]
性能提示
为了最小化性能影响,我们建议尽可能使用 length_squared 函数代替此函数。计算向量的长度需要进行平方根运算,这在计算上很耗费资源,特别是在 Minecraft 中。
length_squared 在以下情况下可以有效使用:
当比较向量长度与给定值时,手动计算给定值的平方并与
length_squared的结果进行比较。当比较两个向量的长度时,比较它们的
length_squared结果而不是计算它们的实际长度。
- #bs.vector:length_squared {scaling:<value>}
计算向量的范数的平方。
- 输入:
分数
$vector.length_squared.[0,1,2] bs.in:向量分量。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
返回值 | 分数
$vector.length_squared bs.out:向量的长度的平方。
示例:计算向量的长度的平方:
scoreboard players set $vector.length_squared.0 bs.in 1000
scoreboard players set $vector.length_squared.1 bs.in 2000
scoreboard players set $vector.length_squared.2 bs.in 3000
function #bs.vector:length_squared
# Display the result
tellraw @a [{"text":" Vector length squared: ","color":"dark_gray"},{"score":{"name":"$vector.length_squared","objective":"bs.out"}}]
制作人员:Aksiome、Leirof
归一化#
- #bs.vector:normalize {scale:<scaling>}
将向量的长度缩放到指定值,并保持其方向不变。
- 输入:
分数
$vector.normalize.[0,1,2] bs.in:向量分量。函数宏:
参数
scale: 应用于函数输出的缩放系数。
- 输出:
分数
$vector.normalize.[0,1,2] bs.out:归一化后的向量分量。
性能提示
向量归一化并不总是需要使用长度。通常可以使用 normalize_max_component 函数代替。虽然这种方法不会归一化长度,但它简化了操作并提高了性能。
示例:将向量(1000, 2000, 3000)按比例1000归一化:
# Define the vector
scoreboard players set $vector.normalize.0 bs.in 1000
scoreboard players set $vector.normalize.1 bs.in 2000
scoreboard players set $vector.normalize.2 bs.in 3000
# Perform the normalization
function #bs.vector:normalize {scale:1000}
# Display the result
tellraw @a [{"text":"Normalized vector: ","color":"dark_gray"},{"score":{"name":"$vector.normalize.0","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.normalize.1","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.normalize.2","objective":"bs.out"},"color":"gold"}]
- #bs.vector:normalize_max_component {scale:<scaling>}
将向量的所有分量除以最大分量值,并保持其方向不变。
- 输入:
分数
$vector.normalize_max_component.[0,1,2] bs.in:向量分量。函数宏:
参数
scale: 应用于函数输出的缩放系数。
- 输出:
分数
$vector.normalize_max_component.[0,1,2] bs.out:归一化后的向量分量。分数
$vector.normalize_max_component.factor bs.out:归一化因子。
示例:将向量(1000, 2000, 3000)按比例1000归一化:
# Define the vector
scoreboard players set $vector.normalize_max_component.0 bs.in 1000
scoreboard players set $vector.normalize_max_component.1 bs.in 2000
scoreboard players set $vector.normalize_max_component.2 bs.in 3000
# Perform the fast normalization
function #bs.vector:normalize_max_component {scale:1000}
# Display the result
tellraw @a [{"text":"Normalized vector: ","color":"dark_gray"},{"score":{"name":"$vector.normalize_max_component.0","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.normalize_max_component.1","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.normalize_max_component.2","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome、Leirof
球坐标转笛卡尔坐标#
- #bs.vector:spherical_to_cartesian {scaling:<value>}
将球坐标转换为笛卡尔坐标。
- 输入:
分数
$vector.spherical_to_cartesian.[0,1,2] bs.in:表示球坐标 \(=(H,V,R)\) 的向量。函数宏:
参数
scaling: 应用于函数输入和输出的缩放系数。
- 输出:
分数
$vector.spherical_to_cartesian.[0,1,2] bs.out:表示笛卡尔坐标 \(=(X,Y,Z)\) 的向量。
示例:我想将球坐标 \((45°, 30°, 1)\) 转换为笛卡尔坐标:
# Define the spherical coordinates
scoreboard players set $vector.spherical_to_cartesian.0 bs.in 45000
scoreboard players set $vector.spherical_to_cartesian.1 bs.in 30000
scoreboard players set $vector.spherical_to_cartesian.2 bs.in 1000
# Perform the conversion
function #bs.vector:spherical_to_cartesian {scaling:1000}
# Display the result
tellraw @a [{"text":"Cartesian coordinates: ","color":"dark_gray"},{"score":{"name":"$vector.spherical_to_cartesian.0","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.spherical_to_cartesian.1","objective":"bs.out"},"color":"gold"},{"text":", ","color":"gold"},{"score":{"name":"$vector.spherical_to_cartesian.2","objective":"bs.out"},"color":"gold"}]
制作人员:Aksiome
💬 这对你有帮助吗?
欢迎在下方留下你的问题和反馈!