小编Shi*_*u18的帖子

在线性路径中将对象从一个点移动到另一个点

我试图在屏幕上直线移动一个精灵,朝着我触摸屏幕的位置,我做的是在每个循环中更新(),它检查当前精灵的位置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 …
Run Code Online (Sandbox Code Playgroud)

java algorithm pathing coordinates game-physics

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

algorithm ×1

coordinates ×1

game-physics ×1

java ×1

pathing ×1