@ne4t0

Как отследить соприкосновение игрока с объектом?

Как сделать так, чтобы игрок при соприкосновении с объектом останавливался, как слева, так и справа.
А при соприкосновении сверху, игрок бы стоял на объекте, не проваливаясь в него?
668bf0d9d2e84745907877.png
class Player {
    constructor() {
        this.velocity = {
            x: 0,
            y: 1,
        },
        this.x = 272;
        this.y = 300;
        this.w = 55;
        this.h = 55;
    }
    draw() {
        c.fillStyle = "green";
        c.fillRect(this.x, this.y, this.w, this.h);
    }
    update() {
        this.x += this.velocity.x;
        this.y += this.velocity.y;

        if(this.y + this.h + this.velocity.y <= canvas.height-50) {
            this.velocity.y += grav;
        }
        else this.velocity.y = 0;

        if(this.x + this.w > object.x &&
            object.x + object.w > this.x &&
            this.y + this.h > object.y &&
            object.y + object.h > this.y
        ) {
            this.x = object.x - this.w;
            this.y = object.y - this.h;
        } 
    
    }
}

class Object {
    constructor() {
        this.x = 400;
        this.y = 350;
        this.w = 100;
        this.h = 100;
    }
    draw() {
        c.fillStyle = "rgb(11, 200, 200)";
        c.fillRect(this.x, this.y, this.w, this.h);
    }
}
  • Вопрос задан
  • 86 просмотров
Пригласить эксперта
Ответы на вопрос 1
ThunderCat
@ThunderCat Куратор тега JavaScript
{PHP, MySql, HTML, JS, CSS} developer
Ваш ответ на вопрос

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

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