box2d_rs/
b2_world_callbacks.rs

1use crate::b2_collision::*;
2use crate::b2_contact::*;
3use crate::b2_fixture::*;
4use crate::b2_joint::*;
5use crate::b2_math::*;
6use crate::b2_common::*;
7use crate::b2rs_common::*;
8
9use std::cell::RefCell;
10use std::rc::Rc;
11
12use crate::private::dynamics::b2_world_callbacks as private;
13
14pub type B2destructionListenerPtr<D> = Rc<RefCell<dyn B2destructionListener<D>>>;
15pub type B2contactFilterPtr<D> = Rc<RefCell<dyn B2contactFilter<D>>>;
16pub type B2contactListenerPtr<D> = Rc<RefCell<dyn B2contactListener<D>>>;
17
18/// Joints and fixtures are destroyed when their associated
19/// body is destroyed. Implement this listener so that you
20/// may nullify references to these joints and shapes.
21pub trait B2destructionListener<D: UserDataType> {
22	/// Called when any joint is about to be destroyed due
23	/// to the destruction of one of its attached bodies.
24	fn say_goodbye_joint(&mut self, joint: B2jointPtr<D>);
25
26	/// Called when any fixture is about to be destroyed due
27	/// to the destruction of its parent body.
28	fn say_goodbye_fixture(&mut self, fixture: FixturePtr<D>);
29}
30
31/// Implement this class to provide collision filtering. In other words, you can implement
32/// this class if you want finer control over contact creation.
33pub trait B2contactFilter<D: UserDataType> {
34	/// Return true if contact calculations should be performed between these two shapes.
35	/// <p style="background:rgba(255,181,77,0.16);padding:0.75em;">
36	/// <strong>Warning:</strong> for performance reasons this is only called when the AABBs begin to overlap.
37	/// </p>
38	fn should_collide(&self, fixture_a: FixturePtr<D>, fixture_b: FixturePtr<D>) -> bool {
39		return private::should_collide(fixture_a, fixture_b);
40	}
41}
42
43pub struct B2contactFilterDefault;
44
45impl<D: UserDataType> B2contactFilter<D> for B2contactFilterDefault {}
46
47/// Contact impulses for reporting. Impulses are used instead of forces because
48/// sub-step forces may approach infinity for rigid body collisions. These
49/// match up one-to-one with the contact points in B2manifold.
50#[derive(Default, Copy, Clone, Debug)]
51pub struct B2contactImpulse {
52	pub normal_impulses: [f32; B2_MAX_MANIFOLD_POINTS],
53	pub tangent_impulses: [f32; B2_MAX_MANIFOLD_POINTS],
54	pub count: i32,
55}
56
57/// Implement this class to get contact information. You can use these results for
58/// things like sounds and game logic. You can also get contact results by
59/// traversing the contact lists after the time step. However, you might miss
60/// some contacts because continuous physics leads to sub-stepping.
61/// Additionally you may receive multiple callbacks for the same contact in a
62/// single time step.
63/// You should strive to make your callbacks efficient because there may be
64/// many callbacks per time step.
65/// <p style="background:rgba(255,181,77,0.16);padding:0.75em;">
66/// <strong>Warning:</strong> You cannot create/destroy Box2D entities inside these callbacks.
67/// </p>
68pub trait B2contactListener<D: UserDataType> {
69	/// Called when two fixtures begin to touch.
70	fn begin_contact(&mut self, contact: &mut dyn B2contactDynTrait<D>) {
71		b2_not_used(contact);
72	}
73
74	/// Called when two fixtures cease to touch.
75	fn end_contact(&mut self, contact: &mut dyn B2contactDynTrait<D>) {
76		b2_not_used(contact);
77	}
78
79	/// This is called after a contact is updated. This allows you to inspect a
80	/// contact before it goes to the solver. If you are careful, you can modify the
81	/// contact manifold (e.g. disable contact).
82	/// A copy of the old manifold is provided so that you can detect changes.
83	/// Note: this is called only for awake bodies.
84	/// Note: this is called even when the number of contact points is zero.
85	/// Note: this is not called for sensors.
86	/// Note: if you set the number of contact points to zero, you will not
87	/// get an end_contact callback. However, you may get a begin_contact callback
88	/// the next step.
89	fn pre_solve(&mut self, contact: &mut dyn B2contactDynTrait<D>, old_manifold: &B2manifold) {
90		b2_not_used(contact);
91		b2_not_used(old_manifold);
92	}
93
94	/// This lets you inspect a contact after the solver is finished. This is useful
95	/// for inspecting impulses.
96	/// Note: the contact manifold does not include time of impact impulses, which can be
97	/// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
98	/// in a separate data structure.
99	/// Note: this is only called for contacts that are touching, solid, and awake.
100	fn post_solve(&mut self, contact: &mut dyn B2contactDynTrait<D>, impulse: &B2contactImpulse) {
101		b2_not_used(contact);
102		b2_not_used(impulse);
103	}
104}
105
106pub struct B2contactListenerDefault;
107
108impl<D: UserDataType> B2contactListener<D> for B2contactListenerDefault {}
109
110/// Called for each fixture found in the query AABB.
111/// 
112/// @return false to terminate the query.
113pub trait B2queryCallback<D: UserDataType>: FnMut(
114	/*fixture:*/ FixturePtr<D>
115) -> bool {}
116impl<F, D: UserDataType> B2queryCallback<D> for F where F: FnMut(FixturePtr<D>) -> bool {}
117
118/// Called for each fixture found in the query. You control how the ray cast
119/// proceeds by returning a f32:
120/// return -1: ignore this fixture and continue
121/// return 0: terminate the ray cast
122/// return fraction: clip the ray to this point
123/// return 1: don't clip the ray and continue
124/// * `fixture` - the fixture hit by the ray
125/// * `point` - the point of initial intersection
126/// * `normal` - the normal vector at the point of intersection
127/// * `fraction` - the fraction along the ray at the point of intersection
128/// 
129/// @return -1 to filter, 0 to terminate, fraction to clip the ray for
130/// closest hit, 1 to continue
131pub trait B2rayCastCallback<D:UserDataType>: FnMut(	
132	/*fixture:*/ FixturePtr<D>,
133	/*point:*/ B2vec2,
134	/*normal:*/ B2vec2,
135	/*fraction:*/ f32) -> f32 {}
136
137impl<F, D: UserDataType> B2rayCastCallback<D> for F where
138	F: FnMut(FixturePtr<D>, B2vec2, B2vec2, f32) -> f32
139{}