From 2750bc0277c3d929603daceee2e8a1e88368a306 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Fri, 26 Aug 2011 20:29:25 +0200 Subject: import from local directory --- src/main/scala/sims/collision/AABB.scala | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/scala/sims/collision/AABB.scala (limited to 'src/main/scala/sims/collision/AABB.scala') diff --git a/src/main/scala/sims/collision/AABB.scala b/src/main/scala/sims/collision/AABB.scala new file mode 100644 index 0000000..68782d1 --- /dev/null +++ b/src/main/scala/sims/collision/AABB.scala @@ -0,0 +1,55 @@ +/* _____ _ __ ________ ___ *\ +** / ___/(_) |/ / ___/ |__ \ Simple Mechanics Simulator 2 ** +** \__ \/ / /|_/ /\__ \ __/ / copyright (c) 2011 Jakob Odersky ** +** ___/ / / / / /___/ / / __/ ** +** /____/_/_/ /_//____/ /____/ ** +\* */ + +package sims.collision + +import sims.math._ + +/* + * y + * ^ + * | + * | +-------+ + * | | max| + * | | | + * | |min | + * | +-------+ + * | + * 0-------------->x + * + */ + +/** Axis Aligned Bounding Boxes (AABBs) are rectangles that frame a shape. + * Their X-axis and Y-axis orientation makes it easy to test two AABBs for overlap. + * @param minVertex Position vector of the bottom-left vertex + * @param maxVertex Position vector of the upper-right vertex */ +case class AABB(val minVertex: Vector2D, + val maxVertex: Vector2D) { + + /** Diagonal vector from `minVertex` to `maxVertex`. */ + def diagonal = maxVertex - minVertex + + /** Width of this AABB. */ + def width = maxVertex.x - minVertex.x + + /** Height of this AABB. */ + def height = maxVertex.y - minVertex.y + + /** Checks if the given point is located within this AABB. */ + def contains(point: Vector2D): Boolean = minVertex.x <= point.x && point.x <= maxVertex.x && minVertex.y <= point.y && point.y <= maxVertex.y + + /** Checks if the given AABB is located within this AABB. */ + def contains(box: AABB): Boolean = contains(box.minVertex) && contains(box.maxVertex) + + /** Checks this AABB with box for overlap. + * @param box AABB with which to check for overlap */ + def overlaps(box: AABB): Boolean = { + val d1 = box.minVertex - maxVertex + val d2 = minVertex - box.maxVertex + !(d1.x > 0 || d1.y > 0 || d2.x > 0 || d2.y > 0) + } +} -- cgit v1.2.3