Skip to main content
Source Link
Jelle
  • 331
  • 1
  • 14

Collision detection

So I've made my collision boxes visual (Draw method) and I saw that there were some problems with the boxes and I fixed them (x, y, length, width). They are now big enough.

This is the collision box inside my constructor for my TileSprite class:

public TileSprite(Texture2D collisionImage, Texture2D textureImage, Vector2 position, Point frameSize, Point currentFrame)
    : base (collisionImage, textureImage, position, frameSize, currentFrame)
{
    // Collision box (Rectangle)
    bCollisionRect = new Rectangle((int)position.X, (int)position.Y, frameSize.X * 2, frameSize.Y * 2);
}

And this is the collision box for my PlayerMovement class. This collision box is written in the update method so it updates when the player moves:

collisionRect = new Rectangle((int)position.X + 4, (int)position.Y + 14, frameSize.X + 10, frameSize.Y + 20);

TileSprite and PlayerMovement have an abstract class in common, named Sprite.I have all my virtual methods for the sprites in Sprite and this is my Collision method:

public virtual void Collision (GameTime gameTime)
{
    // collisionRect is from PlayerMovement and bCollisionRect from TileSprite
    if(collisionRect.Intersects(bCollisionRect))
    {
        Console.WriteLine("collision");
    }
}

But my problem is, that there is no console output when the two collision boxes intersect. (Yes, I call the collision method.)