Need help getting collisions with walls working

Recommended Posts

Hello!

I'm sorry if this question has been asked here before. Unfortunately I did not find anything relevant on this subject (or at least could not apply it to my case) and so I'm here making a post about it.

Namely I'm making a small top-down game and I want to be able to place down walls in the scene. I'm currently approaching the issue of collision from a Game Maker Studio perspective and moving my objects until a collision is found. Unfortunately my current approach doesn't seem to be working and I'm not even convinced it is within the capabilities of Phaser 3. Currently my moving gameobject with a circular collision box gets stuck in the sides of the wall. Here is the code I have right now for handling movement with wall collisions. I'm using arcade physics. It is run inside my moving gameobjects' update function:

``````    public move(vector: Vector2): void {
let length = vector.length();
vector = vector.normalize().scale(Math.min(this.maxMovement, length));
let total = vector.x;
while (total != 0) {
let amount = Math.min(Math.abs(total), 1) * Math.sign(total);
this.x += amount;
let collision = this.scene.physics.overlap(this, this.castScene.wallGroup);
if (collision) {
this.x -= amount;
total = 0;
this.movement.x = 0;
break;
}
total -= amount;
}
total = vector.y;
while (total != 0) {
let amount = Math.min(Math.abs(total), 1) * Math.sign(total);
this.y += amount;
let collision = this.scene.physics.overlap(this, this.castScene.wallGroup);
if (collision) {
this.y -= amount;
total = 0;
this.movement.y = 0;
break;
}
total -= amount;
}
}``````

My current assumption is that scene.physics.overlap does return the proper value every time I update the x or y position of the object. If this is the case, what could I do to work around this? What other approach could I try out? Is there a way I can handle collision such that I can still define how many units per step the object takes?

Thank you in advance!