javascript - Stop Player Movement After Collision Detection In Canvas -


i made 2 walls in canvas. 1 in top , 1 @ bottom. player controlled mouse , wanted know how make player not go through walls.

here's function general collision between 2 objects:

function collides(a, b) {    var val = false;     val = (a.x < b.x + b.width) &&    (a.x + a.width > b.x) &&    (a.y < b.y + b.height) &&    (a.y + a.height > b.y);     return val;         } 

here's code detects collision detection:

if (collides(player, block)){     //i don't know goes here. } 

any appreciated.

reposition player , also clamp player's y position between top , bottom walls.

in mousemove handler (or wherever player repositioned mouse):

// reposition player ...  // , clamp player stay below top wall if( player.y < wall.y+wall.height ){ player.y = wall.y+wall.height);  // , clamp player stay above bottom wall if( player.y+player.height > wall.y ){ player.y = wall.y-player.height); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -