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
use crate::b2_collision::*;
use crate::b2_dynamic_tree::*;
use crate::b2_math::*;
use crate::private::collision::b2_broad_phase as private;

use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default, Clone, Copy, Debug)]
pub struct B2pair {
	pub proxy_id_a: i32,
	pub proxy_id_b: i32,
}

pub type B2broadPhasePtr<UserDataType> = Rc<RefCell<B2broadPhase<UserDataType>>>;

/// The broad-phase is used for computing pairs and performing volume queries and ray casts.
/// This broad-phase does not persist pairs. Instead, this reports potentially new pairs.
/// It is up to the client to consume the new pairs and to track subsequent overlap.
#[derive(Debug)]
pub struct B2broadPhase<UserDataType> {
	pub(crate) m_tree: B2dynamicTree<UserDataType>,

	pub(crate) m_proxy_count: i32,

	pub(crate) m_move_buffer: Vec<i32>,
	pub(crate) m_move_capacity: i32,
	pub(crate) m_move_count: i32,

	pub(crate) pairs: B2broadPhasePairs,
}

//because of &mut
#[derive(Debug, Default)]
pub struct B2broadPhasePairs {
	pub(crate) m_pair_buffer: Vec<B2pair>,
	pub(crate) m_pair_capacity: i32,
	pub(crate) m_pair_count: i32,
}

pub const E_NULL_PROXY: i32 = -1;

impl<UserDataType: Default + Clone> B2broadPhase<UserDataType> {
	pub fn new() -> Self {
		return private::b2_broad_phase_b2_broad_phase();
	}

	//~B2broadPhase();

	/// create a proxy with an initial AABB. Pairs are not reported until
	/// UpdatePairs is called.
	pub fn create_proxy(&mut self, aabb: B2AABB, user_data: &UserDataType) -> i32 {
		return private::b2_broad_phase_create_proxy(self, aabb, user_data);
	}

	/// destroy a proxy. It is up to the client to remove any pairs.
	pub fn destroy_proxy(&mut self, proxy_id: i32) {
		private::b2_broad_phase_destroy_proxy(self, proxy_id);
	}

	/// Call move_proxy as many times as you like, then when you are done
	/// call UpdatePairs to finalized the proxy pairs (for your time step).
	pub fn move_proxy(&mut self, proxy_id: i32, aabb: B2AABB, displacement: B2vec2) {
		private::b2_broad_phase_move_proxy(self, proxy_id, aabb, displacement);
	}

	/// Call to trigger a re-processing of it's pairs on the next call to UpdatePairs.
	pub fn touch_proxy(&mut self, proxy_id: i32) {
		private::b2_broad_phase_touch_proxy(self, proxy_id);
	}

	/// Get the fat AABB for a proxy.
	pub fn get_fat_aabb(&self, proxy_id: i32) -> B2AABB {
		return inline::get_fat_aabb(self, proxy_id);
	}

	/// Get user data from a proxy. Returns None if the id is invalid.
	pub fn get_user_data(&self, proxy_id: i32) -> Option<UserDataType> {
		return inline::get_user_data(self, proxy_id);
	}

	/// Test overlap of fat AABBs.
	pub fn test_overlap(&self, proxy_id_a: i32, proxy_id_b: i32) -> bool {
		return inline::test_overlap(self, proxy_id_a, proxy_id_b);
	}

	/// Get the number of proxies.
	pub fn get_proxy_count(&self) -> i32 {
		return inline::get_proxy_count(self);
	}

	/// update the pairs. This results in pair callbacks. This can only add pairs.
	pub fn update_pairs<CallbackType: AddPairTrait<UserDataType>>(
		&mut self,
		callback: &mut CallbackType,
	) {
		inline::update_pairs(self, callback);
	}

	/// query an AABB for overlapping proxies. The callback class
	/// is called for each proxy that overlaps the supplied AABB.
	pub fn query<F:  QueryCallback>(&self, callback: F, aabb: B2AABB) {
		inline::query(self, callback, aabb);
	}

	/// Ray-cast against the proxies in the tree. This relies on the callback
	/// to perform a exact ray-cast in the case were the proxy contains a shape.
	/// The callback also performs the any collision filtering. This has performance
	/// roughly equal to k * log(n), where k is the number of collisions and n is the
	/// number of proxies in the tree.
	/// * `input` - the ray-cast input data. The ray extends from p1 to p1 + max_fraction * (p2 - p1).
	/// * `callback` - a callback class that is called for each proxy that is hit by the ray.
	pub fn ray_cast<T: RayCastCallback>(&self, callback: T, input: &B2rayCastInput) {
		inline::ray_cast(self, callback, input);
	}

	/// Get the height of the embedded tree.
	pub fn get_tree_height(&self) -> i32 {
		return inline::get_tree_height(self);
	}

	/// Get the balance of the embedded tree.
	pub fn get_tree_balance(&self) -> i32 {
		return inline::get_tree_balance(self);
	}

	/// Get the quality metric of the embedded tree.
	pub fn get_tree_quality(&self) -> f32 {
		return inline::get_tree_quality(self);
	}

	/// Shift the world origin. Useful for large worlds.
	/// The shift formula is: position -= new_origin
	/// * `new_origin` - the new origin with respect to the old origin
	pub fn shift_origin(&mut self, new_origin: B2vec2) {
		inline::shift_origin(self, new_origin);
	}

	pub fn get_tree_mut(&mut self)->&mut B2dynamicTree<UserDataType>{
		return &mut self.m_tree;
	}

	pub(crate) fn buffer_move(&mut self, proxy_id: i32) {
		private::b2_broad_phase_buffer_move(self, proxy_id);
	}

	pub(crate) fn un_buffer_move(&mut self, proxy_id: i32) {
		private::b2_broad_phase_un_buffer_move(self, proxy_id);
	}
}

pub trait AddPairTrait<UserDataType> {
	fn add_pair(
		&mut self,
		proxy_user_data_a: Option<UserDataType>,
		proxy_user_data_b: Option<UserDataType>,
	);
}

mod inline {
	use super::*;

	pub fn get_user_data<UserDataType: Default + Clone>(
		this: &B2broadPhase<UserDataType>,
		proxy_id: i32,
	) -> Option<UserDataType> {
		return this.m_tree.get_user_data(proxy_id);
	}

	pub fn test_overlap<T: Default + Clone>(
		this: &B2broadPhase<T>,
		proxy_id_a: i32,
		proxy_id_b: i32,
	) -> bool {
		let aabb_a: B2AABB = this.m_tree.get_fat_aabb(proxy_id_a);
		let aabb_b: B2AABB = this.m_tree.get_fat_aabb(proxy_id_b);
		return b2_test_overlap(aabb_a, aabb_b);
	}

	pub fn get_fat_aabb<T: Default + Clone>(this: &B2broadPhase<T>, proxy_id: i32) -> B2AABB {
		return this.m_tree.get_fat_aabb(proxy_id);
	}

	pub fn get_proxy_count<T: Default + Clone>(this: &B2broadPhase<T>) -> i32 {
		return this.m_proxy_count;
	}

	pub fn get_tree_height<T: Default + Clone>(this: &B2broadPhase<T>) -> i32 {
		return this.m_tree.get_height();
	}

	pub fn get_tree_balance<T: Default + Clone>(this: &B2broadPhase<T>) -> i32 {
		return this.m_tree.get_max_balance();
	}

	pub fn get_tree_quality<T: Default + Clone>(this: &B2broadPhase<T>) -> f32 {
		return this.m_tree.get_area_ration();
	}

	pub fn update_pairs<T: Default + Clone, CallbackType: AddPairTrait<T>>(
		this: &mut B2broadPhase<T>,
		callback: &mut CallbackType,
	) {
		// reset pair buffer
		this.pairs.m_pair_count=0;

		// Perform tree queries for all moving proxies.
		for i in 0..this.m_move_count {
			let m_query_proxy_id = this.m_move_buffer[i as usize];
			if m_query_proxy_id == E_NULL_PROXY {
				continue;
			}

			// We have to query the tree with the fat AABB so that
			// we don't fail to create a pair that may touch later.
			let fat_aabb: B2AABB = this.m_tree.get_fat_aabb(m_query_proxy_id);
			{
				let pairs = &mut this.pairs;
				let tree = &this.m_tree;
				// query tree, create pairs and add them pair buffer.
				tree.query(|proxy_id:i32|->bool{
					let moved = tree.was_moved(proxy_id);
					return private::b2_broad_phase_query_callback(pairs, m_query_proxy_id, proxy_id, moved);
				}, fat_aabb);
			}
		}

		// Send pairs to caller
		for i in 0..this.pairs.m_pair_count {
			let primary_pair: B2pair = this.pairs.m_pair_buffer[i as usize];
			let user_data_a = this.m_tree.get_user_data(primary_pair.proxy_id_a);
			let user_data_b = this.m_tree.get_user_data(primary_pair.proxy_id_b);

			callback.add_pair(user_data_a, user_data_b);
		}

		// clear move flags
		for i in 0..this.m_move_count {
			let proxy_id: i32 = this.m_move_buffer[i as usize];
			if proxy_id == E_NULL_PROXY {
				continue;
			}

			this.m_tree.clear_moved(proxy_id);
		}

		// reset move buffer
		this.m_move_count = 0;
	}

	pub fn query<UserDataType: Default + Clone, F:  QueryCallback>(
		this: &B2broadPhase<UserDataType>,
		callback: F,
		aabb: B2AABB,
	) {
		this.m_tree.query(callback, aabb);
	}

	pub fn ray_cast<UserDataType: Default + Clone, T: RayCastCallback>(
		this: &B2broadPhase<UserDataType>,
		callback: T,
		input: &B2rayCastInput,
	) {
		this.m_tree.ray_cast(callback, input);
	}

	pub fn shift_origin<UserDataType: Default + Clone>(
		this: &mut B2broadPhase<UserDataType>,
		new_origin: B2vec2,
	) {
		this.m_tree.shift_origin(new_origin);
	}
}