数学隐藏的碰撞数学

由网友(佩奇家的小仙女)分享简介:我正在使用的瓷砖引擎(自制),我的下一个任务是创建一个AI(除了其他认可机构正在做)的一个项目,这是一个棘手,因为AI只能当场球员,如果玩家在AI的视线。计算范围(以砖)后,试图与它的周期为[1瓦= 32 * 32]。于是我想到了创建一个直线的方程。而我在这里,不解数学。任何想法我怎么能计算,如果是重叠的,这些隐...

我正在使用的瓷砖引擎(自制),我的下一个任务是创建一个AI(除了其他认可机构正在做)的一个项目,这是一个棘手,因为AI只能当场球员,如果玩家在AI的视线。计算范围(以砖)后,试图与它的周期为[1瓦= 32 * 32]。 于是我想到了创建一个直线的方程。而我在这里,不解数学。 任何想法我怎么能计算,如果是重叠的,这些隐藏的瓷砖呢? 请注意,我只是想用数学!_爱

  TileInfo.tileData [LA [楼(Y / 32)] [楼(X / 32)];
//拉 - >瓷砖位置阵列,如果是大于0再有就是瓷砖。
 

解决方案

说,观众在位置(X1,Y1)和目标(x2,y2)。现在,我假设有一组 N 沿沿连续砖X 以及。第一,这些瓦片的左下角的位置是(X0,Y0)。瓷砖的大小是 D 以及 X T 沿。现在算算:

连接查看器和目标线是

Y = Y1 +(Y2 - Y1)*(X - X1)/(X2 - X1)

瓷砖边角处点 P1 =(X0,Y0); P2 =(X0 +​​ N * D,Y0); P3 =(X0 +​​ N * D,Y0 + M * T); P4 =(X0,Y0 + M * T)。现在的任务是找到如果该线穿过任何的4段连接两个连续的弯道。让我们通过定义的P1和P2之间的线段(水平线)Y = Y0 。如果设置此进线公式,你可以找到可能的拦截 X ,我命名为

Y0 =(Y2 - Y1)*(十一 - X1)/(X2 - X1)+ Y1

您可以反转这个等式并找到possibx:

喜= X1 +(Y0 - Y1)*(X2 - X1)/(Y2 - Y1)

院部动态丨那些年被误解的数学

现在,如果喜> X0 喜< X0 + N * D 你有一个拦截该段。否则,你的视线自由行。

执行相同的其他三段,其直线通过 P2的定义 - > P3:X = X + N * D ; P3 - > P4:Y = Y0 + M * D ;和 P4 - > P1:X = X0

请注意,当段水平( Y =常数),你必须把这个的行视线直线,算出 X 和比较这 X 的拦截。如果该段是垂直的( X =常数),那么你必须把 X 的直线方程,计算并检查它是否落在区间与否。

最后一点是,你必须要特别注意的情况下,其中,X1 = X2 Y1 = Y2 。这是景点的垂直和水平线,并可能导致除以在上述等式为零。解决办法:处理这些案件分别

I'm working on a project that uses tile engine (self made), and my next task is to create an AI (besides other AIs that are done), this one is tricky because the AI should only spot the player if the player is in the AI's sight. Tried it with for cycles to after calculating the ranges (in tiles) [1 tile = 32*32]. Then I thought about creating an equation of a straight line. And here I am, puzzled in math. Any idea how could I calculate if it's overlaps one of these "hidden" tiles? NOTE that I want only use math!

TileInfo.tileData[la[floor(y / 32)][floor(x / 32)]];
//la -> array of tile positions, if it's >0 then there is a tile.

解决方案

Say that the viewer is at position (x1,y1) and the target at (x2,y2). Now, I am assuming that there is a set of n contiguous tiles along along x and m along y. The lower, left corner of the first of these tiles is at position (x0,y0). The size of tiles are d along x and t along y. Now the math:

The line connecting viewer and target is

y = y1 + (y2 - y1) * (x - x1) / (x2 - x1)

The tiles corners are at points p1 = (x0,y0); p2 = (x0 + n * d, y0); p3 = (x0 + n * d, y0 + m * t); p4 = (x0, y0 + m * t). Now the job is to find if that line crosses any of the 4 segments connecting two consecutive corners. Let's take the segment between p1 and p2 (a horizontal line) defined by y = y0. If you set this into the line equation you can find the possible interception x which I named xi:

y0 = (y2 - y1) * (xi - x1) / (x2 - x1) + y1

You can invert this equation and find the possibx:

xi = x1 + (y0 - y1) * (x2 - x1) / (y2 - y1)

Now if xi > x0 and xi < x0 + n * d you have an interception for this segment. Otherwise you have a free line of sight.

Do the same for the other three segments whose straight lines are defined by p2 -> p3: x = x0 + n * d; p3 -> p4: y = y0 + m * d; and p4 -> p1: x = x0.

Note that when the segment is horizontal (y = const) you have to put this y in the line of sight straight line, calculate x and compare this x with the intercept. If the segment is vertical (x = const) then you have to put x in the straight line equation, calculate y and check if it falls in the interval or not.

A final remark is that you have to take particular care of cases where x1 = x2 or y1 = y2. This are vertical and horizontal line of sights and may lead to division by zero in the above equations. The solution: deal with these cases separately.

阅读全文

相关推荐

最新文章