@kaliboba

Почему не работает коллизия круга?

Делаю коллизию круга, с помощью функции detectBorder, которая на клиенте работает отлично, все правильно просчитывается, но он не сталкивается с границей экрана.
Код функции:
function detectBorder()
{
  try
  {
    if(player.cx + player.radius > canvas.width || player.cx - player.radius <= 0){ 
    player.s = -player.s;
    console.log("X");
    }
    if(player.cy + player.radius > canvas.height || player.cy - player.radius <= 0){ 
      player.s = -player.s;
    console.log("Y");
    }
  }
  catch (error) {
    console.error("Ошибка: ", error);
}
  
}

По задумке, он должен останавливаться, и проверяя подобную конструкции в других кодах(без движения по кнопкам, покажу дальше) она работала, но здесь почему-то нет.
Код движения и класса:
код

const players = {}

class Player {

    constructor(props)
    {
        this.name = props.name,
        this.id = props.id,
        this.cx = 300,
        this.cy = 300,
        this.s = 10,
        this.radius = 30,
        this.angle = 0
    }
    updateRotation(mouseX, mouseY) {
        const dx = mouseX - this.cx;
        const dy = mouseY - this.cy;
        this.angle = Math.atan2(dy, dx);
      };
}

        
export function getPlayer(socket){
socket.on("server", () => 
{
    players[socket.id] = new Player(
        {
            id: socket.id,
            name: Object.keys(players).length
        }
        
    );

});
socket.on("movement", (data) => 
{
const player = players[socket.id];

if(player)
{

    player.updateRotation(data.mouseX, data.mouseY);
}

if (data.up)
{
    player.cy -= player.s
};
if (data.down) 
{
    player.cy += player.s
};
if (data.left)
{
    player.cx -= player.s
};
if (data.right)
{
    player.cx += player.s 
};

})
socket.on("disconnect", () => 
{
    delete players[socket.id];
    setTimeout(() => {
    console.log(players);
    }, 1000);
});
return players;

}

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

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

Войти через центр авторизации
Похожие вопросы