Im testing on a simple game and i was starting to get into collision between the character and any object. I first went about this by getting the characters last position and changing the characters coordinates to it. I was wondering if there would be anyother way of doing it? Right now i have the "detection" part out of the way.
//Class Function
iscolliding(object) {
if (this.x < object.x + object.width &&
this.x + this.width > object.x &&
this.y < object.y + object.height &&
this.height + this.y > object.y) {
return true;
} else {
return false;
}
}
//------------------------------------------------------------------
//Is colliding function (returns bool)
if (character.iscolliding(/*Object*/)) {
console.log('working');
}
Where there is console.log('working')
i was wondering what i would do next? How would i stop the character from going through the object
? I would prefer not to use Jquery, just plain javascript>
Thanks (:
If you need any more info i would gladly answer any comments below! (:
You will want call another function if your current one determines that they are colliding. Then you will set the player's x and/or y value. So if you collide from the player's left side to the object right side you would set player.x
to the object.x + object.w
and set the player's velocity.x to 0.
Here's a function that works. You can see it in action on here where I recently answered another question. Just go down to the answer and run the snippet.
2d Square to rectangle collision detection and action(physics)
The collisionDetection()
below is different from your in that if any value is true then there cannot be a collision and it just return. Otherwise there is one and it calls the narrowPhase()
.
function collisionDetection(obj) {
if (player.x + player.w < obj.x ||
player.x > obj.x + obj.w ||
player.y + player.h < obj.y ||
player.y > obj.y + obj.h) {
return
}
narrowPhase(obj);
}
function narrowPhase(obj) {
let playerTop_ObjBottom = Math.abs(player.y - (obj.y + obj.h));
let playerRight_ObjLeft = Math.abs((player.x + player.w) - obj.x);
let playerLeft_ObjRight = Math.abs(player.x - (obj.x + obj.w));
let playerBottom_ObjTop = Math.abs((player.y + player.h) - obj.y);
if ((player.y <= obj.y + obj.h && player.y + player.h > obj.y + obj.h) && (playerTop_ObjBottom < playerRight_ObjLeft && playerTop_ObjBottom < playerLeft_ObjRight)) {
player.y = obj.y + obj.h;
player.vy = 0;
}
if ((player.y + player.h >= obj.y && player.y < obj.y) && (playerBottom_ObjTop < playerRight_ObjLeft && playerBottom_ObjTop < playerLeft_ObjRight)) {
player.y = obj.y - player.h;
player.jumping = false;
player.vy = 0;
}
if ((player.x + player.w >= obj.x && player.x < obj.x) && (playerRight_ObjLeft < playerTop_ObjBottom && playerRight_ObjLeft < playerBottom_ObjTop)) {
player.x = obj.x - player.w;
player.vx = 0;
}
if ((player.x <= obj.x + obj.w && player.x + player.w > obj.x + obj.w) && (playerLeft_ObjRight < playerTop_ObjBottom && playerLeft_ObjRight < playerBottom_ObjTop)) {
player.x = obj.x + obj.w;
player.vx = 0;
}
}