三个JS检查位置是否在对象后面

发布于 2025-01-11 06:04:42 字数 368 浏览 0 评论 0原文

我有一个 3D 角色以及地板上的几个预定义位置。有没有办法检查某个位置是否位于 3D 角色后面而不是前面或侧面?

我已经包含了我想要实现的目标的粗略草图(抱歉画得不好)。本质上,我想返回红线内红色圆圈的所有位置,并排除这两条线之外的所有其他圆圈。

输入图片此处描述

这可能吗?如果是这样,对我如何实现这一目标有什么建议吗?很抱歉,但我实际上不知道应该使用三个 JS 中的哪些函数来完成这样的事情,或者是否可能。

谢谢你!

I have a 3D character along with several predefined positions on the floor. Is there a way to check if a position is behind the 3D character and not in front or sideways?

I’ve included a rough sketch of what I’m trying to achieve (sorry for the poor drawing). Essentially I would like to return all of the positions of the red circles within the red lines and exclude all other circles outside of these two lines.

enter image description here

Is this possible? If so, is there any suggestion on how I can achieve this? I’m sorry but I don’t actually know which functions to use from Three JS for something like this or if it is possible.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2025-01-18 06:04:43

是的,这是可能的。

首先,您首先检查/圆圈是否位于玩家后面。您可以通过获取玩家所面对的方向(单位向量)和圆的方向向量(对其进行标准化,使其也是单位向量)之间的点积来实现此目的。如果值 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;
}

Yes, it's possible.

You start out by first checking if a point/circle is behind the player. You do this by getting the dot product between the direction player is facing (a unit vector) and the direction vector to the circle (normalize it so that it's also a unit vector). If the values dotProduct <= 0 then the circle is behind your player.

(unit vector means that your vector has a magnitude of one. Which is a fancy way of saying your x/y/z will never go beyond 1)

Code example

// 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;
}

Now that you know which circles are behind your player, you can get the circles within the cone. This is done by getting the angle between the player's position and the circle's position. Then check that the angle fits some criteria (e.g. the angle can't be more than 45deg from back of player).

// ...

// 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文