Can anyone tell me why my collision detection does not work?

in my enemy class I create my rectangle like this.

enemyPos = new Vector2(spritePosX, spritetPosY);
 Rectangle enemyRect = new Rectangle((int)enemyPos.X - enemySprite.Width / 2,
     (int)enemyPos.Y - enemySprite.Height / 2, enemySprite.Width, enemySprite.Height);

they are updated like this.

enemyPos += heading * currentEnemySpeed;
 enemyRect.X = (int)enemyPos.X - enemySprite.Width / 2;
 enemyRect.Y = (int)enemyPos.Y - enemySprite.Height / 2;

and drawn

spriteBatch.Draw(enemySprite, enemyPos, null,
    Color.FromNonPremultiplied(255, spriteVarColor, 255, opacityVal),
    enemyOrientation, enemyRotOrigin, scale, SpriteEffects.None, 0f);

in the projectile class

spritePos = new Vector2(ProjectilePosX, ProjectilePosY);
 Rectangle projectileRect = new Rectangle((int)spritePos.X -
     projectileSprite.Width / 2, (int)spritePos.Y - projectileSprite.Height / 2,
     projectileSprite.Width, projectileSprite.Height);

and updated

spritePos = new Vector2(ProjectilePosX, ProjectilePosY);
projectileRect.X = (int)spritePos.X - projectileSprite.Width / 2;
projectileRect.Y = (int)spritePos.Y - projectileSprite.Height / 2;

ans drawn

spriteBatch.Draw(projectileSprite, spritePos, null,
    Color.FromNonPremultiplied(255, 255, 255, 255), spriteHeading,
    spriteRotOrigin, 1.0f, SpriteEffects.None, 0f);

then in the main game class i do this

foreach (Projectile projectile in projectiles)
{
    foreach (Enemy enemy in enemies)
    {
 
        if (projectile.ColRect.Intersects(enemy.ColRect))
        {
 
            projectile.Active = false;
            Exit();
            Constants.score += 1;

There are no collision detections. Can anyone tell me why? Is it because I have the rectangles as null in the draw method? Please help. :confused:

no idea, but to find the bug:

  1. I would render the collision rectangles so that I have a visual idea of what’s happenning.
  2. I’d put a breakpoint at if (projectile.ColRect.Intersects(enemy.ColRect)) and check what the values are there, and why they’re not passing the ‘if’

Are you changing scale when drawing the enemy? If it’s not 1.0 then you’ll need to scale enemyRect too for the math to match the visuals.

First thing: It is inefficient, but enough for small quantities of projectiles*enemies. Basically it should work the way you describe it.

Two things came into mind here (longshot, since code is incomplete) in addition to the already stated things:

  1. You’re telling us about projectileRect and enemyRect, yet you’re testing for projectile.ColRect and enemy.ColRect)…
  2. If you’re rotating sprites (I just infer that since you’re having a rotOrigin and the position is in the middle of the rectangles), then you’d have to create new axis-aligned-bounding-boxes (AABBs) since the rotation wouldn’t be taken into account (you’re just drawing the rotation)… but it should give you some collision in this case, just not ‘exact’…

I abandoned the rectangle collision approach and went to collision circle and everything worked like a charm. Thanks for the comments guys. :slight_smile:

Nice to hear you were able to solve it.
If you’re up for it you could take a step to the next level by installing some broad-phase collision detection to resolve collision-candidates before doing the narrow-phase collision detection.
After all n*m scales very badly :slight_smile:

Here is some stuff with sample code and nuget package ready to roll…

have fun

Thanks Bro! You’re a Pro! :ok_hand:

Lol…
No.
But virtually everyone else around here sure is.
:slight_smile: