0
\$\begingroup\$

So my player checks if it is colliding with tiles, and it works pretty good. However, when the player is against a wall, moving upwards and sideways, it detects it as hitting the bottom of the wall since the player checks for up and down collision first. How do I prevent this? Maybe there is another collision method I could use? Thanks in advance

The player is moving upwards and sideways, hitting the bottom

Here's the collision code:

player.checkCollision = function (obj) {

            const top = obj.y - this.h;
            const bottom = obj.y + obj.h;

            const left = obj.x - this.w;
            const right = obj.x + obj.w;

            if (this.x > left && this.x < right) {

                if (this.y >= top && this.y - this.vel.y <= top) {

                    this.y = top;
                    this.vel.y = 0;

                    return 1;

                }
                if (this.y <= bottom && this.y - this.vel.y >= bottom) {

                    this.y = bottom;
                    this.vel.y = 0;

                    return 1;

                }   

            }
            if (this.y > top && this.y < bottom) {

                if (this.x >= left && this.x - this.vel.x <= left) {

                    this.x = left;
                    this.vel.x = 0;

                    return 1;

                }
                if (this.x <= right && this.x - this.vel.x >= right) {

                    this.x = right;
                    this.vel.x = 0;

                    return 1;

                }   

            }

            return 0;

        }
\$\endgroup\$
0
\$\begingroup\$

So is the problem that when the player is moving in both directions at the same time a collision is detected and the player stops moving?

You can either redo the whole collision detection function which probably isn't a bad idea or simply try moving in each direction separately and see if that works.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.