use super::{
actor::Actor,
constraint::Constraint,
math::{PxQuat, PxTransform, PxVec3},
shape::{CollisionLayers, Shape},
traits::Class,
};
#[rustfmt::skip]
use physx_sys::{
PxRigidActor,
PxRigidActor_attachShape_mut,
PxRigidActor_detachShape_mut,
PxRigidActor_getConstraints,
PxRigidActor_getGlobalPose,
PxRigidActor_getNbConstraints,
PxRigidActor_getNbShapes,
PxRigidActor_getShapes,
PxRigidActor_setGlobalPose_mut,
};
pub trait RigidActor: Class<PxRigidActor> + Actor {
type Shape: Shape;
fn get_nb_constraints(&self) -> u32 {
unsafe { PxRigidActor_getNbConstraints(self.as_ptr()) }
}
fn get_constraints(&mut self) -> Vec<&mut Constraint> {
let capacity = self.get_nb_constraints();
let mut buffer: Vec<&mut Constraint> = Vec::with_capacity(capacity as usize);
unsafe {
let len = PxRigidActor_getConstraints(
self.as_ptr(),
buffer.as_mut_ptr() as *mut *mut _,
capacity,
0,
);
buffer.set_len(len as usize);
}
buffer
}
fn get_global_pose(&self) -> PxTransform {
unsafe { PxRigidActor_getGlobalPose(self.as_ptr()).into() }
}
fn get_global_position(&self) -> PxVec3 {
unsafe { PxRigidActor_getGlobalPose(self.as_ptr()).p.into() }
}
fn get_global_rotation(&self) -> PxQuat {
unsafe { PxRigidActor_getGlobalPose(self.as_ptr()).q.into() }
}
fn set_global_pose(&mut self, pose: &PxTransform, autowake: bool) {
unsafe {
PxRigidActor_setGlobalPose_mut(self.as_mut_ptr(), pose.as_ptr(), autowake);
}
}
fn get_nb_shapes(&self) -> u32 {
unsafe { PxRigidActor_getNbShapes(self.as_ptr()) }
}
fn get_shapes(&self) -> Vec<&Self::Shape> {
let capacity = self.get_nb_shapes();
let mut buffer: Vec<&Self::Shape> = Vec::with_capacity(capacity as usize);
unsafe {
let len = PxRigidActor_getShapes(
self.as_ptr(),
buffer.as_mut_ptr() as *mut *mut _,
capacity,
0,
);
buffer.set_len(len as usize);
}
buffer
}
fn get_shapes_mut(&mut self) -> Vec<&mut Self::Shape> {
let capacity = self.get_nb_shapes();
let mut buffer: Vec<&mut Self::Shape> = Vec::with_capacity(capacity as usize);
unsafe {
let len = PxRigidActor_getShapes(
self.as_ptr(),
buffer.as_mut_ptr() as *mut *mut _,
capacity,
0,
);
buffer.set_len(len as usize);
}
buffer
}
fn set_collision_filter(
&mut self,
this_layers: CollisionLayers,
other_layers: CollisionLayers,
word3: u32,
word4: u32,
) {
for shape in self.get_shapes_mut() {
shape.set_simulation_filter_data(this_layers, other_layers, word3, word4);
}
}
fn set_query_filter(&mut self, this_layers: CollisionLayers) {
for shape in self.get_shapes_mut() {
shape.set_query_filter_data(this_layers);
}
}
fn attach_shape(&mut self, shape: &mut Self::Shape) -> bool {
unsafe { PxRigidActor_attachShape_mut(self.as_mut_ptr(), shape.as_mut_ptr()) }
}
fn detach_shape(&mut self, shape: &mut Self::Shape) {
unsafe { PxRigidActor_detachShape_mut(self.as_mut_ptr(), shape.as_mut_ptr(), true) };
}
}