Skip to main content
removed blacklisted tag, moved images to i.stack.imgur.com
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

CollisonCollison

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.

NotWorking NotWorkingNot 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.

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.

NotWorking NotWorking

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.

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.

Source Link
John
  • 31
  • 3

Rectangle Collision Not Correct

Collison http://i.gyazo.com/cc8d7d37e2daddfe6263d91d8e7fc86b.gif

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.

NotWorking http://i.gyazo.com/32a92c09a3a28a471906f1978a3d5c86.gif NotWorking http://i.gyazo.com/cbc44a365a14ebe6540f515e609ad128.png

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.