簡體   English   中英

在線性路徑中將對象從一個點移動到另一個點

[英]moving an object from point to point in a linear path

我試圖在屏幕上直線移動一個精靈,朝着我觸摸屏幕的位置,我做的是在每個循環中更新(),它檢查當前精靈的位置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++;

查看: http//en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

這個簡單的算法將告訴你兩點之間的一條線上的每個X,Y坐標。 您可以使用此算法計算它需要訪問的所有位置,將坐標存儲在數組中,並在更新位置時迭代數組。

來自文章:

  function line(x0, x1, y0, y1)
         int deltax := x1 - x0
         int deltay := y1 - y0
         real error := 0
         real deltaerr := abs (deltay / deltax)    // Assume deltax != 0 (line is not vertical),
               // note that this division needs to be done in a way that preserves the fractional part
         int y := y0
         for x from x0 to x1
             plot(x,y)
             error := error + deltaerr
             if error ≥ 0.5 then
                 y := y + 1
                 error := error - 1.0

這是最原始的版本。 本文包含一個更好的通用算法,您應該看一下。

我正在處理像你這樣的類似問題。 (我有一個arraylist持有我的玩家已經離開的位置的歷史,我想用它來回放游戲。)而不是簡單地用1增加x和y位置,你可以:

  1. 計算源位置和目標位置之間的角度。
  2. 使用表示速度的變量計算新方向
  3. 使用計算出的方向更新您的位置

我做了一類。 我希望它有用。

import java.awt.geom.Point2D;

public class MyVelocityCalculator {

    public static void main(String[] args) {
        Point2D.Double currentPosition = new Point2D.Double();
        Point2D.Double destinationPosition = new Point2D.Double();
        currentPosition.setLocation(100, 100);
        destinationPosition.setLocation(50, 50);
        Double speed = 0.5;
        Point2D.Double nextPosition = MyVelocityCalculator.getVelocity(currentPosition, destinationPosition, speed); 

        System.out.println("player was initially at: "+currentPosition);
        System.out.println("player destination is at: "+destinationPosition);
        System.out.println("half seconds later player should be at: "+nextPosition);

    }

    public static final Point2D.Double getVelocity(Point2D.Double currentPosition, Point2D.Double destinationPosition, double speed){
        Point2D.Double nextPosition = new Point2D.Double();
        double angle = calcAngleBetweenPoints(currentPosition, destinationPosition);
        double distance = speed;
        Point2D.Double velocityPoint = getVelocity(angle, distance);
        nextPosition.x = currentPosition.x + velocityPoint.x;
        nextPosition.y = currentPosition.y + velocityPoint.y;
        return nextPosition;
    }

    public static final double calcAngleBetweenPoints(Point2D.Double p1, Point2D.Double p2)
    {
        return Math.toDegrees( Math.atan2( p2.getY()-p1.getY(), p2.getX()-p1.getX() ) );
    }

    public static final Point2D.Double getVelocity(double angle, double speed){
        double x = Math.cos(Math.toRadians(angle))*speed;
        double y = Math.sin(Math.toRadians(angle))*speed;
        return (new Point2D.Double(x, y));
    }
}

不要使用整數。 使用整數是一個非常糟糕的主意。 使用浮動。 主要概念是:定義要執行(步數s )。 計算X和Y的差異( diffXdiffY )。 不要采用絕對值:以這種方式計算它們

float diffX = destX - currentX;

然后通過將diffXdiffY除以s (步數)來計算xMove和yMove值。

float moveX = diffX / s;
float moveY = diffY / s;

現在,您必須為每次迭代添加moveX和moveY值到當前位置。

為了繪制它,你應該使用支持浮點的Graphics2D 如果您不想使用Graphics2D,可以使用Math.round(float)將浮點數舍入為整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM