THREE.js - 尝试从点击生成的Vector 3中获取对象的最近点(Vector 3) [英] THREE.js - Trying to get nearest point (Vector 3) of object from click-generated Vector 3

查看:305
本文介绍了THREE.js - 尝试从点击生成的Vector 3中获取对象的最近点(Vector 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个点击式测试游戏并通过研究取得了大量进展,并找到了关于这个或那个的其他堆栈溢出答案,但这是我第一次看不到找到与我的问题相关的任何内容

I'm working on a point-and-click test game and have made a ton of progress through research and finding other stack overflow answers about this-or-that, but this is the first time I can't seem to find anything related to my question.

我的计划是将PatrolJS实现到我的项目中进行寻路,看起来在解决这个问题后我不应该有任何问题。我的环境以及场景中的一些NPC角色。我在3d空间中进行鼠标交互,能够选择和存储有关所点击内容的数据。我可以将我的角色移动到点击位置。我相信我的问题在于将点击注册到寻路区域。

My plan is to implement PatrolJS into my project for pathfinding, and it seems like I shouldn't have any problem getting it working after this issue is solved. I have my environment as well as some NPC characters in my scene. I have the mouse interaction in 3d space able to select and store data about what is clicked on. I am able to move my character to the click positions. I believe my problem will come in registering clicks into the pathfinding area.

这是我问题的直观表示:

Here a visual representation of my question:

在这里,您可以看到我的环境简化为一种颜色,以及我当前为navmesh区域定义的区域。玩家将能够点击3d环境/ NPC等上的任何位置与环境中的/查看/使用元素进行交互。点击工作已经完成,返回鼠标/光线与之相交的3d空间中的点,并向我提供有关我已定义的那些元素的任何信息。

Here, you can see my environment simplified down to one color, as well as the area I am currently defining for my navmesh area. The player will be able to click anywhere on the 3d environment/NPC's etc to interact with/view/use elements in the environment. That clicking works already, returning me with the point in 3d space that the mouse/ray intersects with, and giving me any information about those elements clicked on that I have defined.

我需要一些指导,是如何将单独的navmesh对象上最近的点返回到环境对象中单击的内容?假设我点击其中一个表格的某些部分。我希望能够在navmesh对象上找到绝对最接近的Vector 3点。这样,如果有人与可导航区域之外的元素进行交互,我仍然可以定义一个端点并将其提供给PatrolJS。

What I need some guidance with is, how can I get returned the most nearby point on the separate navmesh object to what was clicked in the environment object(s)? Say I click on some part of one of the tables. I want to be able to find the absolute closest Vector 3 point on the navmesh object. That way, if someone interacts with elements off of the navigable area, I can still define an endpoint and feed that into PatrolJS.

推荐答案

给定一个由 n 三角形构成的网格和一个点 p ,你可以找到距网格最近的点到 p O(n)中使用暴力,算法如下

Given a mesh made out of n triangles and a point p you can find the closest point from the mesh to p using brute force in O(n), the algorithm is as follows


  • 对于属于网格的每个三角形 t


    • 项目 p 到由 t 定义的飞机上,让 proj 是这一点

    • 如果 proj 位于 t 那么它就是最近点 p ,存储距 proj p

    • 如果 proj 位于三角形之外,则计算最近的点 proj 对于三角形的3个边中的每一个,将每个点的距离存储到 p

    • For each triangle t that belongs to the mesh
      • project p onto the plane defined by t, let proj be this point
      • if proj lies on t then it's the closest point to p, store the distance from proj to p
      • if proj lies outside the triangle then compute the closest point to proj for each of the 3 sides of the triangle, store the distances from each of these points to p

      这篇关于THREE.js - 尝试从点击生成的Vector 3中获取对象的最近点(Vector 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆