public static Collide ( |
||
rectHitboxA | ||
rectHitboxB | ||
return | Vector2 |
private void handleEntitiesCollisions() { // Find collisions between entities foreach (Entity worldEnt in WorldEntities) { foreach (PhysicsEntity regEnt in _registeredEntities) { if (worldEnt == regEnt) { continue; } // Look for collisions if ((regEnt.Hitbox != null) && (worldEnt.Hitbox != null)) { Vector2 collision = Hitbox.Collide(regEnt.Hitbox, worldEnt.Hitbox); // Raise event if (collision != Vector2.Zero) { if (regEnt.IsInvincible == false) { regEnt.CollisionDetected(worldEnt, collision); } } } // Look for collisions if ((regEnt.AwarenessBox != null) && (worldEnt.Hitbox != null) && worldEnt.IsAlive) { Vector2 collision = Hitbox.Collide(regEnt.AwarenessBox, worldEnt.Hitbox); // Raise event if (collision != Vector2.Zero) { regEnt.EntityDetected(worldEnt, collision); } } } } }
private bool handleEntityFloorCollisions(PhysicsEntity regEnt, IEnumerable <Floor> floors) { bool collisionDetected = false; foreach (Floor floor in floors) { Vector2 collision; // Do not cross the floor if (Hitbox.Collide(regEnt.Hitbox, floor.Rectangle, out collision)) { float absDepthX = Math.Abs(collision.X); float absDepthY = Math.Abs(collision.Y); // Resolve the collision along the shallow axis. if (absDepthY <= absDepthX) { bool floorOnTop = floor.Rectangle.Y < regEnt.Hitbox.Dimensions.Y; // If we crossed the top of a tile, we are on the ground. if (floorOnTop == false) { // Hack for passable platforms if (floor.IsPassable == false) { regEnt.IsOnGround = true; } else if (regEnt.Velocity.Y >= 0) { regEnt.IsOnGround = true; } } if (floor.IsPassable == false || regEnt.IsOnGround) { regEnt.Location = new Vector2(regEnt.Location.X, regEnt.Location.Y + collision.Y); if (absDepthY != 0) { regEnt.Velocity = new Vector2(regEnt.Velocity.X, 0f); } } } else { regEnt.Location = new Vector2(regEnt.Location.X + collision.X, regEnt.Location.Y); bool passable = (floor.IsPassable == true); if (!passable) { // If going in the direction of the collision if (floor.Rectangle.X > regEnt.Hitbox.Dimensions.X) { regEnt.IsStuckRight = true; } else if (floor.Rectangle.X < regEnt.Hitbox.Dimensions.X) { regEnt.IsStuckLeft = true; } if (absDepthX != 0) { regEnt.Velocity = new Vector2(0f, regEnt.Velocity.Y); } } } regEnt.Hitbox.UpdateBounds(); if (regEnt.FloorCollisionDetected != null) { regEnt.FloorCollisionDetected(collision); } if (regEnt.IsAlive == false) { break; } collisionDetected = true; } } return(collisionDetected); }