使用Java如何绘制精灵的直线路径?

发布于 2024-08-28 20:20:08 字数 447 浏览 7 评论 0原文

为了说明这一点,我目前有类似的情况,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2024-09-04 20:20:08

如果 T 是从 X0,Y0 移动到 X1,Y1 所需的时间,则任意给定时间 t (0 <= t <= T) 处的位置为:

X = X0 + t*(X1-X0)/T
Y = Y0 + t*(Y1-Y0)/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:

X = X0 + t*(X1-X0)/T
Y = Y0 + t*(Y1-Y0)/T
晨光如昨 2024-09-04 20:20:08

根据您扩展的问题,听起来您有兴趣访问开始位置和结束位置之间的每个像素。在这种情况下,您可能需要查看 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.

我最亲爱的 2024-09-04 20:20:08

通常,您的精灵将具有所需的速度,并将根据其速度和自上次更新以来经过的时间来更新其位置,例如,

public class MySprite {
  int x, y; // Location
  int dx, int dy; // Velocity in pixels / millisecond

  public void updatePosition(long deltaMillis) {
    // Update x and y position based on current velocity.
    x += dx * deltaMillis;
    y += dy * deltaMillis;
  }
}

根据机器的速度,您的动画循环将运行得更快或更慢。但是,由于您的更新基于预先指定的速度,因此这不会影响精灵在屏幕上移动的速度。

public void animationLoop() {
  long prev = System.currentTimeMillis();; 
  long now;
  long deltaMillis;

  while (animationRunning) {
    // Record ellapsed time.
    now = System.currentTimeMillis();
    deltaMillis = now - prev;
    prev = now;

    if (deltaMillis > 0L) { // Some time has passed so move sprite.
      sprite.updatePosition(deltaMillis);
    }

    // TODO: Repaint sprite.
  }
}

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.

public class MySprite {
  int x, y; // Location
  int dx, int dy; // Velocity in pixels / millisecond

  public void updatePosition(long deltaMillis) {
    // Update x and y position based on current velocity.
    x += dx * deltaMillis;
    y += dy * deltaMillis;
  }
}

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.

public void animationLoop() {
  long prev = System.currentTimeMillis();; 
  long now;
  long deltaMillis;

  while (animationRunning) {
    // Record ellapsed time.
    now = System.currentTimeMillis();
    deltaMillis = now - prev;
    prev = now;

    if (deltaMillis > 0L) { // Some time has passed so move sprite.
      sprite.updatePosition(deltaMillis);
    }

    // TODO: Repaint sprite.
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文