libgdx中矩形和圆形扇区之间的交点 [英] Intersection between Rectangle and a Circle sector in libgdx

查看:23
本文介绍了libgdx中矩形和圆形扇区之间的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 libgdx 编写游戏.对于敌人,我想编写一个简单的 AI.如果玩家曾经在视野范围内,该 AI 应该跟踪玩家.视野是一个扇形或二维锥体.达到这个目标的最有效方法是什么?我已经阅读了以下内容:圈内点

I am programming a game using libgdx. For the Enemies i want to write a simple AI. This AI should track the Player if he was in the field of Vision once. The field of Vision is a circle sector or 2d cone. What would be the most efficient way to reach this? I have allready read the following: Point in circle sector

我可以将它用于播放器的每个角点,但我也有给定方向和角度的视野(direction - angle/2direction + angle/之间的扇区)2).角度不会超过 180(因为这对算法来说是个问题).为游戏中的每个敌人计算扇区开始和结束的向量会不会有问题?最好的解决方案是什么,我如何计算给定方向和角度之外的向量?

I could use this with every Corner Point of my Player but i also have the field of Vision given with direction and angle (sector between direction - angle/2 to direction + angle/2). The angle won't be over 180 (as this would be a problem for the algorithm). Would it be a Problem to calculate the Vectors for start and end of sector for every enemie in the game? What would be the best solution and how can i calculate the Vectors out of the given direction and angle?

推荐答案

您可以使用链接帖子中描述的解决方案,但您必须通过计算 dir +/的正弦和余弦将角度转换为向量- 角度/2.链接的解决方案讨论整数算术,但您的向量将是浮点数:

You can use the solution described in the linked post, but you have to convert your angles to vectors by calculating the sine and cosine of dir +/- angle/2. The linked solution talks about integer arithmetic, but your vectors will be floats:

arm1 = [cos(d - 0.5 * a), sin(d - 0.5 * a)]
arm2 = [cos(d + 0.5 * a), sin(d + 0.5 * a)]

cossin 的参数以弧度为单位.x 方向上零点的角度,即传统笛卡尔系统中的东.

The arguments to cos and sin are in radians. An angle of zero points in the x direction, i.e. east in a traditional Cartesian system.

为了不做不必要的工作,你应该先检查容易的事情.

In order not to do unecessary work, you should check the easy things first.

在检查与扇区的交集之前,我会做一个简单的框检查:如果矩形的所有四个角点都在敌人周围的方框之外,则没有交集:

I would do a simple box check before checking for intersection with the sector: If all four corner points of your rectangle are outside the square box around the enemy, there is no intersection:

if (x0 < x-r && x1 < x-r && x2 < x-r && x3 < x-r) return false;
if (x0 > x+r && x1 > x+r && x2 > x+r && x3 > x+r) return false;
if (y0 < y-r && y1 < y-r && y2 < y-r && y3 < y-r) return false;
if (y0 > y+r && y1 > y+r && y2 > y+r && y3 > y+r) return false;

这个简单的检查应该可以排除很多敌人.

This simple check should rule out many enemies.

接下来,按照链接答案中的描述检查所有四个点的半径,如果您的角点都不在半径内,则返回 false.

Next, check the radius for all four points as described in the linked answer and return false if none of your corner points is within the radius.

现在我们才需要用三角函数计算向量.你可以用上面给出的公式来做到这一点.如果你已经有了敌人和角度的正弦和余弦,你可以使用 加法定理加速向量计算:

It is only now that we have to calculate the vectors with the trig functions. You can do this with the formula given above. If you have the sine and cosine of the enemy and angle already, you can use addition theoremes to speed up vector calculation:

# sin_d = sin(d), cos_d = cos(d)
# sin_a = sin(0.5 * a), cos_a = cos(0.5 * a)

arm1 = [cos_d * cos_a + sin_d * sin_a, sin_d * cos_a - cos_d * sin_a]
arm2 = [cos_d * cos_a - sin_d * sin_a, sin_d * cos_a + cos_d * sin_a]

对于每个敌人来说,视角可能是一个常数,因此您可以预先计算它,并且您可能已经预先计算了 cos_dsin_d.

The viewing angle is probably a constant for each enemy, so you can precalculate it, and you might already have cos_d and sin_d precomputed as well.

如果你不关心精度,也不想进入弧度,你可以预先计算一度步长的正弦和余弦,并将它们存储在两个大小为 360 的数组中.然后计算正弦将成为一个问题查找数组,即它会很快,并且您可以进行 n 度的所有计算.(但要注意环绕:5° - 15° 应该是 350°,而不是 -10°.)

If you are not concerned with accuracy and don't want to get into radians, you could precompute the sines and cosines for one-degree steps and store them in two arrays of size 360. Then calculating the sine will become a matter of looking up an array, i.e. it will be fast, and you can do all your calculations n degrees. (But take care of wrapping: 5° - 15° should then be 350°, not -10°.)

这篇关于libgdx中矩形和圆形扇区之间的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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