2

Hi everyone I've recently started to make my own platform game, I've managed to create the map using a 2d array and have also created my player with movement. I'm now stuck on how to approach the rectangle collision side any help is greatly appreciated.

Here is how I have created my map and now I just need to figure out how to approach the collision.

    List<Texture2D> tileTextures = new List<Texture2D>();
    private const int tileWidth = 64;
    private const int tileHeight = 64;


    public void Draw(SpriteBatch spriteBatch, Camera camera)
    {         
        int tileMapWidth = tileMap.GetLength(1);
        int tileMapHeight = tileMap.GetLength(0);

        for (int x = 0; x < tileMapWidth; x++)
        {
            for (int y = 0; y < tileMapHeight; y++)
            {
                int textureIndex = tileMap[y, x];
                Texture2D texture = tileTextures[textureIndex];

                spriteBatch.Draw(
                    texture,
                    new Rectangle(x * tileWidth - (int)camera.cameraPosition.X,
                        y * tileHeight - (int)camera.cameraPosition.Y,
                        tileWidth,
                        tileHeight),
                    Color.White);
            }
        }           
    }
5
  • From what I remember When I was starting with XNA, you had this rectangle class with an intersects method that you could use to determine whether an object was colliding with another Commented May 5, 2016 at 21:56
  • Do you know how I would compare my player rectangle with my tiles though? As i set a new rectangle each time in the tile draw function but I have no idea how I would actually call it to be checked if it was intersecting. Commented May 5, 2016 at 22:06
  • if you're doing XNA, you should probably have an update, method, and you can just iterate over all your players, and tiles, and make a new rectangle, and check for collisions there Commented May 5, 2016 at 22:30
  • So I've created a function for my player as follows: public Rectangle BoundingBox { get { return new Rectangle( (int)position.X, (int)position.Y, spriteWidth, spriteHeight); } } but I'm still unsure how to retrieve the tile rectangle from the code I posted above so I can use the intersects function, do you have any suggestions? Commented May 5, 2016 at 23:41
  • 2
    Honestly it's not that hard Commented May 6, 2016 at 1:43

3 Answers 3

1

You can use the Rectangle of your object and use it's Intersect method to check wheter they have collided or not.

You can find a really simple example of how to check rectangle collisions on my blog, where the user clicks over an enemy to destroy it:

if (mouseState.LeftButton == ButtonState.Pressed)
{
   for (int index = this.enemies.Count - 1; index >= 0; index--)
   {
       if (this.enemies[index].Intersects(
              new Rectangle((int)this.mouseCoordinates.X, 
                            (int)this.mouseCoordinates.Y,     
                            this.mouseTexture.Width, 
                            this.mouseTexture.Height)))
       {
            this.enemies.RemoveAt(index);
       }
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

What @Nahuel Ianni said is great for detecting collision. Now, you didn't specify what you need collision for, and I can see that you will most likely need it for destroying enemies and bullets, which @Nahuel Ianni explained wery well, but I think you'll also need collision between player(s) and walls.

Shaun Spalding made a great point in this tutorial (for GameMaker) on how to detect and resolve collision in a platformer, which can also used in birds-eye-view game. In short, in his tutorial he explains you how you should get the nextRectangle* of your player and check if its colliding any walls, and if it is, just move the player right next to that wall. This way you won't see player(s) inside a wall for a frame / player(s) won't be stuck in walls.

*nextRectangle stands for the Rectangle (body) of your player, moved by the speed/force (whatever logic works for you). That's the body in the next frame. So you can say that you'll need to look into the future of your gameflow to get the best result for collision (and avoid players inside walls).

Comments

0

Each Object that you want to check for collisions should have a Rectangle in it. That way, you can do

bool isHit=obj1.Rect.Intersects(obj2.Rect);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.