1use std::ptr::null_mut;
2
3use crate::{
4 AsPxActor, AsPxBase, AsPxRigidActor, AsPxRigidBody, PxActorRef, PxBaseRef, PxRigidActorRef,
5 PxRigidBodyRef, PxTransform,
6};
7
8pub trait AsArticulationBase {
9 fn as_articulation_base_ptr(&self) -> *mut physx_sys::PxArticulationBase;
10}
11
12pub trait AsArticulationJointBase {
13 fn as_articulation_joint_base_ptr(&self) -> *mut physx_sys::PxArticulationJointBase;
14}
15
16pub trait PxArticulationBase {
17 fn wake_up(&self);
18 fn set_solver_iteration_counts(
19 &self,
20 min_position_iterations: u32,
21 min_velocity_iterations: u32,
22 );
23 fn get_solver_iteration_counts(&self) -> (u32, u32);
24}
25impl<T: AsArticulationBase> PxArticulationBase for T {
26 fn wake_up(&self) {
27 unsafe { physx_sys::PxArticulationBase_wakeUp_mut(self.as_articulation_base_ptr()) }
28 }
29 fn set_solver_iteration_counts(
30 &self,
31 min_position_iterations: u32,
32 min_velocity_iterations: u32,
33 ) {
34 unsafe {
35 physx_sys::PxArticulationBase_setSolverIterationCounts_mut(
36 self.as_articulation_base_ptr(),
37 min_position_iterations,
38 min_velocity_iterations,
39 )
40 }
41 }
42 fn get_solver_iteration_counts(&self) -> (u32, u32) {
43 let mut p = 0u32;
44 let mut v = 0u32;
45 unsafe {
46 physx_sys::PxArticulationBase_getSolverIterationCounts(
47 self.as_articulation_base_ptr(),
48 &mut p,
49 &mut v,
50 )
51 }
52 (p, v)
53 }
54}
55
56pub trait PxArticulationJointBase {
57 fn get_parent_pose(&self) -> PxTransform;
58 fn get_child_pose(&self) -> PxTransform;
59 fn set_parent_pose(&self, pose: &PxTransform);
60 fn set_child_pose(&self, pose: &PxTransform);
61}
62impl<T: AsArticulationJointBase> PxArticulationJointBase for T {
63 fn get_parent_pose(&self) -> PxTransform {
64 let t = unsafe {
65 physx_sys::PxArticulationJointBase_getParentPose(self.as_articulation_joint_base_ptr())
66 };
67 PxTransform(t)
68 }
69 fn get_child_pose(&self) -> PxTransform {
70 let t = unsafe {
71 physx_sys::PxArticulationJointBase_getChildPose(self.as_articulation_joint_base_ptr())
72 };
73 PxTransform(t)
74 }
75 fn set_parent_pose(&self, pose: &PxTransform) {
76 unsafe {
77 physx_sys::PxArticulationJointBase_setParentPose_mut(
78 self.as_articulation_joint_base_ptr(),
79 &pose.0,
80 )
81 }
82 }
83 fn set_child_pose(&self, pose: &PxTransform) {
84 unsafe {
85 physx_sys::PxArticulationJointBase_setChildPose_mut(
86 self.as_articulation_joint_base_ptr(),
87 &pose.0,
88 )
89 }
90 }
91}
92pub struct PxArticulationJointBaseRef(*mut physx_sys::PxArticulationJointBase);
93
94#[derive(Debug, Clone, Copy)]
95pub struct PxArticulationLinkRef(pub(crate) *mut physx_sys::PxArticulationLink);
96impl PxArticulationLinkRef {
97 pub fn new(
98 articulation: &dyn AsArticulationBase,
99 parent: Option<&PxArticulationLinkRef>,
100 pose: &PxTransform,
101 ) -> Self {
102 Self(unsafe {
103 physx_sys::PxArticulationBase_createLink_mut(
104 articulation.as_articulation_base_ptr(),
105 parent.map(|x| x.0).unwrap_or(null_mut()),
106 &pose.0,
107 )
108 })
109 }
110 pub fn get_inbound_joint(&self) -> PxArticulationJointBaseRef {
111 PxArticulationJointBaseRef(unsafe {
112 physx_sys::PxArticulationLink_getInboundJoint(self.0) as _
113 })
114 }
115 pub fn get_link_index(&self) -> u32 {
116 unsafe { physx_sys::PxArticulationLink_getLinkIndex(self.0) }
117 }
118 pub fn get_inbound_joint_dof(&self) -> u32 {
119 unsafe { physx_sys::PxArticulationLink_getInboundJointDof(self.0) }
120 }
121 pub fn release(&mut self) {
122 unsafe { physx_sys::PxArticulationLink_release_mut(self.0) }
123 }
124}
125impl AsPxBase for PxArticulationLinkRef {
126 fn as_base(&self) -> PxBaseRef {
127 PxBaseRef(self.0 as _)
128 }
129}
130impl AsPxActor for PxArticulationLinkRef {
131 fn as_actor(&self) -> PxActorRef {
132 PxActorRef(self.0 as _)
133 }
134}
135impl AsPxRigidActor for PxArticulationLinkRef {
136 fn as_rigid_actor(&self) -> PxRigidActorRef {
137 PxRigidActorRef(self.0 as _)
138 }
139}
140impl AsPxRigidBody for PxArticulationLinkRef {
141 fn as_rigid_body(&self) -> PxRigidBodyRef {
142 PxRigidBodyRef(self.0 as _)
143 }
144}
145unsafe impl Sync for PxArticulationLinkRef {}
146unsafe impl Send for PxArticulationLinkRef {}
147
148bitflags! {
149 pub struct PxArticulationCacheFlags: u8 {
150 const Velocity = 0b00000001;
151 const Acceleration = 0b00000010;
152 const Position = 0b00000100;
153 const Force = 0b00001000;
154 const Root = 0b00010000;
155 const All = Self::Velocity.bits | Self::Acceleration.bits | Self::Position.bits | Self::Force.bits | Self::Root.bits;
156 }
157}
158impl Default for PxArticulationCacheFlags {
159 fn default() -> Self {
160 Self::All
161 }
162}
163
164#[repr(u32)]
165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum PxArticulationJointType {
167 Prismatic = physx_sys::PxArticulationJointType::ePRISMATIC,
168 Revolute = physx_sys::PxArticulationJointType::eREVOLUTE,
169 Spherical = physx_sys::PxArticulationJointType::eSPHERICAL,
170 Fix = physx_sys::PxArticulationJointType::eFIX,
171 Undefined = physx_sys::PxArticulationJointType::eUNDEFINED,
172}
173
174#[repr(u32)]
175#[derive(Debug, Clone, Copy)]
176pub enum PxArticulationMotion {
177 Locked = physx_sys::PxArticulationMotion::eLOCKED,
178 Limited = physx_sys::PxArticulationMotion::eLIMITED,
179 Free = physx_sys::PxArticulationMotion::eFREE,
180}
181
182#[repr(u32)]
183#[derive(Debug, Clone, Copy)]
184pub enum PxArticulationAxis {
185 Twist = physx_sys::PxArticulationAxis::eTWIST,
186 Swing1 = physx_sys::PxArticulationAxis::eSWING1,
187 Swing2 = physx_sys::PxArticulationAxis::eSWING2,
188 X = physx_sys::PxArticulationAxis::eX,
189 Y = physx_sys::PxArticulationAxis::eY,
190 Z = physx_sys::PxArticulationAxis::eZ,
191}
192
193#[repr(u32)]
194#[derive(Debug, Clone, Copy)]
195pub enum PxArticulationDriveType {
196 Acceleration = physx_sys::PxArticulationDriveType::eACCELERATION,
197 Force = physx_sys::PxArticulationDriveType::eFORCE,
198}
199
200#[derive(Copy, Clone, Debug)]
201#[repr(u8)]
202pub enum PxArticulationFlag {
203 FixBase = 1 << 0,
204 DriveLimitsAreForces = 1 << 1,
205}
206
207impl From<PxArticulationFlag> for physx_sys::PxArticulationFlag::Enum {
208 fn from(sys: PxArticulationFlag) -> Self {
209 sys as _
210 }
211}