From 28faaa2ccc8d293fde083711bf1affb518e2fad3 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 5 Sep 2013 17:19:31 +1000 Subject: [PATCH] Add collision types --- src/cgmath/aabb.rs | 31 +++++++++++++++++++++++++++++ src/cgmath/cylinder.rs | 26 ++++++++++++++++++++++++ src/cgmath/frustum.rs | 41 ++++++++++++++++++++++++++++++++++++++ src/cgmath/lib.rs | 8 ++++++++ src/cgmath/obb.rs | 33 ++++++++++++++++++++++++++++++ src/cgmath/plane.rs | 43 ++++++++++++++++++++++++++++++++++++++++ src/cgmath/projection.rs | 29 ++++++++++++++++++++------- src/cgmath/sphere.rs | 24 ++++++++++++++++++++++ 8 files changed, 228 insertions(+), 7 deletions(-) create mode 100644 src/cgmath/aabb.rs create mode 100644 src/cgmath/cylinder.rs create mode 100644 src/cgmath/frustum.rs create mode 100644 src/cgmath/obb.rs create mode 100644 src/cgmath/plane.rs create mode 100644 src/cgmath/sphere.rs diff --git a/src/cgmath/aabb.rs b/src/cgmath/aabb.rs new file mode 100644 index 0000000..a389484 --- /dev/null +++ b/src/cgmath/aabb.rs @@ -0,0 +1,31 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Axis-aligned bounding boxes + +use point::{Point2, Point3}; +use vector::{Vec2, Vec3}; + +#[deriving(Clone, Eq)] +pub struct AABB2 { + center: Point2, + size: Vec2, +} + +#[deriving(Clone, Eq)] +pub struct AABB3 { + center: Point3, + size: Vec3, +} diff --git a/src/cgmath/cylinder.rs b/src/cgmath/cylinder.rs new file mode 100644 index 0000000..17bf384 --- /dev/null +++ b/src/cgmath/cylinder.rs @@ -0,0 +1,26 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Oriented bounding cylinder + +use point::Point3; +use vector::Vec3; + +#[deriving(Clone, Eq)] +pub struct Cylinder { + center: Point3, + axis: Vec3, + radius: S, +} diff --git a/src/cgmath/frustum.rs b/src/cgmath/frustum.rs new file mode 100644 index 0000000..bb7c89e --- /dev/null +++ b/src/cgmath/frustum.rs @@ -0,0 +1,41 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! View frustum for visibility determination + +use plane::Plane3; +use point::Point3; + +#[deriving(Clone, Eq)] +pub struct Frustum { + left: Plane3, + right: Plane3, + bottom: Plane3, + top: Plane3, + near: Plane3, + far: Plane3, +} + +#[deriving(Clone, Eq)] +pub struct FrustumPoints { + near_top_left: Point3, + near_top_right: Point3, + near_bottom_left: Point3, + near_bottom_right: Point3, + far_top_left: Point3, + far_top_right: Point3, + far_bottom_left: Point3, + far_bottom_right: Point3, +} diff --git a/src/cgmath/lib.rs b/src/cgmath/lib.rs index d876b0f..9533599 100644 --- a/src/cgmath/lib.rs +++ b/src/cgmath/lib.rs @@ -28,11 +28,19 @@ pub mod quaternion; pub mod vector; pub mod angle; +pub mod plane; pub mod point; pub mod ray; +pub mod rotation; +pub mod frustum; pub mod projection; +pub mod aabb; +pub mod cylinder; +pub mod obb; +pub mod sphere; + pub mod util { use std::num::one; diff --git a/src/cgmath/obb.rs b/src/cgmath/obb.rs new file mode 100644 index 0000000..7c5e6e8 --- /dev/null +++ b/src/cgmath/obb.rs @@ -0,0 +1,33 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Oriented bounding boxes + +use point::{Point2, Point3}; +use vector::{Vec2, Vec3}; + +#[deriving(Clone, Eq)] +pub struct OBB2 { + center: Point2, + axis: Vec2, + extents: Vec2, +} + +#[deriving(Clone, Eq)] +pub struct OBB3 { + center: Point3, + axis: Vec3, + extents: Vec3, +} diff --git a/src/cgmath/plane.rs b/src/cgmath/plane.rs new file mode 100644 index 0000000..c72bfd8 --- /dev/null +++ b/src/cgmath/plane.rs @@ -0,0 +1,43 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use vector::Vec3; + +use std::fmt; + +/// A 3-dimendional plane formed from the equation: `Ax + Bx + Cx + D = 0` +/// +/// # Fields +/// +/// - `normal`: the normal of the plane where: +/// - `normal.x`: corresponds to `A` in the plane equation +/// - `normal.y`: corresponds to `B` in the plane equation +/// - `normal.z`: corresponds to `C` in the plane equation +/// - `distance`: the distance value, corresponding to `D` in the plane equation +#[deriving(Clone, Eq)] +pub struct Plane3 { + normal: Vec3, + distance: S, +} + +impl ToStr for Plane3 { + fn to_str(&self) -> ~str { + format!("{:f}x + {:f}y + {:f}z + {:f} = 0", + self.normal.x, + self.normal.y, + self.normal.z, + self.distance) + } +} diff --git a/src/cgmath/projection.rs b/src/cgmath/projection.rs index c6e72e7..bcde1db 100644 --- a/src/cgmath/projection.rs +++ b/src/cgmath/projection.rs @@ -16,6 +16,7 @@ use std::num::{zero, one}; use angle::{Angle, rad, tan, cot}; +use frustum::Frustum; use matrix::{Mat4, ToMat4}; use util::two; @@ -62,7 +63,9 @@ pub fn ortho(left: S, right: S, bottom: S, top: S, near: S, fa }.to_mat4() } -pub trait Projection: ToMat4 {} +pub trait Projection: ToMat4 { + fn to_frustum(&self) -> Frustum; +} /// A perspective projection based on a vertical field-of-view angle. #[deriving(Clone, Eq)] @@ -90,6 +93,12 @@ impl> PerspectiveFov { } } +impl> Projection for PerspectiveFov { + fn to_frustum(&self) -> Frustum { + self.to_perspective().to_frustum() + } +} + impl> ToMat4 for PerspectiveFov { fn to_mat4(&self) -> Mat4 { let half_turn: A = Angle::from(rad::(Real::frac_pi_2())); @@ -130,8 +139,6 @@ impl> ToMat4 for PerspectiveFov { } } -impl> Projection for PerspectiveFov; - /// A perspective projection with arbitrary left/right/bottom/top distances #[deriving(Clone, Eq)] pub struct Perspective { @@ -140,6 +147,12 @@ pub struct Perspective { near: S, far: S, } +impl Projection for Perspective { + fn to_frustum(&self) -> Frustum { + fail!("Not yet implemented!"); + } +} + impl ToMat4 for Perspective { fn to_mat4(&self) -> Mat4 { assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right); @@ -173,8 +186,6 @@ impl ToMat4 for Perspective { } } -impl Projection for Perspective; - /// An orthographic projection with arbitrary left/right/bottom/top distances #[deriving(Clone, Eq)] pub struct Ortho { @@ -183,6 +194,12 @@ pub struct Ortho { near: S, far: S, } +impl Projection for Ortho { + fn to_frustum(&self) -> Frustum { + fail!("Not yet implemented!"); + } +} + impl ToMat4 for Ortho { fn to_mat4(&self) -> Mat4 { assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right); @@ -215,5 +232,3 @@ impl ToMat4 for Ortho { c3r0, c3r1, c3r2, c3r3) } } - -impl Projection for Ortho; diff --git a/src/cgmath/sphere.rs b/src/cgmath/sphere.rs new file mode 100644 index 0000000..f1e1035 --- /dev/null +++ b/src/cgmath/sphere.rs @@ -0,0 +1,24 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Bounding sphere + +use point::Point3; + +#[deriving(Clone, Eq)] +pub struct Sphere { + center: Point3, + radius: S, +}