C
C
CTR102021-01-27 00:18:51
JavaScript
CTR10, 2021-01-27 00:18:51

How to make an object move?

How to normally make an object move to a point and check that it is in place? At some point, the object just starts to "shake in place" and does not change the point. And in general, the movement to a new position is very strange.

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);

        }

    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question