如何在点数组之间可靠地移动角色?
我目前有一个来自 A* 寻路函数结果的点数组 (x,y)。该数组的设置使得第一个索引是最接近字符的点,下一个索引是路径中需要行进的下一个点。
这些点位于网格上(当前网格上每个点之间有 8 个像素,这是可以更改的。)
我当前的策略是将角色移动到第一个点,当他到达该点时,然后移动到下一个点,就这么简单。
我通过创建从角色到该点的向量来移动到第一个点,并找出它有多远。然后我找到角度并朝那个方向移动。
// How the character is moved
double xMove = Math.cos(charAngle * (Math.PI / 180)) * 0.1 * delta;
double yMove = Math.sin(charAngle * (Math.PI / 180)) * 0.1 * delta;
x += xMove;
y += yMove;
absVx -= Math.abs(xMove);
absVy -= Math.abs(yMove);
absVX
和 absVy
存储剩余的绝对距离,并且每次移动后都会递减。如果它们都小于或等于零,那么我们已经到达目的地,我们将其从数组中删除,并使用下一个点再次重新计算所有内容。
我有两个问题。
有时角色会跳过一个点,然后他就永远旅行。这很可能是由于游戏引擎增加了运动(光滑)的增量,并且角色移动得太远,但我不确定这一点。
当我单击目的地时,效果很好,但我需要的是能够按住鼠标按钮。目前,如果我按住鼠标,角色只会在第一个点上“静止”。
我有一个解决第二个问题的想法,但我不确定这是个好主意。它存储从最后一次鼠标单击开始您想要前往的位置,但直到您经过要移动到的点为止才实际计算它。
所以,我希望有人真正聪明和有趣可以和我就此进行一些对话:D
I currently have an array of points (x,y) from the result of an A* pathfinding function. This array is set up so that the first index is the point closest to the character and the next one is the next point that needs to be traveled in the path.
These points are on a grid (currently 8 pixels between each point on the grid, this is changable.)
My current strategy is to move the character to the first point, when he has arrived at that point, then move to the next one, simple as that.
I move to the first point by creating a vector from the character to that point and find how far it is. Then I find the angle and move in that direction.
// How the character is moved
double xMove = Math.cos(charAngle * (Math.PI / 180)) * 0.1 * delta;
double yMove = Math.sin(charAngle * (Math.PI / 180)) * 0.1 * delta;
x += xMove;
y += yMove;
absVx -= Math.abs(xMove);
absVy -= Math.abs(yMove);
absVX
and absVy
store the absolute distance left to go and it is decremented after each movement. If they are both below or equal to zero, then we have reached our destination, we remove it from the array and recalculate everything again with the next point.
I have two issues.
Sometimes the character skips over a point and then he just travels forever. This is most likely due to the delta the game engine adds to the movement (slick) and the character moves too far, I'm not sure on this one though.
This works fine when I single click the destination but what I need is to be able to hold down the mouse button. Currently the character just "stands still" on the first point if I hold the mouse down.
I had an idea for a solution to the 2nd problem but I'm not sure it's a good one. It is to store the location you want to go from the last mouse click but not actually calculate it untill you have passed the point you were moving to.
So, I hope someone really smart and fun can have a little conversation with me about this :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于1,最可能的原因是舍入问题;添加这些 xMove / yMove 会引入一个小错误,并且 x 和 y 永远不会真正获得目的地的值,因此算法会继续运行。
解决方法:
1 定义“到达目的地”,使其允许一定程度的误差
2 找出角色到达目的地需要多少“滴答声”,当它“到达”时,强制角色位置到达目的地(这允许纠正累积误差)。
另外,我会回到图形理论。 IIRC,您在点之间移动的路径应该等于 Bresenham 的画线算法。在后面的阶段,您的角色可以沿着他的路径遵循 B 样条线。
About 1, the most probably cause is a rounding issue; adding these xMove / yMove introduce an small error and the x and y never really get the value of the destination, so the algorithm keeps running.
Ways to solve:
1 Define "reaching destination" so it allows a degree of error
2 Find out how many "ticks" will take to the character to arrive to destination, when it "arrives", force the character position to its destination (this allows to correct accumulated error).
Also, I would go back to graphics theory. IIRC, your path moving between points should be equal to Bresenham's line drawing algorithm. For later stages, your character could follow a B-Spline along his path.