private void CheckCollision(CircleCollider collider) { var hit = collider.Collide(X, Y, (int)Tags.PLAYERATTACK); if (hit != null) { bool isHurt = true; // Make sure it didn't go through the front side if (Hitbox.Collide(X, Y, hit.Entity) != null) { var hitboxPos = new Vector2(Hitbox.X, Hitbox.Y); var colliderPos = new Vector2(collider.X, collider.Y); var entityPos = new Vector2(hit.X, hit.Y); var toHitbox = entityPos - hitboxPos; var angleToHitbox = Math.Atan2(toHitbox.Y, toHitbox.X); var hypt = (float)Math.Sin(angleToHitbox) * toHitbox.Y; var hitboxPenetration = hypt - hit.HalfWidth; var colliderPenetration = (collider.Radius + hit.HalfWidth) - (colliderPos - entityPos).Length; isHurt = (colliderPenetration > hitboxPenetration); } // Apply damage if okay if (isHurt) { var e = hit.Entity as Projectile; if (e.damage > 1) { ApplyDamage(e.damage); if (health > 0) { Game.Coroutine.Start(SwimAttack()); } } e.HitEnemy(); } } }