1use crate::b2_body::*;
2use crate::b2_broad_phase::*;
3use crate::b2_collision::*;
4use crate::b2_math::*;
5use crate::b2_common::*;
6use crate::b2rs_common::UserDataType;
7use crate::b2_settings::*;
8use crate::b2_shape::*;
9use crate::b2rs_linked_list::*;
10use crate::private::dynamics::b2_fixture as private;
11use std::cell::RefCell;
12use std::rc::{Rc, Weak};
13
14#[cfg(feature="serde_support")]
15use serde::{Serialize, Deserialize};
16
17impl Default for B2filter {
18 fn default() -> Self {
19 return B2filter {
20 category_bits: 0x0001,
21 mask_bits: 0xFFFF,
22 group_index: 0,
23 };
24 }
25}
26
27#[derive(Clone, Copy, Debug)]
29#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
30pub struct B2filter {
31 pub category_bits: u16,
33
34 pub mask_bits: u16,
37
38 pub group_index: i16,
42}
43
44impl<D: UserDataType> Default for B2fixtureDef<D> {
45 fn default() -> Self {
47 return B2fixtureDef {
48 shape: None,
49 user_data: None,
50 friction: 0.2,
51 restitution: 0.0,
52 restitution_threshold: 1.0 * B2_LENGTH_UNITS_PER_METER,
53 density: 0.0,
54 is_sensor: false,
55 filter: B2filter::default(),
56 };
57 }
58}
59
60#[derive(Clone)]
63pub struct B2fixtureDef<D: UserDataType> {
64 pub shape: Option<ShapeDefPtr>,
67
68 pub user_data: Option<D::Fixture>,
70
71 pub friction: f32,
73
74 pub restitution: f32,
76
77 pub restitution_threshold: f32,
80
81 pub density: f32,
83
84 pub is_sensor: bool,
87
88 pub filter: B2filter,
90}
91
92pub type FixturePtr<D> = Rc<RefCell<B2fixture<D>>>;
93pub type FixtureWeakPtr<D> = Weak<RefCell<B2fixture<D>>>;
94pub type FixtureProxyPtr<D> = Rc<RefCell<B2fixtureProxy<D>>>;
95
96#[derive(Default)]
98pub struct B2fixtureProxy<D: UserDataType> {
99 pub(crate) aabb: B2AABB,
100 pub(crate) fixture: Option<FixtureWeakPtr<D>>,
101 pub(crate) child_index: i32,
102 pub(crate) proxy_id: i32,
103}
104
105impl<D:UserDataType> LinkedListNode<B2fixture<D>> for B2fixture<D>
106{
107 fn get_next(&self) -> Option<FixturePtr<D>> {
108 return self.m_next.clone();
109 }
110 fn set_next(&mut self, value: Option<FixturePtr<D>>)
111 {
112 self.m_next = value;
113 }
114 fn take_next(&mut self) -> Option<FixturePtr<D>> {
115 return self.m_next.take();
116 }
117}
118
119#[derive(Default, Clone)]
127pub struct B2fixture<D: UserDataType> {
128 pub(crate) m_density: f32,
129
130 pub(crate) m_next: Option<FixturePtr<D>>,
131 pub(crate) m_body: Option<BodyWeakPtr<D>>,
132
133 pub(crate) m_shape: Option<ShapePtr>,
134
135 pub(crate) m_friction: f32,
136 pub(crate) m_restitution: f32,
137 pub(crate) m_restitution_threshold: f32,
138
139 pub(crate) m_proxies: Vec<FixtureProxyPtr<D>>,
140 pub(crate) m_proxy_count: i32,
141
142 pub(crate) m_filter: B2filter,
143
144 pub(crate) m_is_sensor: bool,
145
146 pub(crate) m_user_data: Option<D::Fixture>,
147}
148
149impl<D: UserDataType> B2fixture<D> {
150 pub fn get_type(&self) -> B2ShapeType {
154 return inline::get_type(self);
155 }
156
157 pub fn get_shape(&self) -> ShapePtr {
161 return inline::get_shape(self);
162 }
163
164 pub fn set_sensor(&mut self, sensor: bool) {
166 private::b2_fixture_set_sensor(self, sensor);
167 }
168
169 pub fn is_sensor(&self) -> bool {
173 return inline::is_sensor(self);
174 }
175
176 pub fn set_filter_data(&mut self, filter: B2filter) {
180 private::b2_fixture_set_filter_data(self, filter);
181 }
182
183 pub fn get_filter_data(&self) -> B2filter {
185 return inline::get_filter_data(self);
186 }
187
188 pub fn refilter(&mut self) {
190 private::b2_fixture_refilter(self);
191 }
192
193 pub fn get_body(&self) -> BodyPtr<D> {
197 return inline::get_body(self);
198 }
199
200 pub fn get_next(&self) -> Option<FixturePtr<D>> {
204 return inline::get_next(self);
205 }
206
207 pub fn get_user_data(&self) -> Option<D::Fixture> {
210 return inline::get_user_data(self);
211 }
212
213 pub fn set_user_data(&mut self, data: &D::Fixture) {
215 inline::set_user_data(self, data);
216 }
217
218 pub fn test_point(&self, p: B2vec2) -> bool {
221 return inline::test_point(self, p);
222 }
223
224 pub fn ray_cast(
229 &self,
230 output: &mut B2rayCastOutput,
231 input: &B2rayCastInput,
232 child_index: i32,
233 ) -> bool {
234 return inline::ray_cast(self, output, input, child_index);
235 }
236
237 pub fn get_mass_data(&self, mass_data: &mut B2massData) {
241 inline::get_mass_data(self, mass_data);
242 }
243
244 pub fn set_density(&mut self, density: f32) {
247 inline::set_density(self, density);
248 }
249
250 pub fn get_density(&self) -> f32 {
252 return inline::get_density(self);
253 }
254
255 pub fn get_friction(&self) -> f32 {
257 return inline::get_friction(self);
258 }
259
260 pub fn set_friction(&mut self, friction: f32) {
263 return inline::set_friction(self, friction);
264 }
265
266 pub fn get_restitution(&self) -> f32 {
268 return inline::get_restitution(self);
269 }
270
271 pub fn set_restitution(&mut self, restitution: f32) {
274 inline::set_restitution(self, restitution);
275 }
276
277 pub fn get_restitution_threshold(&self)-> f32{
279 return inline::get_restitution_threshold(self);
280 }
281
282 pub fn set_restitution_threshold(&mut self,threshold:f32)
285 {
286 inline::set_restitution_threshold(self, threshold)
287 }
288
289 pub fn get_aabb(&self, child_index: i32) -> B2AABB {
293 return inline::get_aabb(self, child_index);
294 }
295
296 pub(crate) fn default() -> Self {
297 return private::b2_fixture_default();
298 }
299
300 pub(crate) fn create(
303 self_: &mut B2fixture<D>,
304 body: BodyPtr<D>,
305 def: &B2fixtureDef<D>,
306 ) {
307 private::b2_fixture_create(self_, body, def);
308 }
309
310 pub(crate) fn create_proxies(
312 self_ptr: FixturePtr<D>,
313 broad_phase: &mut B2broadPhase<FixtureProxyPtr<D>>,
314 xf: &B2Transform,
315 ) {
316 private::b2_fixture_create_proxies(self_ptr, broad_phase, xf);
317 }
318 pub(crate) fn destroy_proxies(
319 &mut self,
320 broad_phase: &mut B2broadPhase<FixtureProxyPtr<D>>,
321 ) {
322 private::b2_fixture_destroy_proxies(self, broad_phase);
323 }
324
325 pub(crate) fn synchronize(
326 &mut self,
327 broad_phase: &mut B2broadPhase<FixtureProxyPtr<D>>,
328 xf1: B2Transform,
329 xf2: B2Transform,
330 ) {
331 private::b2_fixture_synchronize(self, broad_phase, xf1, xf2);
332 }
333}
334
335mod inline {
336 use super::*;
337
338 pub fn get_type<T: UserDataType>(self_: &B2fixture<T>) -> B2ShapeType {
339 return self_.m_shape.as_ref().unwrap().get_type();
340 }
341
342 pub fn get_shape<T: UserDataType>(self_: &B2fixture<T>) -> ShapePtr {
343 return self_.m_shape.as_ref().unwrap().clone();
344 }
345
346 pub fn is_sensor<T: UserDataType>(self_: &B2fixture<T>) -> bool {
347 return self_.m_is_sensor;
348 }
349
350 pub fn get_filter_data<T: UserDataType>(self_: &B2fixture<T>) -> B2filter {
351 return self_.m_filter;
352 }
353
354 pub fn get_user_data<D: UserDataType>(self_: &B2fixture<D>) -> Option<D::Fixture> {
355 return self_.m_user_data.clone();
356 }
357
358 pub fn set_user_data<D: UserDataType>(self_: &mut B2fixture<D>, data: &D::Fixture) {
359 self_.m_user_data = Some(data.clone());
360 }
361
362 pub fn get_body<T: UserDataType>(self_: &B2fixture<T>) -> BodyPtr<T> {
363 return self_.m_body.as_ref().unwrap().upgrade().unwrap();
364 }
365
366 pub fn get_next<T: UserDataType>(self_: &B2fixture<T>) -> Option<FixturePtr<T>> {
367 return self_.m_next.clone();
368 }
369
370 pub fn set_density<T: UserDataType>(self_: &mut B2fixture<T>, density: f32) {
371 b2_assert(b2_is_valid(density) && density >= 0.0);
372 self_.m_density = density;
373 }
374
375 pub fn get_density<T: UserDataType>(self_: &B2fixture<T>) -> f32 {
376 return self_.m_density;
377 }
378
379 pub fn get_friction<T: UserDataType>(self_: &B2fixture<T>) -> f32 {
380 return self_.m_friction;
381 }
382
383 pub fn set_friction<T: UserDataType>(self_: &mut B2fixture<T>, friction: f32) {
384 self_.m_friction = friction;
385 }
386
387 pub fn get_restitution<T: UserDataType>(self_: &B2fixture<T>) -> f32 {
388 return self_.m_restitution;
389 }
390
391 pub fn set_restitution<T: UserDataType>(self_: &mut B2fixture<T>, restitution: f32) {
392 self_.m_restitution = restitution;
393 }
394
395 pub fn get_restitution_threshold<T: UserDataType>(self_: &B2fixture<T>) ->f32
396 {
397 return self_.m_restitution_threshold;
398 }
399
400 pub fn set_restitution_threshold<T: UserDataType>(self_: &mut B2fixture<T>, threshold:f32)
401 {
402 self_.m_restitution_threshold = threshold;
403 }
404
405 pub fn test_point<T: UserDataType>(self_: &B2fixture<T>, p: B2vec2) -> bool {
406 return self_.m_shape.as_ref().unwrap().test_point(
407 self_.m_body
408 .as_ref()
409 .unwrap()
410 .upgrade()
411 .unwrap()
412 .borrow()
413 .get_transform(),
414 p,
415 );
416 }
417
418 pub fn ray_cast<T: UserDataType>(
419 self_: &B2fixture<T>,
420 output: &mut B2rayCastOutput,
421 input: &B2rayCastInput,
422 child_index: i32,
423 ) -> bool {
424 return self_.m_shape.as_ref().unwrap().ray_cast(
425 output,
426 input,
427 self_.m_body
428 .as_ref()
429 .unwrap()
430 .upgrade()
431 .unwrap()
432 .borrow()
433 .get_transform(),
434 child_index as usize,
435 );
436 }
437
438 pub fn get_mass_data<T: UserDataType>(self_: &B2fixture<T>, mass_data: &mut B2massData) {
439 self_.m_shape
440 .as_ref()
441 .unwrap()
442 .compute_mass(mass_data, self_.m_density);
443 }
444
445 pub fn get_aabb<T: UserDataType>(self_: &B2fixture<T>, child_index: i32) -> B2AABB {
446 b2_assert(0 <= child_index && child_index < self_.m_proxies.len() as i32);
447 return self_.m_proxies[child_index as usize].as_ref().borrow().aabb;
448 }
449}