0
\$\begingroup\$

Collison

As you can see on the top corner it says COL: False/True. This is if the player bounds and the tiles which are solids. The rectangles for the tiles and players are checked if they intercept each-other. Looks like its working right? Well nope. Look more closely.

Not Working Not working

The BOTTOM RIGHT corner needs to be inside the tiles for it to count.

Now let's get to the code I used now that hopefully you understand the problem.

Player Bounds (Rectangle)

playerBounds.Width = 32;
playerBounds.Height = 64;
playerBounds.X = (int)this.position.X;
playerBounds.Y = (int)this.position.Y;

Tile Bounds (Rectangle)

newTile.bounds = new Rectangle(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);

Now onto how it detects it:

for (int x = 0; x < Tilemap.MAP_WIDTH; x++)
        {
            for (int y = 0; y < Tilemap.MAP_HEIGHT; y++)
            {

                if (tm.tile[x, y].bounds.Intersects(playerBounds))
                {
                    if (tm.tile[x, y].getSolid())
                    {
                        Colliding = true;
                    } else
                    {
                        Colliding = false;
                    }
                }
            }

        }

Move

    public void Move(Vector2 pos)
    {
        for (int i = 0; i < speed; i++)
        {

            position += pos;
        }

    }

I have used breakpoints at the collision detection loop. And the rectangle fully covers the character and the tiles.

\$\endgroup\$
  • 1
    \$\begingroup\$ You need colliding TRUE if any intersections occur, not just the last one checked. Put colliding=false outside your loop. Then do colliding = colliding || true; and remove colliding = false. You could also break out of the loop on the first collision detected, no need to see the rest! \$\endgroup\$ – david van brink Jul 5 '15 at 18:26
  • \$\begingroup\$ Also this is in a update method. \$\endgroup\$ – John Jul 5 '15 at 19:09
  • \$\begingroup\$ Wait you might be right the break; fixed the top right corner. \$\endgroup\$ – John Jul 5 '15 at 19:19
  • \$\begingroup\$ @davidvanbrink Can you re-write my Colliding Function with said changes? \$\endgroup\$ – John Jul 5 '15 at 19:23
  • \$\begingroup\$ FIXED IT! Ill post answer \$\endgroup\$ – John Jul 5 '15 at 19:34
1
\$\begingroup\$
        for (int x = 0; x < Tilemap.MAP_WIDTH; x++)
        {
            for (int y = 0; y < Tilemap.MAP_HEIGHT; y++)
            {

                if (tm.tile[x, y].bounds.Intersects(playerBounds))
                {
                    if (tm.tile[x, y].getSolid())
                    {
                        foundCols++;
                    } else
                    {

                    }
                }
            }

        }
        if (foundCols < 1)
        {
            Colliding = false;
            foundCols = 0;
        }
        else
        {
            Colliding = true;
            foundCols = 0;
        }

Answer by: David van brink.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.