/*
* Collidable.cs
* Authors: Adam Nabinger
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using Microsoft.Xna.Framework;
namespace MaritimeDefender
{
///
/// Defines a class that performs checks of collision based on position and size
///
public class Collidable
{
#region Fields
///
/// Center of the Entity
///
protected Vector3 mPos;
///
/// Volume of the Entity
///
protected Vector3 mSize;
#endregion
#region Properties
///
/// Gets/Sets the position of the entity
///
public Vector3 Position {
get { return mPos; }
set { mPos = value; }
}
///
/// Gets/Sets the horizontal position
///
public float X {
get { return mPos.X; }
set { mPos.X = value; }
}
///
/// Gets/Sets the verticle position
///
public float Y {
get { return mPos.Y; }
set { mPos.Y = value; }
}
///
/// Gets/Sets the layer depth
///
public float Z {
get { return mPos.Z; }
set { mPos.Z = value; }
}
///
/// Gets/Sets the size of the entity
///
public virtual Vector3 Size {
get { return mSize; }
set { mSize = value; }
}
///
/// Gets/Sets the X-dimension of size
///
public virtual float Width {
get { return mSize.X; }
set { mSize.X = value; }
}
///
/// Gets/Sets the Y-dimension of size
///
public virtual float Height {
get { return mSize.Y; }
set { mSize.Y = value; }
}
///
/// Gets/Sets the Z-dimension of size
///
public virtual float Depth {
get { return mSize.Z; }
set { mSize.Z = value; }
}
#endregion
#region Collision
///
/// Tests collision between 2 entities' bounding boxes in x and y dimensions
///
/// The first object to check for collision against
/// The second object to check for collision against
/// True if collision occurs, false otherwise
internal static bool Collides2D(Collidable entity1, Collidable entity2)
{
return (entity1.X + (entity1.Width * 0.5f) >= entity2.X - (entity2.Width * 0.5f) &&
entity1.X - (entity1.Width * 0.5f) <= entity2.X + (entity2.Width * 0.5f) &&
entity1.Y + (entity1.Height * 0.5f) >= entity2.Y - (entity2.Height * 0.5f) &&
entity1.Y - (entity1.Height * 0.5f) <= entity2.Y + (entity2.Height * 0.5f));
}
///
/// Tests collision between 2 entities' bounding boxes in 3 dimensions
///
/// The first object to check for collision against
/// The second object to check for collision against
/// True if collision occurs, false otherwise
internal static bool Collides3D(Collidable entity1, Collidable entity2)
{
return (entity1.X + (entity1.Width * 0.5f) >= entity2.X - (entity2.Width * 0.5f) &&
entity1.X - (entity1.Width * 0.5f) <= entity2.X + (entity2.Width * 0.5f) &&
entity1.Y + (entity1.Height * 0.5f) >= entity2.Y - (entity2.Height * 0.5f) &&
entity1.Y - (entity1.Height * 0.5f) <= entity2.Y + (entity2.Height * 0.5f) &&
entity1.Z + (entity1.Depth * 0.5f) >= entity2.Z - (entity2.Depth * 0.5f) &&
entity1.Z - (entity1.Depth * 0.5f) <= entity2.Z + (entity2.Depth * 0.5f));
}
#endregion
}
}