简体   繁体   English

C ++如何计算两个3D点之间的弧线

[英]C++ How to calculate an arc between two 3D points

I read through the forum and as I am sure this question has been asked before, but I couldn't really find what I was looking for. 我阅读了该论坛,并且确定已经问过这个问题了,但是我找不到真正想要的东西。

My problem is the following: 我的问题如下:

I have an AI-Character moving along a spline. 我有一个沿着花键移动的AI角色。 Should that path be blocked, the character should move in an arc around it and then continue on it's path. 如果该路径被阻塞,角色应该绕其弧线移动,然后沿其路径继续前进。

For arguments sake lets assume that the spline has a length of 7000 units. 为了论证,假设样条线的长度为7000个单位。

Therefore, I have two 3D (x,y,z) vectors. 因此,我有两个3D(x,y,z)向量。 The first vector is the current position of the AI-bot and the second vector the position past the obstacle. 第一个向量是AI-bot的当前位置,第二个向量是越过障碍物的位置。 For the time being lets just say: current spline position + 400 units; 现在暂时说:当前样条位置+ 400个单位; later on I could do a line trace to get the dimension of the obstacle etc. but for now I don't care about it. 稍后,我可以进行线迹跟踪以获取障碍物的尺寸等,但现在我不在乎它。

Now I would like to compute an alternative path to avoid aforementioned obstacle - hence compute the arc between these two points - How do I do this? 现在,我想计算一条替代路径以避免上述障碍-因此计算这两点之间的弧线-我该怎么做? I am really terrible at maths but looked at projectile trajectory because I thought that it would be sort of the same, just was unable to really understand it :< 我在数学方面确实很糟糕,但是看着弹丸的轨迹,因为我认为那是一样的,只是无法真正理解它:<

It doesn't have to be an arc. 不必一定是弧线。 You can solve this problem recursively in a very simple way. 您可以以非常简单的方式递归解决此问题。

Consider you're at position A, and the obstacle is at position B. You can do the following moves: 假设您在位置A,障碍物在位置B。您可以执行以下操作:

  • From current position to A+V(B[x]+height(B),0,0) 从当前位置到A+V(B[x]+height(B),0,0)
  • From current position to A+V(0,B[y]+width(B),0) 从当前位置到A+V(0,B[y]+width(B),0)
  • From current position to A+V(B[x]-height(B),0,0) 从当前位置到A+V(B[x]-height(B),0,0)

where V is a vector with components V(x,y,z), width(B) is the width of the obstacle and B[x] is the x component of the position of B. This way you moved around it along a rectangle. 其中V是具有分量V(x,y,z)的向量,width(B)是障碍物的宽度,B [x]是B位置的x分量。这样,您就沿着矩形移动了它。 You can now smoothen the path by subdividing that rectangle in halves. 现在,您可以通过将该矩形细分成两半来平滑路径。 3 subdivisions are enough to make this smooth enough. 3个细分足以使它足够平滑。 To subdivide, take the middle point the first path, and draw a line to the middle of the second path. 若要细分,请以第一个路径的中点为中心,然后在第二个路径的中点处画一条线。 The same you do from the second path to the third one, and now your rectangle becomes an octagon. 从第二条路径到第三条路径的操作相同,现在矩形变成一个八边形。 If that's not smooth enough, do a few more steps. 如果那还不够平滑,请再执行几个步骤。 This will create a new spline that you can use. 这将创建一个可以使用的新样条线。

I would look at a combination of splines and the EQS system. 我将研究样条曲线和EQS系统的组合。 The spline defines the ideal path to follow. 样条曲线定义了理想的轨迹。 The EQS system finds locations near or on the path, while still doing obstacle avoidance. EQS系统查找路径附近或路径上的位置,同时仍在避开障碍物。 EQS can return all valid destinations so you can manually order them by custom critera. EQS可以返回所有有效的目的地,因此您可以通过自定义critera手动订购它们。

Actors set on a spline do work, but there's a whole bunch o' mess when making them stop following a spline, creating a new one at the correct point, attaching the actor the new spline, and so on. 设置在样条线上的Actor可以工作,但是当使它们停止跟随样条线,在正确的位置创建新的,将actor附加新的样条线时,会产生一堆混乱。

I arrived at this conclusion yesterday after exactly going the messy way of adding spline points etc. The only problem i see is that I find the EQS system very difficult to understand. 昨天,我完全按照加样条点等的杂乱方式得出了这个结论。我看到的唯一问题是,我发现EQS系统很难理解。 Not following the examples as such, but modifying it in the way I need it. 不遵循这些示例,而是按照我需要的方式对其进行修改。 Lets see, i keep you posted. 让我们看看,我给你发帖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM