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 libc::size_t;
use std::mem::transmute;
use super::fixture::*;
use super::super::collision::shapes::shape::*;
use super::super::common::math::*;
use super::super::common::settings::*;
use super::world::*;
#[repr(C)]
#[derive(Debug)]
pub enum BodyType {
StaticBody = 0,
KinematicBody,
DynamicBody
}
#[repr(C)]
#[derive(Debug)]
pub struct BodyDef {
pub body_type: BodyType,
pub position: Vec2,
pub angle: Float32,
pub linear_velocity: Vec2,
pub angular_velocity: Float32,
pub linear_damping: Float32,
pub angular_damping: Float32,
pub allow_sleep: bool,
pub awake: bool,
pub fixed_rotation: bool,
pub bullet: bool,
pub active: bool,
pub user_data: size_t,
pub gravity_scale : Float32,
}
impl Default for BodyDef {
fn default () -> BodyDef {
BodyDef {
user_data: 0,
position: Vec2::default(),
angle: 0.0,
linear_velocity: Vec2::default(),
angular_velocity: 0.0,
linear_damping: 0.0,
angular_damping: 0.0,
allow_sleep: true,
awake: true,
fixed_rotation: false,
bullet: false,
body_type: BodyType::StaticBody,
active: true,
gravity_scale: 1.0,
}
}
}
pub enum B2Body {}
extern {
fn b2Body_CreateFixture_FromShape(this: *mut B2Body, shape: *const B2Shape, density: Float32) -> *mut B2Fixture;
fn b2Body_CreateFixture(this: *mut B2Body, def: *mut FixtureDef) -> *mut B2Fixture;
fn b2Body_GetAngle(this: *mut B2Body) -> Float32;
fn b2Body_GetFixtureList(this: *mut B2Body) -> *mut B2Fixture;
fn b2Body_GetNext(this: *mut B2Body) -> *mut B2Body;
fn b2Body_GetPosition(this: *mut B2Body) -> &Vec2;
fn b2Body_GetUserData(this: *const B2Body) -> usize;
fn b2Body_GetWorld(this: *const B2Body) -> *mut B2World;
fn b2Body_GetLocalPoint(this: *const B2Body, worldPoint: &Vec2) -> Vec2;
}
#[allow(raw_pointer_derive)]
#[derive(Clone, Debug)]
pub struct Body {
pub ptr: *mut B2Body
}
impl Body {
pub fn create_fixture(&self, def: &FixtureDef) -> Fixture {
unsafe {
Fixture { ptr: b2Body_CreateFixture(self.ptr, transmute(def)) }
}
}
pub fn create_fixture_from_shape(&self, shape: &Shape, density: f32) -> Fixture {
unsafe {
Fixture { ptr: b2Body_CreateFixture_FromShape(self.ptr, shape.handle(), density) }
}
}
pub fn get_angle(&self) -> f32 {
unsafe {
b2Body_GetAngle(self.ptr)
}
}
pub fn get_fixture_list(&self) -> Option<Fixture> {
let ptr;
unsafe {
ptr = b2Body_GetFixtureList(self.ptr);
}
if ptr.is_null() {
None
} else {
Some(Fixture { ptr: ptr })
}
}
pub fn get_next(&self) -> Option<Body> {
let ptr: *mut B2Body;
unsafe {
ptr = b2Body_GetNext(self.ptr);
}
if ptr.is_null() {
None
} else {
Some(Body { ptr: ptr })
}
}
pub fn get_position(&self) -> &Vec2 {
unsafe {
b2Body_GetPosition(self.ptr)
}
}
pub fn get_user_data(&self) -> usize {
unsafe {
b2Body_GetUserData(self.ptr)
}
}
pub fn get_world(&self) -> World {
unsafe {
World { ptr: b2Body_GetWorld(self.ptr) }
}
}
pub fn get_local_point(&self, world_point: &Vec2) -> Vec2 {
unsafe {
b2Body_GetLocalPoint(self.ptr, world_point)
}
}
}