在线性路径上将物体从一点移动到另一点
我试图在屏幕上以直线移动精灵,朝向我触摸屏幕的位置,我所做的是在每个循环中的 update() 上,它检查当前精灵的位置xy == 到目的地 x ,y 。如果它没有精灵的 x++ 和 y++... 问题是..它不是沿直线移动...因为在某些情况下x或y坐标首先到达目的地x或y...我如何更改它以便x和y都满足一起到达目的地?
我当前的精灵对象的伪代码
destX = destination X
destY = destination Y
posX = current X
posY = current Y
public void update(){
if(destX > posX && destY < posY)
{
posX++;
posY--;
}
else if (destX > posX && destY > posY){
posX++;
posY++;
}
else if(destX < posX && destY > posY)
{
posX--;
posY++;
}
else if(destX < posX && destY < posY){
posX--;
posY--;
}
else if(destX < posX)
posX--;
else if(destX > posX)
posX++;
else if(destY < posY)
posY--;
else if(destY > posY)
posY++;
I'm trying to move a sprite across the screen in a straight line, towards on the location where've I touched the screen, what i did was upon the update() in each loop , it checks to see if the current sprite's location x y is == to the destination x ,y . if it hasn't sprite's x++ and y++...
the thing is ..it ain't moving in a straight line... as there are cases where the x or y coordinate reaches the destination x or y first... how do i changed it so that the both x and y meets the destination together?
my current pseudo code for the sprite object
destX = destination X
destY = destination Y
posX = current X
posY = current Y
public void update(){
if(destX > posX && destY < posY)
{
posX++;
posY--;
}
else if (destX > posX && destY > posY){
posX++;
posY++;
}
else if(destX < posX && destY > posY)
{
posX--;
posY++;
}
else if(destX < posX && destY < posY){
posX--;
posY--;
}
else if(destX < posX)
posX--;
else if(destX > posX)
posX++;
else if(destY < posY)
posY--;
else if(destY > posY)
posY++;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看:http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
简单的算法会告诉您两点之间的直线上的每个 X,Y 坐标。您可以使用此算法来计算它需要访问的所有位置,将坐标存储在数组中,并在更新位置时迭代该数组。
摘自文章:
这是最原始的版本。本文包含您应该查看的更好的通用算法。
Check out: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
This simple algorithm will tell you each X,Y coordinate on a line between two points. You could use this algorithm to compute all of the positions it needs to visit, store the coordinates in an array, and iterate over the array as you update the position.
From the Article:
This is the most primitive version. The article contains a better generalized algorithm that you should look at.
我正在处理与你类似的问题。 (我有一个数组列表,保存我的玩家所走位置的历史记录,我想用它来倒带游戏。)
而不是简单地将 x 和 y 位置增加 1
位置。
代表速度
我做了一个类。我希望它有用。
I am dealing with a similair problem as yours. (I have an arraylist holding the history of positions my player has gone and I want to use that to rewind the game.)
Instead of simply increasing x and y position with 1 you can:
position.
represents the speed
I made a class of that. I hope it is usefull.
不要使用整数。使用整数是一个非常糟糕的主意。使用漂浮物。主要概念是:定义要执行的步骤数(
s
)。计算 X 和 Y 的差异(diffX
和diffY
)。不要取绝对值:以这种方式计算它们然后通过将
diffX
和diffY
除以s
(步数)来计算 xMove 和 yMove 值)。现在,您必须为每次迭代添加 moveX 和 moveY 值到当前位置。
为了绘制它,您应该使用支持浮点的
Graphics2D
。如果您不想使用 Graphics2D,则可以使用Math.round(float)
将浮点数舍入为整数。Don't use integers. This is a very bad idea to work with ints. Use floats. The main concept is: define the number of steps you want to perform (
s
). Compute differences in X and Y (diffX
anddiffY
). Don't take absolute values: Compute them this wayThen compute the xMove and yMove values by dividing
diffX
anddiffY
bys
(number of steps).And now you have to add for each iteration the moveX and moveY values to the current position.
And for drawing it, you should use
Graphics2D
, which supports floating points. If you don't want to use Graphics2D, you can round the floats to ints, usingMath.round(float)
.