Add collision types

This commit is contained in:
Brendan Zabarauskas 2013-09-05 17:19:31 +10:00
parent aef1d153a1
commit 28faaa2ccc
8 changed files with 228 additions and 7 deletions

31
src/cgmath/aabb.rs Normal file
View file

@ -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<S> {
center: Point2<S>,
size: Vec2<S>,
}
#[deriving(Clone, Eq)]
pub struct AABB3<S> {
center: Point3<S>,
size: Vec3<S>,
}

26
src/cgmath/cylinder.rs Normal file
View file

@ -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<S> {
center: Point3<S>,
axis: Vec3<S>,
radius: S,
}

41
src/cgmath/frustum.rs Normal file
View file

@ -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<S> {
left: Plane3<S>,
right: Plane3<S>,
bottom: Plane3<S>,
top: Plane3<S>,
near: Plane3<S>,
far: Plane3<S>,
}
#[deriving(Clone, Eq)]
pub struct FrustumPoints<S> {
near_top_left: Point3<S>,
near_top_right: Point3<S>,
near_bottom_left: Point3<S>,
near_bottom_right: Point3<S>,
far_top_left: Point3<S>,
far_top_right: Point3<S>,
far_bottom_left: Point3<S>,
far_bottom_right: Point3<S>,
}

View file

@ -28,11 +28,19 @@ pub mod quaternion;
pub mod vector; pub mod vector;
pub mod angle; pub mod angle;
pub mod plane;
pub mod point; pub mod point;
pub mod ray; pub mod ray;
pub mod rotation;
pub mod frustum;
pub mod projection; pub mod projection;
pub mod aabb;
pub mod cylinder;
pub mod obb;
pub mod sphere;
pub mod util { pub mod util {
use std::num::one; use std::num::one;

33
src/cgmath/obb.rs Normal file
View file

@ -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<S> {
center: Point2<S>,
axis: Vec2<S>,
extents: Vec2<S>,
}
#[deriving(Clone, Eq)]
pub struct OBB3<S> {
center: Point3<S>,
axis: Vec3<S>,
extents: Vec3<S>,
}

43
src/cgmath/plane.rs Normal file
View file

@ -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<S> {
normal: Vec3<S>,
distance: S,
}
impl<S: Clone + fmt::Float> ToStr for Plane3<S> {
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)
}
}

View file

@ -16,6 +16,7 @@
use std::num::{zero, one}; use std::num::{zero, one};
use angle::{Angle, rad, tan, cot}; use angle::{Angle, rad, tan, cot};
use frustum::Frustum;
use matrix::{Mat4, ToMat4}; use matrix::{Mat4, ToMat4};
use util::two; use util::two;
@ -62,7 +63,9 @@ pub fn ortho<S: Clone + Float>(left: S, right: S, bottom: S, top: S, near: S, fa
}.to_mat4() }.to_mat4()
} }
pub trait Projection<S>: ToMat4<S> {} pub trait Projection<S>: ToMat4<S> {
fn to_frustum(&self) -> Frustum<S>;
}
/// A perspective projection based on a vertical field-of-view angle. /// A perspective projection based on a vertical field-of-view angle.
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
@ -90,6 +93,12 @@ impl<S: Clone + Float, A: Angle<S>> PerspectiveFov<S, A> {
} }
} }
impl<S: Clone + Float, A: Angle<S>> Projection<S> for PerspectiveFov<S, A> {
fn to_frustum(&self) -> Frustum<S> {
self.to_perspective().to_frustum()
}
}
impl<S: Clone + Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> { impl<S: Clone + Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
let half_turn: A = Angle::from(rad::<S>(Real::frac_pi_2())); let half_turn: A = Angle::from(rad::<S>(Real::frac_pi_2()));
@ -130,8 +139,6 @@ impl<S: Clone + Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
} }
} }
impl<S: Clone + Float, A: Angle<S>> Projection<S> for PerspectiveFov<S, A>;
/// A perspective projection with arbitrary left/right/bottom/top distances /// A perspective projection with arbitrary left/right/bottom/top distances
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Perspective<S> { pub struct Perspective<S> {
@ -140,6 +147,12 @@ pub struct Perspective<S> {
near: S, far: S, near: S, far: S,
} }
impl<S: Clone + Float> Projection<S> for Perspective<S> {
fn to_frustum(&self) -> Frustum<S> {
fail!("Not yet implemented!");
}
}
impl<S: Clone + Float> ToMat4<S> for Perspective<S> { impl<S: Clone + Float> ToMat4<S> for Perspective<S> {
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right); assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right);
@ -173,8 +186,6 @@ impl<S: Clone + Float> ToMat4<S> for Perspective<S> {
} }
} }
impl<S: Clone + Float> Projection<S> for Perspective<S>;
/// An orthographic projection with arbitrary left/right/bottom/top distances /// An orthographic projection with arbitrary left/right/bottom/top distances
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Ortho<S> { pub struct Ortho<S> {
@ -183,6 +194,12 @@ pub struct Ortho<S> {
near: S, far: S, near: S, far: S,
} }
impl<S: Clone + Float> Projection<S> for Ortho<S> {
fn to_frustum(&self) -> Frustum<S> {
fail!("Not yet implemented!");
}
}
impl<S: Clone + Float> ToMat4<S> for Ortho<S> { impl<S: Clone + Float> ToMat4<S> for Ortho<S> {
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right); assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right);
@ -215,5 +232,3 @@ impl<S: Clone + Float> ToMat4<S> for Ortho<S> {
c3r0, c3r1, c3r2, c3r3) c3r0, c3r1, c3r2, c3r3)
} }
} }
impl<S: Clone + Float> Projection<S> for Ortho<S>;

24
src/cgmath/sphere.rs Normal file
View file

@ -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<S> {
center: Point3<S>,
radius: S,
}