Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oxide engine/feat/physics particle #55

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions oxide_physics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "oxide_physics"
version = "0.0.1"
authors = [
"Chris Ohk <[email protected]>",
"Changseo Jang <[email protected]>",
"Yongwook Choi <[email protected]>",
"Chaneun Yeo <[email protected]>",
"Seokwon Moon <[email protected]>",
"Oxide Engine"
]
edition = "2021"
description = "Physics library for Oxide"

repository = "https://github.com/OxideEngine/Oxide"

license = "MIT"

[lib]
name = "oxide_physics"
path = "src/lib.rs"

[dependencies]
oxide_math = {path = "../oxide_math", version = "0.0.1"}
num-traits = "0.2"
generational-arena = "0.2"
165 changes: 165 additions & 0 deletions oxide_physics/src/aabb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
use crate::collide_broad_phase::{BoundingVolume, HasBoundingVolume};
use oxide_math::commons::vector3::Vector3;

#[derive(Debug, PartialEq)]
pub struct AABB {
pub mins: Vector3,
pub maxs: Vector3,
}

pub fn aabb<S>(shape: &S, tv: Vector3) -> AABB
where
S: HasBoundingVolume<AABB>,
{
shape.bounding_volume(tv)
}

pub fn local_aabb<S>(shape: &S) -> AABB
where
S: HasBoundingVolume<AABB>,
{
shape.local_bounding_volume()
}

impl AABB {
pub fn new(mins: Vector3, maxs: Vector3) -> AABB {
AABB { mins, maxs }
}

pub fn mins(&self) -> Vector3 {
Vector3 {
x: self.mins.x,
y: self.mins.y,
z: self.mins.z,
}
}

pub fn maxs(&self) -> Vector3 {
Vector3 {
x: self.maxs.x,
y: self.maxs.y,
z: self.maxs.z,
}
}
}

impl BoundingVolume for AABB {
// check if the bounding volume 'bv' intersects with self
fn intersects(&self, other: &AABB) -> bool {
self.mins.x <= other.maxs.x
&& self.mins.y <= other.maxs.y
&& self.mins.z <= other.maxs.z
&& self.maxs.x >= other.mins.x
&& self.maxs.y >= other.mins.y
&& self.maxs.z >= other.mins.z
}

// check if self contains the 'bv'
fn contains(&self, other: &AABB) -> bool {
self.mins.x <= other.mins.x
&& self.mins.y <= other.mins.y
&& self.mins.z <= other.mins.z
&& self.maxs.x >= other.maxs.x
&& self.maxs.y >= other.maxs.y
&& self.maxs.z >= other.maxs.z
}

// merge this bounding volume with the other 'bv'
fn merged(&self, other: &AABB) -> AABB {
AABB {
mins: Vector3 {
x: self.mins.x.min(other.mins.x),
y: self.mins.y.min(other.mins.y),
z: self.mins.z.min(other.mins.z),
},
maxs: Vector3 {
x: self.maxs.x.max(other.maxs.x),
y: self.maxs.y.max(other.maxs.y),
z: self.maxs.z.max(other.maxs.z),
},
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::shape::Ball;

#[test]
fn test_intersects() {
let ball = Ball::new(1.0).unwrap();
let ball_aabb0 = ball.local_bounding_volume();
let ball_aabb1 = ball.bounding_volume(Vector3 {
x: 0.5,
y: 0.5,
z: 0.5,
});
let ball_aabb2 = ball.bounding_volume(Vector3 {
x: 1.5,
y: 1.5,
z: 1.5,
});
let ball_aabb3 = ball.bounding_volume(Vector3 {
x: 3.0,
y: 1.0,
z: 1.0,
});
assert_eq!(true, ball_aabb0.intersects(&ball_aabb1));
assert_eq!(true, ball_aabb0.intersects(&ball_aabb2));
assert_eq!(false, ball_aabb0.intersects(&ball_aabb3));
}

#[test]
fn test_contains() {
let ball = Ball::new(1.0).unwrap();
let bigball = Ball::new(3.0).unwrap();
let ball_aabb0 = ball.local_bounding_volume();
let ball_aabb1 = ball.bounding_volume(Vector3 {
x: 0.5,
y: 0.5,
z: 0.5,
});
let ball_aabb2 = ball.bounding_volume(Vector3 {
x: 1.5,
y: 1.5,
z: 1.5,
});
let ball_aabb3 = ball.bounding_volume(Vector3 {
x: 3.0,
y: 1.0,
z: 1.0,
});
let bigball_aabb = bigball.local_bounding_volume();
assert_eq!(false, ball_aabb0.contains(&ball_aabb1));
assert_eq!(true, bigball_aabb.contains(&ball_aabb2));
assert_eq!(false, bigball_aabb.contains(&ball_aabb3));
}

#[test]
fn test_merged() {
let ball = Ball::new(1.0).unwrap();
let bigball = Ball::new(3.0).unwrap();
let ball_aabb = ball.bounding_volume(Vector3 {
x: 3.0,
y: 1.0,
z: 1.0,
});
let bigball_aabb = bigball.local_bounding_volume();
assert_eq!(
ball_aabb.merged(&bigball_aabb),
AABB {
mins: Vector3 {
x: -3.0,
y: -3.0,
z: -3.0
},
maxs: Vector3 {
x: 4.0,
y: 3.0,
z: 3.0
},
}
);
}
}
91 changes: 91 additions & 0 deletions oxide_physics/src/aabb_ball.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::aabb::AABB;
use crate::collide_broad_phase::HasBoundingVolume;
use crate::shape::Ball;
use oxide_math::commons::vector3::Vector3;

pub fn ball_aabb(center: Vector3, radius: f32) -> AABB {
AABB::new(
Vector3 {
x: center.x - radius,
y: center.y - radius,
z: center.z - radius,
},
Vector3 {
x: center.x + radius,
y: center.y + radius,
z: center.z + radius,
},
)
}

pub fn local_ball_aabb(radius: f32) -> AABB {
ball_aabb(
Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
radius,
)
}

impl HasBoundingVolume<AABB> for Ball {
fn bounding_volume(&self, tv: Vector3) -> AABB {
ball_aabb(tv, self.radius())
}

fn local_bounding_volume(&self) -> AABB {
local_ball_aabb(self.radius())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ball_aabb() {
let ball = Ball::new(4.0).unwrap();
let ball_aabb = ball.bounding_volume(Vector3 {
x: 3.0,
y: 4.0,
z: 5.0,
});
assert_eq!(
ball_aabb,
AABB {
mins: Vector3 {
x: -1.0,
y: 0.0,
z: 1.0
},
maxs: Vector3 {
x: 7.0,
y: 8.0,
z: 9.0
},
}
);
}

#[test]
fn test_local_aabb() {
let ball = Ball::new(4.0).unwrap();
let ball_aabb = ball.local_bounding_volume();
assert_eq!(
ball_aabb,
AABB {
mins: Vector3 {
x: -4.0,
y: -4.0,
z: -4.0
},
maxs: Vector3 {
x: 4.0,
y: 4.0,
z: 4.0
},
}
);
}
}
84 changes: 84 additions & 0 deletions oxide_physics/src/aabb_cuboid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use crate::aabb::AABB;
use crate::collide_broad_phase::HasBoundingVolume;
use crate::shape::Cuboid;
use oxide_math::commons::vector3::Vector3;

impl HasBoundingVolume<AABB> for Cuboid {
fn bounding_volume(&self, tv: Vector3) -> AABB {
let tv2 = Vector3 {
x: tv.x,
y: tv.y,
z: tv.z,
};
AABB::new(self.mins() + tv, self.maxs() + tv2)
}

fn local_bounding_volume(&self) -> AABB {
self.bounding_volume(Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_cuboid_aabb() {
let cuboid = Cuboid::new(Vector3 {
x: 3.0,
y: 4.0,
z: 5.0,
})
.unwrap();
let cuboid_aabb = cuboid.bounding_volume(Vector3 {
x: 3.0,
y: 4.0,
z: 5.0,
});
assert_eq!(
cuboid_aabb,
AABB {
mins: Vector3 {
x: 0.0,
y: 0.0,
z: 0.0
},
maxs: Vector3 {
x: 6.0,
y: 8.0,
z: 10.0
},
}
);
}

#[test]
fn test_local_aabb() {
let cuboid = Cuboid::new(Vector3 {
x: 3.0,
y: 4.0,
z: 5.0,
})
.unwrap();
let cuboid_aabb = cuboid.local_bounding_volume();
assert_eq!(
cuboid_aabb,
AABB {
mins: Vector3 {
x: -3.0,
y: -4.0,
z: -5.0
},
maxs: Vector3 {
x: 3.0,
y: 4.0,
z: 5.0
},
}
);
}
}
26 changes: 26 additions & 0 deletions oxide_physics/src/collide_broad_phase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use oxide_math::commons::vector3::Vector3;

pub trait BoundingVolume {
// check if the bounding volume 'bv' intersects with self
fn intersects(&self, bv: &Self) -> bool;

// check if self contains the 'bv'
fn contains(&self, bv: &Self) -> bool;

// merge this bounding volume with the other 'bv'
fn merged(&self, bv: &Self) -> Self;
}

pub trait HasBoundingVolume<BV> {
// TBD: rotation by 4x4 matrix
// bounding volume of 'self' translated by 'tv'
fn bounding_volume(&self, tv: Vector3) -> BV;

fn local_bounding_volume(&self) -> BV {
self.bounding_volume(Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
})
}
}
Loading