Sign in to follow this  
jlpt

Need improvements for my collision detection

Recommended Posts

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
   }
}

}
        

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.