/// <summary> /// Checks if this object is colliding with the collider of another object. /// </summary> /// <param name="other">Other collider object.</param> /// <returns>True if colliding, false otherwise.</returns> public bool IsColliding(ColliderObject other) { if (!other.Collidable || !Collidable || other is LeafServer) { return(false); } // Get this position and the position of the other object, and make 2D. Vector3 colliderPos = Transform.Position; Vector3 otherPos = other.Transform.Position; colliderPos.Y = 0.0f; otherPos.Y = 0.0f; if (other.colliderType == ColliderType.CIRCLE) { // Get distance between objects. float distance = Vector3.Distance(colliderPos, otherPos); // Check if the objects are overlapping. if (other.Radius > 0.0f && distance <= Radius + other.Radius) { // If overlapping, they're colliding. Return true. return(true); } } else if (other.colliderType == ColliderType.BOX) { float down = other.Transform.Position.Z - other.Radius; float up = other.Transform.Position.Z + other.Radius; float left = other.Transform.Position.X - other.Radius; float right = other.Transform.Position.X + other.Radius; if (Transform.Position.X > left && Transform.Position.X < right && Transform.Position.Z < up && Transform.Position.Z > down) { return(true); } } // If not overlapping, return false. return(false); }
/// <summary> /// Bounce off of another object. /// </summary> /// <param name="other">The collider of the other object </param> public void Bounce(ColliderObject other) { Velocity = (-Velocity * Bounciness); }