三个JS检查位置是否在对象后面
Three JS check if position is behind object
我有一个 3D 角色以及地板上的几个预定义位置。有没有办法检查一个位置是否在 3D 角色的后面而不是前面或侧面?
我已经附上了我想要实现的目标的粗略草图(抱歉画得不好)。本质上,我想 return 红线内所有红圈的位置,并排除这两条线外的所有其他圆圈。
这可能吗?如果是这样,对我如何实现这一目标有什么建议吗?很抱歉,我实际上不知道要使用 Three JS 中的哪些函数来做这样的事情,或者是否可能。
谢谢!
是的,有可能。
您首先 检查 是否有 点/圆 在玩家后面 .您可以通过获取玩家面对的方向(单位向量)和圆的方向向量(对其进行归一化,使其也是单位向量)之间的 点积 来实现。如果值 dotProduct <= 0
那么圆圈在你的玩家后面。
(单位向量意味着你的向量的大小为 1。这是一种奇特的说法,你的 x/y/z 永远不会超过 1)
代码示例
// Let's assume that the following code is in some sort of loop, yes
// Get the direction vector to the circle
const directionVect = circle.position.clone().sub(player.position).normalize();
// If your player is a camera you can get the direction like so
const playerFacing = player.getWorldDirection(new THREE.Vector3());
// Orientation
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
// ...to be continued...
} else {
return;
}
既然您知道玩家身后有哪些圆圈,您就可以得到圆锥体内的圆圈。这是通过获取玩家位置和圆圈位置之间的角度来完成的。然后检查角度是否符合某些标准(例如,角度不能超过玩家背部的 45 度)。
// ...
// Orientation
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
const angle = player.position.angleTo(circle.position);
if (angle < Math.PI * 0.25) {
// Do something with circle
}
} else {
return;
}
我有一个 3D 角色以及地板上的几个预定义位置。有没有办法检查一个位置是否在 3D 角色的后面而不是前面或侧面?
我已经附上了我想要实现的目标的粗略草图(抱歉画得不好)。本质上,我想 return 红线内所有红圈的位置,并排除这两条线外的所有其他圆圈。
这可能吗?如果是这样,对我如何实现这一目标有什么建议吗?很抱歉,我实际上不知道要使用 Three JS 中的哪些函数来做这样的事情,或者是否可能。
谢谢!
是的,有可能。
您首先 检查 是否有 点/圆 在玩家后面 .您可以通过获取玩家面对的方向(单位向量)和圆的方向向量(对其进行归一化,使其也是单位向量)之间的 点积 来实现。如果值 dotProduct <= 0
那么圆圈在你的玩家后面。
(单位向量意味着你的向量的大小为 1。这是一种奇特的说法,你的 x/y/z 永远不会超过 1)
代码示例
// Let's assume that the following code is in some sort of loop, yes
// Get the direction vector to the circle
const directionVect = circle.position.clone().sub(player.position).normalize();
// If your player is a camera you can get the direction like so
const playerFacing = player.getWorldDirection(new THREE.Vector3());
// Orientation
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
// ...to be continued...
} else {
return;
}
既然您知道玩家身后有哪些圆圈,您就可以得到圆锥体内的圆圈。这是通过获取玩家位置和圆圈位置之间的角度来完成的。然后检查角度是否符合某些标准(例如,角度不能超过玩家背部的 45 度)。
// ...
// Orientation
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
const angle = player.position.angleTo(circle.position);
if (angle < Math.PI * 0.25) {
// Do something with circle
}
} else {
return;
}