Jump to content
  • Advertisement
Sign in to follow this  
Robo-Link

Colision Detection?

This topic is 3989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i am doing a game in C# .net and i need some help with my colision detection. can ya tell me whats wrong? the bottom and side one doesn't work right. the side one stops my guy when he runs over it, and also sometimes still lets him go through. and the bottom one doesn't work eather.
public void colDectionTop(player p1, worldMap wm)
        {
            if (this.getTopColBox() >= p1.getBottomColBox() &&
                this.getTopColBox() - 5 <= p1.getBottomColBox()&&
                this.getLeftColBox() <= p1.getLeftColBox() &&
                this.getRightColBox() >= p1.getLeftColBox() ||

                this.getTopColBox() >= p1.getBottomColBox() &&
                this.getTopColBox() - 5 <= p1.getBottomColBox() &&
                this.getLeftColBox() <= p1.getRightColBox() &&
                this.getRightColBox() >= p1.getRightColBox() 
              )
           {
               if (!p1.jumping)
               {
                   p1.setY(this.getY() - 50);
                   p1.landing = false;
               }
           }
        }

        public void colDectionBottom(player p1, worldMap wm)
        {//doesnt work right
            if (this.getBottomColBox() <= p1.getTopColBox() &&
                this.getBottomColBox() + 20 >= p1.getTopColBox() &&
                this.getLeftColBox() <= p1.getLeftColBox() &&
                this.getRightColBox() >= p1.getLeftColBox() ||

                this.getBottomColBox() <= p1.getTopColBox() &&
                this.getBottomColBox() + 20 >= p1.getTopColBox() &&
                this.getLeftColBox() <= p1.getRightColBox() &&
                this.getRightColBox() >= p1.getRightColBox()
              )
            {
                if (p1.jumping)
                {
                   // p1.setY(p1.getY() + 20);
                    p1.jumping = false;
                    p1.landing = true;
                }
            }
        }

        public void colDectionLeft(player p1, worldMap wm)
        {//doesnt work right
            if (this.getLeftColBox() <= p1.getRightColBox() &&
                this.getLeftColBox() + 20 >= p1.getRightColBox() &&
                this.getTopColBox() <= p1.getBottomColBox() &&
                this.getBottomColBox() >= p1.getBottomColBox() ||

                this.getLeftColBox() <= p1.getRightColBox() &&
                this.getLeftColBox() + 20 >= p1.getRightColBox() &&
                this.getTopColBox() <= p1.getTopColBox() &&
                this.getBottomColBox() >= p1.getTopColBox()
              )
            {
                    p1.setX(p1.getX() - 10);
                    wm.setX(wm.getX() - 10);
                    p1.MOVE_RIGHT = false;
            }
        }

Share this post


Link to post
Share on other sites
Advertisement
In your collision check, it appears that you're attempting to separate two blocks of four lines of collision detection for each if statement. The logical OR (||) that you're using, however, is only effective on the two statements immediately before and after the OR, and not on the entire "block." You need to group the blocks together in order to effectively OR the entire set of checks.

Here's what I mean:

if( ( this.getTopColBox() >= p1.getBottomColBox() &&
this.getTopColBox() - 5 <= p1.getBottomColBox()&&
this.getLeftColBox() <= p1.getLeftColBox() &&
this.getRightColBox() >= p1.getLeftColBox() ) ||

( this.getTopColBox() >= p1.getBottomColBox() &&
this.getTopColBox() - 5 <= p1.getBottomColBox() &&
this.getLeftColBox() <= p1.getRightColBox() &&
this.getRightColBox() >= p1.getRightColBox() )
)

Notice the additional parentheses around the top and the bottom block, and how the logical OR now separates both chunks.

I think that little fix will help you out some. However, your checking itself is a bit convoluted and should be simplified in order to reduce your possible pool of errors. First fix the OR problem, and then look at how you could reduce your checks--some of them are redundant.

Share this post


Link to post
Share on other sites
thanks, but i seem to be having the same problem.
here is the new bottom and side col methods


public void colDectionBottom(player p1, worldMap wm)
{//doesnt work right
if ((this.getBottomColBox() <= p1.getTopColBox() &&
this.getBottomColBox() + 20 >= p1.getTopColBox() &&
this.getLeftColBox() <= p1.getLeftColBox() &&
this.getRightColBox() >= p1.getLeftColBox()) ||

(this.getBottomColBox() <= p1.getTopColBox() &&
this.getBottomColBox() + 20 >= p1.getTopColBox() &&
this.getLeftColBox() <= p1.getRightColBox() &&
this.getRightColBox() >= p1.getRightColBox())
)
{
if (p1.jumping)
{
// p1.setY(p1.getY() + 20);
p1.jumping = false;
p1.landing = true;
}
}
}

public void colDectionLeft(player p1, worldMap wm)
{//doesnt work right
if ((this.getLeftColBox() <= p1.getRightColBox() &&
this.getLeftColBox() + 20 >= p1.getRightColBox() &&
this.getTopColBox() <= p1.getBottomColBox() &&
this.getBottomColBox() >= p1.getBottomColBox()) ||

(this.getLeftColBox() <= p1.getRightColBox() &&
this.getLeftColBox() + 20 >= p1.getRightColBox() &&
this.getTopColBox() <= p1.getTopColBox() &&
this.getBottomColBox() >= p1.getTopColBox())
)
{
p1.setX(p1.getX() - 10);
wm.setX(wm.getX() - 10);
p1.MOVE_RIGHT = false;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!