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
use wrap::*;
use common::math::Vec2;
use user_data::UserDataTypes;
use dynamics::world::{World, BodyHandle};
use dynamics::joints::{Joint, JointType, JointDef};

pub struct RevoluteJointDef {
    pub body_a: BodyHandle,
    pub body_b: BodyHandle,
    pub collide_connected: bool,
    pub local_anchor_a: Vec2,
    pub local_anchor_b: Vec2,
    pub reference_angle: f32,
    pub enable_limit: bool,
    pub lower_angle: f32,
    pub upper_angle: f32,
    pub enable_motor: bool,
    pub motor_speed: f32,
    pub max_motor_torque: f32,
}

impl RevoluteJointDef {
    pub fn new(body_a: BodyHandle, body_b: BodyHandle) -> RevoluteJointDef {
        RevoluteJointDef {
            body_a: body_a,
            body_b: body_b,
            collide_connected: false,
            local_anchor_a: Vec2 { x: 0., y: 0. },
            local_anchor_b: Vec2 { x: 0., y: 0. },
            reference_angle: 0.,
            enable_limit: false,
            lower_angle: 0.,
            upper_angle: 0.,
            enable_motor: false,
            motor_speed: 0.,
            max_motor_torque: 0.,
        }
    }

    pub fn init<U: UserDataTypes>(&mut self,
                                  world: &World<U>,
                                  body_a: BodyHandle,
                                  body_b: BodyHandle,
                                  anchor: &Vec2) {
        self.try_init(world, body_a, body_b, anchor).expect("joint init filed: invalid body handle");
    }

    pub fn try_init<U: UserDataTypes>(&mut self,
                                      world: &World<U>,
                                      body_a: BodyHandle,
                                      body_b: BodyHandle,
                                      anchor: &Vec2) -> Option<()> {
        self.body_a = body_a;
        self.body_b = body_b;
        let a = world.try_body(body_a)?;
        let b = world.try_body(body_b)?;
        self.local_anchor_a = a.local_point(anchor);
        self.local_anchor_b = b.local_point(anchor);
        self.reference_angle = b.angle() - a.angle();
        Some(())
    }
}

impl JointDef for RevoluteJointDef {
    fn joint_type() -> JointType
        where Self: Sized
    {
        JointType::Revolute
    }

    unsafe fn create<U: UserDataTypes>(&self, world: &mut World<U>) -> *mut ffi::Joint {
        self.try_create(world).expect("joint create failed: invalid body handle")
    }

    unsafe fn try_create<U: UserDataTypes>(&self, world: &mut World<U>) -> Option<*mut ffi::Joint> {
        Some(ffi::World_create_revolute_joint(world.mut_ptr(),
                                              world.try_body_mut(self.body_a)?.mut_ptr(),
                                              world.try_body_mut(self.body_b)?.mut_ptr(),
                                              self.collide_connected,
                                              self.local_anchor_a,
                                              self.local_anchor_b,
                                              self.reference_angle,
                                              self.enable_limit,
                                              self.lower_angle,
                                              self.upper_angle,
                                              self.enable_motor,
                                              self.motor_speed,
                                              self.max_motor_torque))
    }
}

wrap_joint! {
    ffi::RevoluteJoint => RevoluteJoint (JointType::Revolute)
    < ffi::RevoluteJoint_as_joint
    > ffi::Joint_as_revolute_joint
}

impl RevoluteJoint {
    pub fn local_anchor_a<'a>(&'a self) -> &'a Vec2 {
        unsafe {
            &*ffi::RevoluteJoint_get_local_anchor_a(self.ptr()) // Comes from a C++ &
        }
    }

    pub fn local_anchor_b<'a>(&'a self) -> &'a Vec2 {
        unsafe {
            &*ffi::RevoluteJoint_get_local_anchor_b(self.ptr()) // Comes from a C++ &
        }
    }

    pub fn reference_angle(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_reference_angle(self.ptr()) }
    }

    pub fn joint_angle(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_joint_angle(self.ptr()) }
    }

    pub fn joint_speed(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_joint_speed(self.ptr()) }
    }

    pub fn is_limit_enabled(&self) -> bool {
        unsafe { ffi::RevoluteJoint_is_limit_enabled(self.ptr()) }
    }

    pub fn lower_limit(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_lower_limit(self.ptr()) }
    }

    pub fn upper_limit(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_upper_limit(self.ptr()) }
    }

    pub fn is_motor_enabled(&self) -> bool {
        unsafe { ffi::RevoluteJoint_is_motor_enabled(self.ptr()) }
    }

    pub fn motor_speed(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_motor_speed(self.ptr()) }
    }

    pub fn max_motor_torque(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_max_motor_torque(self.ptr()) }
    }

    pub fn motor_torque(&self) -> f32 {
        unsafe { ffi::RevoluteJoint_get_motor_torque(self.ptr()) }
    }

    pub fn enable_limit(&mut self, flag: bool) {
        unsafe { ffi::RevoluteJoint_enable_limit(self.mut_ptr(), flag) }
    }

    pub fn set_limits(&mut self, lower: f32, upper: f32) {
        unsafe { ffi::RevoluteJoint_set_limits(self.mut_ptr(), lower, upper) }
    }

    pub fn enable_motor(&mut self, flag: bool) {
        unsafe { ffi::RevoluteJoint_enable_motor(self.mut_ptr(), flag) }
    }

    pub fn set_motor_speed(&mut self, speed: f32) {
        unsafe { ffi::RevoluteJoint_set_motor_speed(self.mut_ptr(), speed) }
    }

    pub fn set_max_motor_torque(&mut self, torque: f32) {
        unsafe { ffi::RevoluteJoint_set_max_motor_torque(self.mut_ptr(), torque) }
    }
}

#[doc(hidden)]
pub mod ffi {
    pub use dynamics::world::ffi::World;
    pub use dynamics::body::ffi::Body;
    pub use dynamics::joints::ffi::Joint;
    use common::math::Vec2;

    pub enum RevoluteJoint {}

    extern "C" {
        pub fn World_create_revolute_joint(world: *mut World,
                                           body_a: *mut Body,
                                           body_b: *mut Body,
                                           collide_connected: bool,
                                           local_anchor_a: Vec2,
                                           local_anchor_b: Vec2,
                                           reference_angle: f32,
                                           enable_limit: bool,
                                           lower_angle: f32,
                                           upper_angle: f32,
                                           enable_motor: bool,
                                           motor_speed: f32,
                                           max_motor_torque: f32)
                                           -> *mut Joint;
        pub fn RevoluteJoint_as_joint(slf: *mut RevoluteJoint) -> *mut Joint;
        pub fn Joint_as_revolute_joint(slf: *mut Joint) -> *mut RevoluteJoint;
        pub fn RevoluteJoint_get_local_anchor_a(slf: *const RevoluteJoint) -> *const Vec2;
        pub fn RevoluteJoint_get_local_anchor_b(slf: *const RevoluteJoint) -> *const Vec2;
        pub fn RevoluteJoint_get_reference_angle(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_get_joint_angle(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_get_joint_speed(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_is_limit_enabled(slf: *const RevoluteJoint) -> bool;
        pub fn RevoluteJoint_enable_limit(slf: *mut RevoluteJoint, flag: bool);
        pub fn RevoluteJoint_get_lower_limit(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_get_upper_limit(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_set_limits(slf: *mut RevoluteJoint, lower: f32, upper: f32);
        pub fn RevoluteJoint_is_motor_enabled(slf: *const RevoluteJoint) -> bool;
        pub fn RevoluteJoint_enable_motor(slf: *mut RevoluteJoint, flag: bool);
        pub fn RevoluteJoint_set_motor_speed(slf: *mut RevoluteJoint, speed: f32);
        pub fn RevoluteJoint_get_motor_speed(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_set_max_motor_torque(slf: *mut RevoluteJoint, torque: f32);
        pub fn RevoluteJoint_get_max_motor_torque(slf: *const RevoluteJoint) -> f32;
        pub fn RevoluteJoint_get_motor_torque(slf: *const RevoluteJoint) -> f32;
    }
}