jlpt Posted January 3, 2018 Share Posted January 3, 2018 I have a function which detects collision but when the player is moving too fast or if it is too small or both, the player clips through the wall. If i can have help on how to improve/change/redo the code to make it work at higher speeds and when the player is smaller function checkPos(obj1,obj2){ if (obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && //COLLISION FROM BOTTOM obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y) { if (obj1.x > obj2.x+obj2.width-(player.speed*2) && obj1.y + obj1.height > obj2.y+player.speed*2 && obj1.y+player.speed*2 < obj2.y + obj2.height) { // hit a block on the right obj1.x = obj1.x + player.speed } if (obj1.x+obj1.width-(player.speed+4) < obj2.x) { // hit a block on the left obj1.x = obj1.x - player.speed } if (obj1.y > obj2.y+obj2.height-(player.speed*2) && obj1.x + obj1.width > obj2.x + player.speed*2 && obj1.x+player.speed*2 < obj2.x + obj2.width){ // hit a block on the bottom obj1.y = obj1.y + player.speed } if (obj1.y+obj1.height-(player.speed*2) < obj2.y){ // hit a block on the top obj1.y = obj1.y - player.speed } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.