0
\$\begingroup\$

I've come across a problem with the collision detection code in the platformer starter kit for xna.It will send up the impassible flag on the x axis despite being nowhere near a wall in either direction on the x axis, could someone could tell me why this happens ?

Here is the collision method.

  /// <summary>
    /// Detects and resolves all collisions between the player and his neighboring
    /// tiles. When a collision is detected, the player is pushed away along one
    /// axis to prevent overlapping. There is some special logic for the Y axis to
    /// handle platforms which behave differently depending on direction of movement.
    /// </summary>
    private void HandleCollisions()
    {
        // Get the player's bounding rectangle and find neighboring tiles.
        Rectangle bounds = BoundingRectangle;
        int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
        int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
        int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
        int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

        // Reset flag to search for ground collision.
        isOnGround = false;

        // For each potentially colliding tile,
        for (int y = topTile; y <= bottomTile; ++y)
        {
            for (int x = leftTile; x <= rightTile; ++x)
            {
                // If this tile is collidable,
                TileCollision collision = Level.GetCollision(x, y);
                if (collision != TileCollision.Passable)
                {
                    // Determine collision depth (with direction) and magnitude.
                    Rectangle tileBounds = Level.GetBounds(x, y);
                    Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
                    if (depth != Vector2.Zero)
                    {
                        float absDepthX = Math.Abs(depth.X);
                        float absDepthY = Math.Abs(depth.Y);

                        // Resolve the collision along the shallow axis.
                        if (absDepthY < absDepthX || collision == TileCollision.Platform)
                        {
                            // If we crossed the top of a tile, we are on the ground.
                            if (previousBottom <= tileBounds.Top)
                                isOnGround = true;

                            // Ignore platforms, unless we are on the ground.
                            if (collision == TileCollision.Impassable || IsOnGround)
                            {
                                // Resolve the collision along the Y axis.
                                Position = new Vector2(Position.X, Position.Y + depth.Y);

                                // Perform further collisions with the new bounds.
                                bounds = BoundingRectangle;
                            }
                        }
                       //This is the section which deals with collision on the x-axis
                        else if (collision == TileCollision.Impassable) // Ignore platforms.
                        {
                            // Resolve the collision along the X axis.
                            Position = new Vector2(Position.X + depth.X, Position.Y);

                            // Perform further collisions with the new bounds.
                            bounds = BoundingRectangle;
                        }
                    }
                }
            }
        }

        // Save the new bounds bottom.
        previousBottom = bounds.Bottom;
    }
\$\endgroup\$
4
  • \$\begingroup\$ Post code, because I doubt people here will have looked through that kit. \$\endgroup\$ Commented Mar 12, 2011 at 11:39
  • \$\begingroup\$ I posted the method which deals with collision \$\endgroup\$
    – dbomb101
    Commented Mar 12, 2011 at 11:46
  • \$\begingroup\$ From that code it looks like the Level.GetCollision() function is giving you the impassable/passable flag. Might help if you posted that here. \$\endgroup\$
    – Ray Dey
    Commented Mar 12, 2011 at 20:26
  • \$\begingroup\$ its not so much the flag thats causing the issue, but when two items intersect with each other which is causing the problem I've only part way fixed it at the moment \$\endgroup\$
    – dbomb101
    Commented Mar 13, 2011 at 21:20

1 Answer 1

0
\$\begingroup\$

I have changed

 if (absDepthY < absDepthX || collision == TileCollision.Platform)

to

 if (absDepthY <= absDepthX || collision == TileCollision.Platform)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .