1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Author: Tom Olsson <tom.olsson@embark-studios.com>

// Copyright © 2019, Embark Studios, all rights reserved.

// Created:  2 April 2019


#![warn(clippy::all)]

/*!

*/
use super::{
    articulation_joint_base::*, articulation_link::ArticulationDriveType, body::*, px_type::*,
};
use glam::{Mat4, Vec3};
use log::*;
use physx_macros::*;
use physx_sys::*;

/* todo[tolsson]
add a wrapper struct for PxArticulationJointReducedCoordinate (or trait object)
 */

////////////////////////////////////////////////////////////////////////////////

// Section ENUMS

////////////////////////////////////////////////////////////////////////////////


#[derive(Debug, Clone, Copy)]
pub enum ArticulationJointDriveType {
    Target,
    Error,
}

impl Into<PxArticulationJointDriveType::Enum> for ArticulationJointDriveType {
    fn into(self) -> PxArticulationJointDriveType::Enum {
        match self {
            ArticulationJointDriveType::Target => PxArticulationJointDriveType::eTARGET,
            ArticulationJointDriveType::Error => PxArticulationJointDriveType::eERROR,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////


#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub enum ArticulationMotion {
    Locked,
    Limited,
    Free,
}

impl Into<PxArticulationAxis::Enum> for ArticulationMotion {
    fn into(self) -> PxArticulationMotion::Enum {
        match self {
            ArticulationMotion::Locked => PxArticulationMotion::eLOCKED,
            ArticulationMotion::Limited => PxArticulationMotion::eLIMITED,
            ArticulationMotion::Free => PxArticulationMotion::eFREE,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////


#[derive(Debug, Clone, Copy)]
pub enum ArticulationAxis {
    Twist,
    Swing1,
    Swing2,
    X,
    Y,
    Z,
}

impl ArticulationAxis {
    /// Iterator over all available axes

    pub fn all_axes() -> impl Iterator<Item = &'static Self> {
        [
            ArticulationAxis::Twist,
            ArticulationAxis::Swing1,
            ArticulationAxis::Swing2,
            ArticulationAxis::X,
            ArticulationAxis::Y,
            ArticulationAxis::Z,
        ]
        .iter()
    }

    /// Iterator over all linear (translative) axss

    pub fn linear_axes() -> impl Iterator<Item = &'static Self> {
        [
            ArticulationAxis::X,
            ArticulationAxis::Y,
            ArticulationAxis::Z,
        ]
        .iter()
    }

    /// Iterator over all angular (rotational) axes

    pub fn angular_axes() -> impl Iterator<Item = &'static Self> {
        [
            ArticulationAxis::Twist,
            ArticulationAxis::Swing1,
            ArticulationAxis::Swing2,
        ]
        .iter()
    }
}

impl Into<PxArticulationAxis::Enum> for ArticulationAxis {
    fn into(self) -> PxArticulationAxis::Enum {
        match self {
            ArticulationAxis::Twist => PxArticulationAxis::eTWIST,
            ArticulationAxis::Swing1 => PxArticulationAxis::eSWING1,
            ArticulationAxis::Swing2 => PxArticulationAxis::eSWING2,
            ArticulationAxis::X => PxArticulationAxis::eX,
            ArticulationAxis::Y => PxArticulationAxis::eY,
            ArticulationAxis::Z => PxArticulationAxis::eZ,
        }
    }
}

/******************************************************************************/

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ArticulationJointType {
    Prismatic,
    Revolute,
    Spherical,
    Fix,
    Undefined,
}

impl Into<PxArticulationJointType::Enum> for ArticulationJointType {
    fn into(self) -> PxArticulationJointType::Enum {
        match self {
            ArticulationJointType::Prismatic => PxArticulationJointType::ePRISMATIC,
            ArticulationJointType::Revolute => PxArticulationJointType::eREVOLUTE,
            ArticulationJointType::Spherical => PxArticulationJointType::eSPHERICAL,
            ArticulationJointType::Fix => PxArticulationJointType::eFIX,
            ArticulationJointType::Undefined => PxArticulationJointType::eUNDEFINED,
        }
    }
}

impl From<PxArticulationJointType::Enum> for ArticulationJointType {
    fn from(other: PxArticulationJointType::Enum) -> Self {
        match other {
            PxArticulationJointType::ePRISMATIC => ArticulationJointType::Prismatic,
            PxArticulationJointType::eREVOLUTE => ArticulationJointType::Revolute,
            PxArticulationJointType::eSPHERICAL => ArticulationJointType::Spherical,
            PxArticulationJointType::eFIX => ArticulationJointType::Fix,
            PxArticulationJointType::eUNDEFINED => ArticulationJointType::Undefined,
            _ => panic!("invalid joint type"),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

// Section BUILDER

////////////////////////////////////////////////////////////////////////////////


#[physx_type(inherit = "ArticulationJointBase")]
impl ArticulationJointReducedCoordinate {
    /// Set target angle around axis for this joint

    pub fn set_target(&mut self, rot: f32, axis: ArticulationAxis) {
        unsafe {
            PxArticulationJointReducedCoordinate_setDriveTarget_mut(
                self.get_raw_mut(),
                axis.into(),
                rot,
            )
        };
    }

    /// set joint type

    pub fn set_type(&mut self, _type: ArticulationJointType) {
        unsafe {
            PxArticulationJointReducedCoordinate_setJointType_mut(self.get_raw_mut(), _type.into())
        }
    }

    /// get joint type

    pub fn get_type(&self) -> ArticulationJointType {
        ArticulationJointType::from(unsafe {
            PxArticulationJointReducedCoordinate_getJointType(self.get_raw())
        })
    }

    /// Set motion limits for Axis

    pub fn set_limit(&mut self, axis: ArticulationAxis, min: f32, max: f32) {
        unsafe {
            PxArticulationJointReducedCoordinate_setLimit_mut(
                self.get_raw_mut(),
                axis.into(),
                min,
                max,
            );
        }
    }

    /// set drive type of the associated joints

    pub fn set_drive(
        &mut self,
        axis: ArticulationAxis,
        stiffness: f32,
        damping: f32,
        max_force: f32,
        _type: ArticulationDriveType,
    ) {
        unsafe {
            PxArticulationJointReducedCoordinate_setDrive_mut(
                self.get_raw_mut(),
                axis.into(),
                stiffness,
                damping,
                max_force,
                _type.into(),
            )
        }
    }

    pub fn set_motion(&mut self, axis: ArticulationAxis, motion: ArticulationMotion) {
        unsafe {
            PxArticulationJointReducedCoordinate_setMotion_mut(
                self.get_raw_mut(),
                axis.into(),
                motion.into(),
            );
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

// Section BUILDER

////////////////////////////////////////////////////////////////////////////////


// todo[tolsson]

pub struct JointBuilder {
    joint_type: ArticulationJointType,
    axis: Vec3,
    joint_frame: Mat4,
    joint_limit: [f32; 2],
}

impl Default for JointBuilder {
    fn default() -> Self {
        Self {
            joint_type: ArticulationJointType::Undefined,
            axis: Vec3::unit_x(),
            joint_frame: Mat4::identity(),
            joint_limit: [-std::f32::consts::PI, std::f32::consts::PI],
        }
    }
}

impl JointBuilder {
    pub fn build(&self, part: PartHandle) -> ArticulationJointReducedCoordinate {
        let z_axis = self.axis == Vec3::unit_z();
        let link = part.1 as *mut PxArticulationLink;

        unsafe {
            let joint = PxArticulationLink_getInboundJoint(link);

            let mut joint = ArticulationJointReducedCoordinate::from_ptr(
                joint as *mut PxArticulationJointReducedCoordinate,
            );
            joint.set_child_pose(Mat4::identity());
            joint.set_parent_pose(self.joint_frame);

            joint.set_type(self.joint_type);
            match self.joint_type {
                ArticulationJointType::Revolute => {
                    let axis = if z_axis {
                        ArticulationAxis::Swing2
                    } else {
                        ArticulationAxis::Twist
                    };
                    joint.set_motion(axis, ArticulationMotion::Limited);
                    joint.set_limit(axis, self.joint_limit[0], self.joint_limit[1]);
                }

                ArticulationJointType::Spherical => {
                    for axis in ArticulationAxis::angular_axes() {
                        joint.set_motion(*axis, ArticulationMotion::Free);
                    }
                }
                _ => { /* fixme */ }
            }

            joint
        }
    }

    pub fn joint_type(mut self, joint_type: ArticulationJointType) -> Self {
        self.joint_type = joint_type;
        self
    }

    pub fn axis(mut self, axis: Vec3) -> Self {
        self.axis = axis;
        self
    }

    pub fn transform_from_parent(mut self, transform: Mat4) -> Self {
        self.joint_frame = transform;
        self
    }

    pub fn limit(mut self, lower: f32, upper: f32) -> Self {
        self.joint_limit = [lower, upper];
        self
    }

    pub fn get_joint_type(&self) -> ArticulationJointType {
        self.joint_type
    }

    pub fn joint_transform(&self) -> Mat4 {
        self.joint_frame
    }
}