使用Java如何绘制精灵的直线路径?
为了说明这一点,我目前有类似的情况,
int startX,endX,currX;
int startY,endY,currY;
public void updatePos(){
if(currX<=endX){
currX+=1;
}
//Same for y
我可以看到我不想每次移动 x 和 y 相同的量,但不知道如何计算出我应该做什么来确定每个应该调整多少。
任何想法
也许这将有助于澄清
想象我从这些坐标开始 (0,0) 我想要去 (18,10)
如果我增加 x 和 y 直到它们以 1 为步长到达所需的目标,则精灵会沿对角线向上移动,然后穿过屏幕向右移动,然后停留在正确的位置。
我想要的行为是它朝目的地点单方向行进(如果愿意的话,沿着三角形的斜率)
我正在寻找有关如何找到下一组点的建议
To illustrate I currently have something like
int startX,endX,currX;
int startY,endY,currY;
public void updatePos(){
if(currX<=endX){
currX+=1;
}
//Same for y
I can see that I dont want to move x and y the same amount each time but dont know how to work out what I should do to determine how much each should be adjusted.
Any ideas
Maybe this will help clarify
Imagine I start at these coords
(0,0)
and I want to get to
(18,10)
if I increment my x and y until they reach the desired target by a step of one the sprite moves diagonnally upwards then across the screen to the right before resting in the correct position.
The behaviour I want is that it travels a single direction towards the destination point (along the splope of a triangle if you will)
Im looking for suggestions on how to find my next set of points
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 T 是从 X0,Y0 移动到 X1,Y1 所需的时间,则任意给定时间 t (0 <= t <= T) 处的位置为:
If T is the time that is needed to move from X0,Y0 to X1,Y1, the position at any given time t (0 <= t <= T) is:
根据您扩展的问题,听起来您有兴趣访问开始位置和结束位置之间的每个像素。在这种情况下,您可能需要查看 Bresenham 线算法,它描述了如何来做到这一点。
Based on your expanded question it sounds like you're interested in visiting every pixel between the start and end location. In this case you might want to take a look at Bresenham's Line Algorithm, which describes how to do this.
通常,您的精灵将具有所需的速度,并将根据其速度和自上次更新以来经过的时间来更新其位置,例如,
根据机器的速度,您的动画循环将运行得更快或更慢。但是,由于您的更新基于预先指定的速度,因此这不会影响精灵在屏幕上移动的速度。
Typically you will have a required velocity for your sprite and will update its position based on its velocity and the time ellapsed since it was last updated, e.g.
Depending on the speed of your machine your animation loop will run faster or slower. However, as your updates are based on a pre-specified velocity this will not affect the speed of the sprite moving across the screen.