@CTR10

Как сделать движение объекта?

Как нормально сделать движение объекта к точкЕ и проверить, что он на месте? В какой-то момент объект просто начинает "трястиcь на месте" и не меняет точку. И в целом на новую позицию начинает движение очень странно.
move(x, y) {


            let dx = x - this.x;
            let dy = y - this.y;

            let angle = (180 * Math.atan(dy / dx)) / Math.PI;


            this.x = this.x + ((Math.cos(angle)) * this.speed);
            this.y = this.y + ((Math.sin(angle)) * this.speed);

            /*    if (x > this.x) this.x += x * this.speed;
                else this.x -= x * this.speed;

                if (y > this.y) this.y += y * this.speed;
      else this.y -= y * this.speed;

    */
            //    console.log("loc" + this.x + "/"+ this.y );


        }

   reshenie ()
        {
            let r;
            let x;
            let y;
            x = random(1,800);
            y = random(1,600);
            this.pos_x_next = x;
            this.pos_y_next = y;
            this.d="move";
            console.log( this.pos_x_next+"/"+this.pos_y_next);
        }

  function random(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; //Максимум не включается, минимум включается
    }





   function update() {


        for (var i = 0; i <= 18; i++) {


            let player1;
            player1 = players[i];


            /*   if (player.pos_x[i]>player.x&&player.x> player.pos_x[i]) {
                   player.next();
       alert(this.i);

               }*/


            let m;
            m = false;
            if (player1.d == "move") {


                if (player1.pos_x_next != Math.ceil(player1.x) && player1.pos_y_next != Math.ceil(player1.y)) {
                    m = true;
                }

            }

            if (m) {
                player1.move(player1.pos_x_next, player1.pos_y_next);
            } else {
                player1.d = "reshenie";
                player1.reshenie();
            }


            console.log(player1.d);

        }

    }
  • Вопрос задан
  • 97 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
summer Ярославль
от 100 000 до 140 000 ₽
КРАФТТЕК Санкт-Петербург
от 60 000 до 80 000 ₽