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
use std::marker::PhantomData;
use std::mem;
use std::sync::RwLock;
use std::time::Duration;

use crate::collision::{Collision, NewtonCollision};
use crate::ffi;
use crate::handle::{AsHandle, FromHandle, Handle, IntoHandle};
use crate::joint::iter::Joints;
use crate::math::{Mat4, Vec3};
use crate::newton::Newton;

/// Body iterators.
pub mod iter;

#[repr(i32)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum SleepState {
    Active = 0,
    Sleeping = 1,
}

#[repr(u32)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
enum Type {
    Dynamic = ffi::NEWTON_DYNAMIC_BODY,
    Kinematic = ffi::NEWTON_KINEMATIC_BODY,
}

#[derive(Default)]
struct UserData {
    /// Callback only applicable to dynamic bodies.
    /// Where you apply weight and other forces on the body.
    force_and_torque: Option<Box<dyn FnMut(Body, Duration, usize)>>,

    /// Body transform callback.
    /// This closure is called whenever there is a change in the transformation of a body.
    transform: Option<Box<dyn FnMut(Body, Mat4, usize)>>,

    /// Destructor callback
    // Type is FnMut because FnOnce cannot be consumed when Boxed.
    destructor: Option<Box<dyn FnMut(Body)>>,

    /// An optional name given to the body when it is created.
    name: Option<&'static str>,

    // Lock used to write/read body properties such as position, matrix, collision,
    // etc, ...
    //
    // Everything else that is critical to safety (use after free, null references, etc)
    // should be handled at compile time by the borrow checker.
    lock: RwLock<()>,
}

// Where should I place the lifetime, in the type, or in the method??
trait IntoBody<'a> {
    fn into_body(self) -> Body<'a>;
}

impl<'a> IntoBody<'a> for Body<'a> {
    fn into_body(self) -> Body<'a> {
        self
    }
}

bodies! {
    /// Dynamic body wrapper.
    ///
    /// If a dynamic body has no mass, it is equivalent to a static body.
    #[derive(Debug, Eq, PartialEq)]
    (Dynamic, NewtonCreateDynamicBody, dynamic) => pub struct DynamicBody<'a>(...);

    /// A body that is not affected by forces and is controlled by the application.
    #[derive(Debug, Eq, PartialEq)]
    (Kinematic, NewtonCreateKinematicBody, kinematic) => pub struct KinematicBody<'a>(...);
}

macro_rules! lock {
    ($body:ident, read) => {
        unsafe {
            let udata = &ffi::NewtonBodyGetUserData($body.as_raw());
            let udata: &Box<UserData> = mem::transmute(udata);
            let _lock = udata.lock.read().unwrap();
        }
    };
    ($body:ident, write) => {
        unsafe {
            let udata = &ffi::NewtonBodyGetUserData($body.as_raw());
            let udata: &Box<UserData> = mem::transmute(udata);
            let _lock = udata.lock.write().unwrap();
        }
    };
}

unsafe extern "C" fn body_destructor(body: *const ffi::NewtonBody) {
    let udata = ffi::NewtonBodyGetUserData(body);
    let mut udata: Box<UserData> = Box::from_raw(udata as _);

    if let Some(mut destructor) = udata.destructor.take() {
        let body = Body::from_raw(body, false);
        destructor(body);
    }
}

/// Implementation of most of the NewtonBody API.
pub trait NewtonBody {
    fn as_raw(&self) -> *const ffi::NewtonBody;

    fn matrix(&self) -> Mat4 {
        lock!(self, read);
        let mut mat: Mat4 = Default::default();
        unsafe { ffi::NewtonBodyGetMatrix(self.as_raw(), mat[0].as_mut_ptr()) }
        mat
    }

    fn set_continuous(&self, cont: bool) {
        lock!(self, write);
        unsafe {
            let state = if cont { 1 } else { 0 };
            ffi::NewtonBodySetContinuousCollisionMode(self.as_raw(), state);
        }
    }

    fn is_continuous(&self) -> bool {
        lock!(self, write);
        unsafe {
            let state = ffi::NewtonBodyGetContinuousCollisionMode(self.as_raw());
            state == 1
        }
    }

    fn set_matrix(&self, matrix: Mat4) {
        lock!(self, write);
        unsafe { ffi::NewtonBodySetMatrix(self.as_raw(), matrix[0].as_ptr()) }
    }

    fn position(&self) -> Vec3 {
        lock!(self, read);
        let mut pos: Vec3 = Default::default();
        unsafe { ffi::NewtonBodyGetPosition(self.as_raw(), pos.as_mut_ptr()) }
        pos
    }

    fn into_raw(self) -> *const ffi::NewtonBody
        where Self: Sized
    {
        self.as_raw()
    }

    fn velocity(&self) -> Vec3 {
        lock!(self, read);
        let mut velo: Vec3 = Default::default();
        unsafe { ffi::NewtonBodyGetVelocity(self.as_raw(), velo.as_mut_ptr()) }
        velo
    }

    fn aabb(&self) -> (Vec3, Vec3) {
        lock!(self, read);
        let mut min: Vec3 = Default::default();
        let mut max: Vec3 = Default::default();
        unsafe {
            ffi::NewtonBodyGetAABB(self.as_raw(), min.as_mut_ptr(), max.as_mut_ptr());
        }
        (min, max)
    }

    /*
    fn body_type(&self) -> Type {
        unsafe {
            let btype = ffi::NewtonBodyGetType(self.as_raw());
            mem::transmute(btype)
        }
    }
    */

    fn sleep_state(&self) -> SleepState {
        lock!(self, read);
        unsafe {
            let state = ffi::NewtonBodyGetSleepState(self.as_raw());
            mem::transmute(state)
        }
    }

    fn set_sleep_state(&self, state: SleepState) {
        lock!(self, write);
        unsafe {
            let state = mem::transmute(state);
            ffi::NewtonBodySetSleepState(self.as_raw(), state);
        }
    }

    fn set_active(&self) {
        lock!(self, write);
        self.set_sleep_state(SleepState::Active)
    }

    fn set_sleeping(&self) {
        lock!(self, write);
        self.set_sleep_state(SleepState::Sleeping)
    }

    fn set_force(&self, force: Vec3) {
        lock!(self, write);
        unsafe { ffi::NewtonBodySetForce(self.as_raw(), force.as_ptr()) }
    }

    fn set_collision<C: NewtonCollision>(&self, collision: C) {
        lock!(self, write);
        unsafe { ffi::NewtonBodySetCollision(self.as_raw(), collision.as_raw()) }
    }

    fn collision(&self) -> Collision {
        lock!(self, read);
        unsafe {
            let collision = ffi::NewtonBodyGetCollision(self.as_raw());
            Collision::from_raw(collision, false)
        }
    }

    fn name(&self) -> Option<&'static str> {
        unsafe {
            let udata = &ffi::NewtonBodyGetUserData(self.as_raw());
            let udata: &Box<UserData> = mem::transmute(udata);
            udata.name
        }
    }

    fn mass(&self) -> (f32, Vec3) {
        lock!(self, read);
        let mut mass = 0.0;
        let mut i: Vec3 = [0.0, 0.0, 0.0];
        unsafe { ffi::NewtonBodyGetMass(self.as_raw(), &mut mass, &mut i[0], &mut i[1], &mut i[2]) }
        (mass, i)
    }

    fn set_mass<C: NewtonCollision>(&self, mass: f32, collision: &C) {
        lock!(self, write);
        let collision = collision.as_raw();
        unsafe {
            ffi::NewtonBodySetMassProperties(self.as_raw(), mass, collision);
        }
    }

    fn set_destroy_callback<F>(&self, callback: F)
        where F: FnMut(Body) + Send + 'static
    {
        lock!(self, write);
        unsafe {
            let mut udata: &mut Box<UserData> =
                mem::transmute(&mut ffi::NewtonBodyGetUserData(self.as_raw()));

            udata.destructor = Some(Box::new(callback));
        }
    }

    fn set_transform_callback<F>(&self, callback: F)
        where F: FnMut(Body, Mat4, usize) + Send + 'static
    {
        lock!(self, write);
        unsafe {
            let mut udata: &mut Box<UserData> =
                mem::transmute(&mut ffi::NewtonBodyGetUserData(self.as_raw()));
            udata.transform = Some(Box::new(callback));
            ffi::NewtonBodySetTransformCallback(self.as_raw(), Some(set_transform));
        }

        unsafe extern "C" fn set_transform(body: *const ffi::NewtonBody,
                                           matrix: *const f32,
                                           thread: std::os::raw::c_int) {
            let mut udata = ffi::NewtonBodyGetUserData(body);
            let udata: &mut Box<UserData> = mem::transmute(&mut udata);

            if let Some(callback) = &mut udata.transform {
                let matrix = mem::transmute::<_, &Mat4>(matrix).clone();
                callback(Body::from_raw(body, false), matrix, thread as _);
            }
        }
    }

    fn joints(&self) -> Joints {
        let joint = unsafe { ffi::NewtonBodyGetFirstJoint(self.as_raw()) };
        Joints { joint, body: self.as_raw(), _phantom: PhantomData }
    }

    fn set_force_and_torque_callback<F>(&self, callback: F)
        where F: FnMut(Body, Duration, usize) + Send + 'static
    {
        lock!(self, write);
        unsafe {
            let mut udata: &mut Box<UserData> =
                mem::transmute(&mut ffi::NewtonBodyGetUserData(self.as_raw()));
            udata.force_and_torque = Some(Box::new(callback));
            ffi::NewtonBodySetForceAndTorqueCallback(self.as_raw(), Some(force_and_torque));
        }

        unsafe extern "C" fn force_and_torque(body: *const ffi::NewtonBody,
                                              timestep: f32,
                                              thread: std::os::raw::c_int) {
            let seconds = timestep.floor() as u64;
            let nanos = (timestep.fract() * 1_000_000_000.0) as u32;
            let timestep = Duration::new(seconds, nanos);

            let mut udata = ffi::NewtonBodyGetUserData(body);
            let mut udata: &mut Box<UserData> = mem::transmute(&mut udata);
            if let Some(callback) = &mut udata.force_and_torque {
                let body = match mem::transmute::<_, Type>(ffi::NewtonBodyGetType(body)) {
                    Type::Dynamic => Body::Dynamic(DynamicBody { raw: body,
                                                                 _phantom: PhantomData,
                                                                 owned: false }),
                    Type::Kinematic => Body::Kinematic(KinematicBody { raw: body,
                                                                       _phantom: PhantomData,
                                                                       owned: false }),
                };
                callback(body, timestep, thread as usize);
            }
        }
    }
}