0
votes

I'm currently working on a platformer project based in C# for XNA.

My problem occurs when the character walks off of a platform. He should fall, but won't. I'm currently using a series of bools; jumpBool, fallBool, groundBool and a static bool that determines whether or not he's on the platform using a rectangle helper.

The code that determines what state of jump he's in is as follows.

    if (onGround == true)
        {
            fallBool = false;
            gravity = 0.0f;
            jumpheight = -14.0f;
        }
        else
        {
            fallBool = true;
        }

        if (jumpBool == true)
        {
            gravity = 0.0f;
            jumpheight += 1.0f;
            Position.Y += jumpheight;
            onGround = false;
        }

        if (jumpheight > 0)
        {
            jumpBool = false;
            fallBool = true;
        }

        if (fallBool == true)
        {
            gravity = 1.0f;
        }

This all works until I tried to add collision testing when he touched a platform.

The following determines the bounding boxes to check whether the player is above a platform.

static class RectangleHelper
{
    public static bool TouchTopOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Bottom >= r2.Top - 1 &&
                r1.Bottom <= r2.Top + (r2.Height / 2) &&
                r1.Right >= r2.Left + r2.Width / 5 &&
                r1.Left <= r2.Right - r2.Width / 5);
    }

    public static bool TouchBottomOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Top <= r2.Bottom + (r2.Height / 5) &&
                r1.Top >= r2.Bottom - 1 &&
                r1.Right >= r2.Left + (r2.Width / 5) &&
                r1.Left <= r2.Right - (r2.Width / 5));
    }

    public static bool TouchLeftOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Right <= r2.Right &&
                r1.Right >= r2.Right - 5 &&
                r1.Top <= r2.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));
    }

    public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Left <= r2.Left &&
                r1.Left >= r2.Left - 5 &&
                r1.Top <= r2.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));
    }
}

And finally, this code checks if he is standing on a block, and changes the jumping properties when he touches a platform.

 if (rectangle.TouchTopOf(newRectangle))
        {
            jumpheight = -14.0f;
            onGround = true;
            gravity = 0.0f;
            fallBool = false;
        }

However, because I'm using a bool to determine whether or not he is on the ground, when he walks off a platform, the bool is still set to true and he doesn't fall. I've thought of trying something like

 if else(!rectangle.TouchTopOf(newRectangle))
 {
      onGround = false;
 }

but that causes onGround to always be false for some reason, and he just falls through the platform. How can I possibly cause him to fall once he walks off a platform?

Thank you for looking at this post and making it all the way down here. I really appreciate it.

1
Why don'y you make a check for player standing on ground then? I guess it would be checking for collisions of the bottom parts of player with any clipping tile. You can of course look for what others implement as their basic collision detection and resolution.user1306322

1 Answers

0
votes

Looks like you're only setting 'onGround' when the player is on the ground (and not updating it after the player steps off), since your function returns a bool why don't you just try

onGround = rectangle.TouchTopOf(newRectangle);