TransWikia.com

Player hitting bottom of tile when beside it?

Game Development Asked by mushrombrother.jpg on November 2, 2021

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;

        }

One Answer

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.

Answered by dabski on November 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP