Sign in to follow this  

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

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.