use crate::distance::ShapeProxy;
use crate::math_functions::{Matrix3, Plane, Vec3, MAT3_ZERO, VEC3_ZERO};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MassData {
pub mass: f32,
pub center: Vec3,
pub inertia: Matrix3,
}
impl Default for MassData {
fn default() -> Self {
MassData {
mass: 0.0,
center: VEC3_ZERO,
inertia: MAT3_ZERO,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Sphere {
pub center: Vec3,
pub radius: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Capsule {
pub center1: Vec3,
pub center2: Vec3,
pub radius: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RayCastInput {
pub origin: Vec3,
pub translation: Vec3,
pub max_fraction: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct ShapeCastInput {
pub proxy: ShapeProxy,
pub translation: Vec3,
pub max_fraction: f32,
pub can_encroach: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PlaneResult {
pub plane: Plane,
pub point: Vec3,
}
impl Default for PlaneResult {
fn default() -> Self {
PlaneResult {
plane: Plane {
normal: VEC3_ZERO,
offset: 0.0,
},
point: VEC3_ZERO,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CollisionPlane {
pub plane: Plane,
pub push_limit: f32,
pub push: f32,
pub clip_velocity: bool,
}
impl Default for CollisionPlane {
fn default() -> Self {
CollisionPlane {
plane: Plane {
normal: VEC3_ZERO,
offset: 0.0,
},
push_limit: 0.0,
push: 0.0,
clip_velocity: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct PlaneSolverResult {
pub delta: Vec3,
pub iteration_count: i32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub struct SurfaceMaterial {
pub friction: f32,
pub restitution: f32,
pub rolling_resistance: f32,
pub tangent_velocity: Vec3,
pub user_material_id: u64,
pub custom_color: u32,
}
impl Default for SurfaceMaterial {
fn default() -> Self {
default_surface_material()
}
}
pub fn default_surface_material() -> SurfaceMaterial {
SurfaceMaterial {
friction: 0.6,
restitution: 0.0,
rolling_resistance: 0.0,
tangent_velocity: VEC3_ZERO,
user_material_id: 0,
custom_color: 0,
}
}
pub const SURFACE_MATERIAL_SIZE: usize = 40;
impl SurfaceMaterial {
pub fn to_bytes(self) -> [u8; SURFACE_MATERIAL_SIZE] {
let mut buf = [0u8; SURFACE_MATERIAL_SIZE];
buf[0..4].copy_from_slice(&self.friction.to_le_bytes());
buf[4..8].copy_from_slice(&self.restitution.to_le_bytes());
buf[8..12].copy_from_slice(&self.rolling_resistance.to_le_bytes());
buf[12..16].copy_from_slice(&self.tangent_velocity.x.to_le_bytes());
buf[16..20].copy_from_slice(&self.tangent_velocity.y.to_le_bytes());
buf[20..24].copy_from_slice(&self.tangent_velocity.z.to_le_bytes());
buf[24..32].copy_from_slice(&self.user_material_id.to_le_bytes());
buf[32..36].copy_from_slice(&self.custom_color.to_le_bytes());
buf
}
pub fn from_bytes(buf: &[u8]) -> Self {
debug_assert!(buf.len() >= SURFACE_MATERIAL_SIZE);
let read_f32 = |o: usize| f32::from_le_bytes(buf[o..o + 4].try_into().unwrap());
Self {
friction: read_f32(0),
restitution: read_f32(4),
rolling_resistance: read_f32(8),
tangent_velocity: Vec3 {
x: read_f32(12),
y: read_f32(16),
z: read_f32(20),
},
user_material_id: u64::from_le_bytes(buf[24..32].try_into().unwrap()),
custom_color: u32::from_le_bytes(buf[32..36].try_into().unwrap()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(i32)]
pub enum ShapeType {
#[default]
Capsule = 0,
Compound = 1,
Height = 2,
Hull = 3,
Mesh = 4,
Sphere = 5,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct ShapeExtent {
pub min_extent: f32,
pub max_extent: Vec3,
}