bevy_flowfield_tiles_plugin 0.15.0

An implementation of FlowField (vector field) pathfinding as a plugin to the Bevy game engine
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! Defines the world size
//!

use bevy::prelude::*;

use crate::flowfields::utilities::{CompassDir, FIELD_RESOLUTION};
use crate::flowfields::{fields::FieldCell, sectors::SectorID};

/// The dimensions and scaling of the world
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Default, Clone, Copy, Reflect)]
pub struct Dimensions {
	/// The origin point of your game world. This is used to translate a point from Bevy global space into a [SectorID] and [FieldCell], and back
	///
	/// ## In 3d
	///
	/// This is taken as an `(x, z)` point in space
	///
	/// ## In 2d
	///
	/// This is taken as an `(x, y)` point in space
	origin: (f32, f32),
	/// Dimensions of the world
	///
	/// ## In 3d
	///
	/// This is taken as `(x, z)` length of the world, imagine a birds eye view of a world
	///
	/// ## In 2d
	///
	/// This is taken as the `(x, y)` length of the world
	size: (f32, f32),
	/// A unit of space, this forms the basis of the dimensions of a [FieldCell]
	/// and influences the number of sectors computed
	world_unit_size: f32,
	/// Actor size influences the expansion of
	/// [crate::flowfields::fields::cost_field::CostField] impassable cells to
	/// ensure that Actors avoid trying to path through small gaps which they
	/// can't fit through - hence an alternative route will be explored to go
	/// around small gaps
	actor_scale: u32,
}

impl Dimensions {
	/// Create a new instance of [Dimensions].
	pub fn new(
		origin: (f32, f32),
		size: (f32, f32),
		world_unit_size: f32,
		actor_radius: f32,
	) -> Self {
		if size.0 <= 0.0 || size.1 <= 0.0 {
			panic!(
				"Size must be greater than 0.0, found size({}, {})",
				size.0, size.1
			)
		}
		if actor_radius <= 0.0 {
			panic!("Actor size cannot be less than zero");
		}
		if world_unit_size <= 0.0 {
			panic!("World unit size must be more than zero")
		}
		let sector_len = world_unit_size * FIELD_RESOLUTION as f32;
		let sector_columns = size.0 / sector_len;
		let sector_rows = size.1 / sector_len;
		if sector_columns < 1.0 || sector_rows < 1.0 {
			panic!(
				"`world_unit_size * {}` must be an exact factor of `size`",
				FIELD_RESOLUTION
			);
		}
		if actor_radius >= sector_len {
			panic!(
				"actor_radius cannot be bigger than the length of a sector. Sector length {}, actor size {}",
				sector_len, actor_radius
			);
		}
		let actor_scale = (actor_radius / world_unit_size).ceil() as u32;
		if actor_scale >= 10 {
			panic!("Actors cannot be larger than an entire sector");
		}
		Dimensions {
			origin,
			size,
			world_unit_size,
			actor_scale,
		}
	}
	/// Get the size tuple of the world
	pub fn get_size(&self) -> (f32, f32) {
		self.size
	}
	/// Number of `x` units in size
	pub fn get_length(&self) -> f32 {
		self.size.0
	}
	/// 2d: number of `y` units in size
	///
	/// 3d: number of `z` units in size
	pub fn get_depth(&self) -> f32 {
		self.size.1
	}
	pub fn get_unit_scale(&self) -> f32 {
		self.world_unit_size
	}
	pub fn get_actor_scale(&self) -> u32 {
		self.actor_scale
	}
	/// Based on `size` and unit size calculate the number of [`FieldCell`] columns across all sectors
	pub fn get_total_field_cell_columns(&self) -> usize {
		(self.get_length() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as usize
			* FIELD_RESOLUTION
	}
	/// Based on `size` and unit size calculate the number of [`FieldCell`] rows across all sectors
	pub fn get_total_field_cell_rows(&self) -> usize {
		(self.get_depth() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as usize
			* FIELD_RESOLUTION
	}
	/// Get the number of sector columns
	pub fn get_sector_column_count(&self) -> usize {
		(self.get_length() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as usize
	}
	/// Get the number of sector rows
	pub fn get_sector_row_count(&self) -> usize {
		(self.get_depth() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as usize
	}

	/// From a global position in 2D `x, y` calculate the sector ID that point resides in
	#[cfg(feature = "2d")]
	pub fn get_sector_id_from_xy(&self, position: Vec2) -> Option<SectorID> {
		let sector_len = self.world_unit_size * FIELD_RESOLUTION as f32;
		// find the global point in space for Sector (0, 0) based on the global origin
		let top_left = Vec2::new(self.size.0 / -2.0, self.size.1 / 2.0)
			+ Vec2::new(self.origin.0, self.origin.1);
		// find the bottom corner
		let bottom_right = Vec2::new(self.size.0 / 2.0, self.size.1 / -2.0)
			+ Vec2::new(self.origin.0, self.origin.1);
		// ensure position is within bounds
		if position.x < top_left.x
			|| position.x > bottom_right.x
			|| position.y > top_left.y
			|| position.y < bottom_right.y
		{
			error!(
				"Position is out of bounds of Dimensions, x {}, y {}, cannot calculate SectorID. Is the actor outside of the map or trying to request route outside of it?",
				position.x, position.y
			);
			//TODO use Result instead
			return None;
		}
		// get the vector size from fields origin to position
		let to_pos = (position - top_left).abs();
		// the lengths of this vector when divided by the size of a sector reveal the sector ID
		let col = (to_pos.x / sector_len).floor();
		let row = (to_pos.y / sector_len).floor();
		Some(SectorID::new(col as i32, row as i32))
	}

	/// Get the `(x,y)` coordinates of the top left corner of a sector in global space
	#[cfg(feature = "2d")]
	pub fn get_sector_corner_xy(&self, sector_id: &SectorID) -> Vec2 {
		let sector_len = self.world_unit_size * FIELD_RESOLUTION as f32;
		// find the global point in space for Sector (0, 0) based on the global origin
		let top_left = Vec2::new(self.size.0 / -2.0, self.size.1 / 2.0)
			+ Vec2::new(self.origin.0, self.origin.1);

		let relative_offset = Vec2::new(
			sector_id.get_column() as f32 * sector_len,
			sector_id.get_row() as f32 * -sector_len,
		);

		top_left + relative_offset
	}
	/// From a 2d position get the sector and field cell it resides in
	#[cfg(feature = "2d")]
	pub fn get_sector_and_field_cell_from_xy(
		&self,
		position: Vec2,
	) -> Option<(SectorID, FieldCell)> {
		if let Some(sector_id) = self.get_sector_id_from_xy(position) {
			let sector_corner_origin = self.get_sector_corner_xy(&sector_id);
			let field_id_0 =
				((position.x - sector_corner_origin.x) / self.world_unit_size).floor() as usize;
			let field_id_1 =
				((-position.y + sector_corner_origin.y) / self.world_unit_size).floor() as usize;
			let field_id = FieldCell::new(field_id_0, field_id_1);
			return Some((sector_id, field_id));
		}
		//TODO return Result
		None
	}
	/// From a field cell within a Sector retrieve the 2d Vec2 of its
	/// position. If the position sits outside of the world then [None] is
	/// returned
	#[cfg(feature = "2d")]
	pub fn get_xy_from_field_sector(&self, sector: SectorID, field: FieldCell) -> Option<Vec2> {
		let sector_xy = self.get_sector_corner_xy(&sector);
		let f_col = field.get_column() as f32;
		let f_row = field.get_row() as f32;

		let f_offset = Vec2::new(f_col * self.world_unit_size, -f_row * self.world_unit_size);
		// point in space for top-left corner of the field cell
		let point = sector_xy + f_offset;
		if point.x.abs() > self.origin.0.abs() + self.get_length() / 2.0
			|| point.y.abs() > self.origin.1.abs() + self.get_depth() / 2.0
		{
			None
		} else {
			Some(point)
		}
	}

	/// From a field cell within a Sector retrieve the 2d (x-z) Vec3 of its
	/// position. If the position is outside of the world then [None] is
	/// returned
	///
	/// The `y` coordinate is defaulted to `0.0`.
	#[cfg(feature = "3d")]
	pub fn get_xyz_from_field_sector(&self, sector: SectorID, field: FieldCell) -> Option<Vec3> {
		let sector_xyz = self.get_sector_corner_xyz(&sector);
		let f_col = field.get_column() as f32;
		let f_row = field.get_row() as f32;

		let f_offset = Vec3::new(
			f_col * self.world_unit_size,
			0.0,
			f_row * self.world_unit_size,
		);
		// point in space for top-left corner of the field cell
		let point = sector_xyz + f_offset;
		if point.x.abs() > self.origin.0.abs() + self.get_length() / 2.0
			|| point.z.abs() > self.origin.1.abs() + self.get_depth() / 2.0
		{
			None
		} else {
			Some(point)
		}
	}

	/// From a position in `x, y, z` space and the dimensions of the map calculate
	/// the sector ID that point resides in
	#[cfg(feature = "3d")]
	pub fn get_sector_id_from_xyz(&self, position: Vec3) -> Option<SectorID> {
		let sector_len = self.world_unit_size * FIELD_RESOLUTION as f32;
		// find the global point in space for Sector (0, 0) based on the global origin
		let top_left = Vec2::new(self.size.0 / -2.0, self.size.1 / -2.0)
			+ Vec2::new(self.origin.0, self.origin.1);
		// find the bottom corner
		let bottom_right = Vec2::new(self.size.0 / 2.0, self.size.1 / 2.0)
			+ Vec2::new(self.origin.0, self.origin.1);
		// ensure position is within bounds
		if position.x < top_left.x
			|| position.x > bottom_right.x
			|| position.z < top_left.y
			|| position.z > bottom_right.y
		{
			//TODO use Result instead
			return None;
		}
		// get the vector size from fields origin to position
		let to_pos_x = (position.x - top_left.x).abs();
		let to_pos_z = (position.z - top_left.y).abs();
		// the lengths of this vector when divided by the size of a sector reveal the sector ID
		let col = (to_pos_x / sector_len).floor();
		let row = (to_pos_z / sector_len).floor();
		Some(SectorID::new(col as i32, row as i32))
	}

	/// Calculate the `x, y, z` coordinates at the top-left corner of a sector based on map dimensions
	#[cfg(feature = "3d")]
	pub fn get_sector_corner_xyz(&self, sector_id: &SectorID) -> Vec3 {
		let sector_len = self.world_unit_size * FIELD_RESOLUTION as f32;
		// find the global point in space for Sector (0, 0) based on the global origin
		let top_left = Vec3::new(self.size.0 / -2.0, 0.0, self.size.1 / -2.0)
			+ Vec3::new(self.origin.0, 0.0, self.origin.1);

		let relative_offset = Vec3::new(
			sector_id.get_column() as f32 * sector_len,
			0.0,
			sector_id.get_row() as f32 * sector_len,
		);

		top_left + relative_offset
	}
	/// From a point in 3D space calculate what Sector and field cell it resides in
	#[cfg(feature = "3d")]
	pub fn get_sector_and_field_cell_from_xyz(
		&self,
		position: Vec3,
	) -> Option<(SectorID, FieldCell)> {
		if let Some(sector_id) = self.get_sector_id_from_xyz(position) {
			let sector_corner_origin = self.get_sector_corner_xyz(&sector_id);
			let field_id_0 =
				((position.x - sector_corner_origin.x) / self.world_unit_size).floor() as usize;
			let field_id_1 =
				((position.z - sector_corner_origin.z) / self.world_unit_size).floor() as usize;
			let field_id = FieldCell::new(field_id_0, field_id_1);
			return Some((sector_id, field_id));
		}
		//TODO return Result
		None
	}

	/// A sector has up to four neighbours. Based on the ID of the sector and the dimensions
	/// of the map retrieve the IDs neighbouring sectors
	pub fn get_ids_of_neighbouring_sectors(self, sector_id: &SectorID) -> Vec<SectorID> {
		CompassDir::get_sector_neighbours(
			sector_id,
			self.get_length(),
			self.get_depth(),
			self.get_unit_scale(),
		)
	}

	/// A sector has up to four neighbours. Based on the ID of the sector and the dimensions
	/// of the map retrieve the IDs neighbouring sectors and the [CompassDir] direction from the
	/// current sector that that sector is found in
	pub fn get_compass_dir_and_ids_of_neighbouring_sectors(
		&self,
		sector_id: &SectorID,
	) -> Vec<(CompassDir, SectorID)> {
		CompassDir::get_sector_neighbours_with_compass_dir(
			sector_id,
			self.get_length(),
			self.get_depth(),
			self.get_unit_scale(),
		)
	}
	/// From an [CompassDir] get the ID of a neighbouring sector. Returns [None]
	/// if the sector would be out of bounds
	pub fn get_sector_id_from_compass_dir(
		&self,
		compass_dir: CompassDir,
		sector_id: &SectorID,
	) -> Option<SectorID> {
		let sector_column_limit =
			(self.get_length() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as i32 - 1;
		let sector_row_limit =
			(self.get_depth() / (self.world_unit_size * FIELD_RESOLUTION as f32)) as i32 - 1;
		match compass_dir {
			CompassDir::North => {
				if sector_id.get_row() > 0 {
					Some(SectorID::new(
						sector_id.get_column(),
						sector_id.get_row() - 1,
					))
				} else {
					None
				}
			}
			CompassDir::East => {
				if sector_id.get_column() < sector_column_limit {
					Some(SectorID::new(
						sector_id.get_column() + 1,
						sector_id.get_row(),
					))
				} else {
					None
				}
			}
			CompassDir::South => {
				if sector_id.get_row() < sector_row_limit {
					Some(SectorID::new(
						sector_id.get_column(),
						sector_id.get_row() + 1,
					))
				} else {
					None
				}
			}
			CompassDir::West => {
				if sector_id.get_column() > 0 {
					Some(SectorID::new(
						sector_id.get_column() - 1,
						sector_id.get_row(),
					))
				} else {
					None
				}
			}
			CompassDir::NorthEast => {
				if sector_id.get_row() > 0 {
					if sector_id.get_column() < sector_column_limit {
						Some(SectorID::new(
							sector_id.get_column() + 1,
							sector_id.get_row() - 1,
						))
					} else {
						None
					}
				} else {
					None
				}
			}
			CompassDir::SouthEast => {
				if sector_id.get_row() < sector_row_limit {
					if sector_id.get_column() < sector_column_limit {
						Some(SectorID::new(
							sector_id.get_column() + 1,
							sector_id.get_row() + 1,
						))
					} else {
						None
					}
				} else {
					None
				}
			}
			CompassDir::SouthWest => {
				if sector_id.get_row() < sector_row_limit {
					if sector_id.get_column() > 0 {
						Some(SectorID::new(
							sector_id.get_column() - 1,
							sector_id.get_row() + 1,
						))
					} else {
						None
					}
				} else {
					None
				}
			}
			CompassDir::NorthWest => {
				if sector_id.get_row() > 0 {
					if sector_id.get_column() > 0 {
						Some(SectorID::new(
							sector_id.get_column() - 1,
							sector_id.get_row() - 1,
						))
					} else {
						None
					}
				} else {
					None
				}
			}
			CompassDir::Zero => {
				error!(
					"`get_sector_id_from_compass_dir` should never be called with `CompassDir::Zero`"
				);
				None
			}
		}
	}
}

// #[rustfmt::skip]
#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn cell_col_count() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);

		let actual = 30;
		let result = dimensions.get_total_field_cell_columns();
		assert!(actual == result);
	}
	#[test]
	fn cell_row_count() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);

		let actual = 30;
		let result = dimensions.get_total_field_cell_rows();
		assert!(actual == result);
	}
	#[test]
	fn sector_col_count() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);

		let actual = 3;
		let result = dimensions.get_sector_column_count();
		assert!(actual == result);
	}
	#[test]
	fn sector_row_count() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);

		let actual = 3;
		let result = dimensions.get_sector_row_count();
		assert!(actual == result);
	}

	#[test]
	#[cfg(feature = "3d")]
	fn sector_costfields_top_left_sector_id_from_xyz() {
		let origin = (0.0, 0.0);
		let size = (20.0, 20.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(-5.0, 0.0, -5.0);
		let result = dimensions.get_sector_id_from_xyz(position).unwrap();
		let actual: SectorID = SectorID::new(0, 0);
		assert_eq!(actual, result);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_costfields_top_right_sector_id_from_xyz() {
		let origin = (0.0, 0.0);
		let size = (20.0, 20.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(5.0, 0.0, -5.0);
		let result = dimensions.get_sector_id_from_xyz(position).unwrap();
		let actual: SectorID = SectorID::new(1, 0);
		assert_eq!(actual, result);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_costfields_bottom_right_sector_id_from_xyz() {
		let origin = (0.0, 0.0);
		let size = (20.0, 20.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(5.0, 0.0, 5.0);
		let result = dimensions.get_sector_id_from_xyz(position).unwrap();
		let actual: SectorID = SectorID::new(1, 1);
		assert_eq!(actual, result);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_costfields_bottom_left_sector_id_from_xyz() {
		let origin = (0.0, 0.0);
		let size = (20.0, 20.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(-5.0, 0.0, 5.0);
		let result = dimensions.get_sector_id_from_xyz(position).unwrap();
		let actual: SectorID = SectorID::new(0, 1);
		assert_eq!(actual, result);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_fieldcell_id_from_xyz() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(0.0, 0.0, 0.0);
		let result = dimensions
			.get_sector_and_field_cell_from_xyz(position)
			.unwrap();
		let actual = FieldCell::new(5, 5);
		assert_eq!(actual, result.1);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_fieldcell_id_from_xyz_small() {
		let origin = (0.0, 0.0);
		let size = (50.0, 100.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(0.0, 0.0, 0.0);
		let result = dimensions
			.get_sector_and_field_cell_from_xyz(position)
			.unwrap();
		let actual_sector = SectorID::new(2, 5);
		let actual_field = FieldCell::new(5, 0);
		assert_eq!(actual_sector, result.0);
		assert_eq!(actual_field, result.1);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_fieldcell_id_from_xyz_single() {
		let origin = (0.0, 0.0);
		let size = (10.0, 10.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(0.0, 0.0, 0.0);
		let result = dimensions
			.get_sector_and_field_cell_from_xyz(position)
			.unwrap();
		let actual_sector = SectorID::new(0, 0);
		let actual_field = FieldCell::new(5, 5);
		assert_eq!(actual_sector, result.0);
		assert_eq!(actual_field, result.1);
	}
	#[test]
	#[cfg(feature = "2d")]
	fn sector_from_xy_none() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec2::new(-1500.0, 0.0);
		let result = dimensions.get_sector_id_from_xy(position);

		assert!(result.is_none());
	}
	#[test]
	#[cfg(feature = "2d")]
	fn sector_from_xy() {
		let origin = (0.0, 0.0);
		let size = (1280.0, 1280.0);
		let world_unit_size = 64.0;
		let actor_radius = 16.0;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec2::new(530.0, 75.0);
		let result = dimensions.get_sector_id_from_xy(position);
		let actual = SectorID::new(1, 0);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_xyz_corner_zero() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(0, 0);
		let result = dimensions.get_sector_corner_xyz(&sector_id);
		let actual = Vec3::new(-15.0, 0.0, -15.0);
		assert_eq!(actual, result)
	}
	#[test]
	#[cfg(feature = "3d")]
	fn sector_xyz_corner_centre() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_corner_xyz(&sector_id);
		let actual = Vec3::new(-5.0, 0.0, -5.0);
		assert_eq!(actual, result)
	}
	#[test]
	fn get_northern_sector_neighbours() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(4, 0);
		let result = dimensions.get_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			SectorID::new(5, 0),
			SectorID::new(4, 1),
			SectorID::new(3, 0),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_eastern_sector_neighbours() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(19, 3);
		let result = dimensions.get_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			SectorID::new(19, 2),
			SectorID::new(19, 4),
			SectorID::new(18, 3),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_southern_sector_neighbours() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(5, 19);
		let result = dimensions.get_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			SectorID::new(5, 18),
			SectorID::new(6, 19),
			SectorID::new(4, 19),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_western_sector_neighbours() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(0, 5);
		let result = dimensions.get_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			SectorID::new(0, 4),
			SectorID::new(1, 5),
			SectorID::new(0, 6),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_centre_sector_neighbours() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(5, 7);
		let result = dimensions.get_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			SectorID::new(5, 6),
			SectorID::new(6, 7),
			SectorID::new(5, 8),
			SectorID::new(4, 7),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_northern_sector_neighbours_with_direction() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(4, 0);
		let result = dimensions.get_compass_dir_and_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			(CompassDir::East, SectorID::new(5, 0)),
			(CompassDir::South, SectorID::new(4, 1)),
			(CompassDir::West, SectorID::new(3, 0)),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_eastern_sector_neighbours_with_direction() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(19, 3);
		let result = dimensions.get_compass_dir_and_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			(CompassDir::North, SectorID::new(19, 2)),
			(CompassDir::South, SectorID::new(19, 4)),
			(CompassDir::West, SectorID::new(18, 3)),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_southern_sector_neighbours_with_direction() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(5, 19);
		let result = dimensions.get_compass_dir_and_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			(CompassDir::North, SectorID::new(5, 18)),
			(CompassDir::East, SectorID::new(6, 19)),
			(CompassDir::West, SectorID::new(4, 19)),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_western_sector_neighbours_with_direction() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(0, 5);
		let result = dimensions.get_compass_dir_and_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			(CompassDir::North, SectorID::new(0, 4)),
			(CompassDir::East, SectorID::new(1, 5)),
			(CompassDir::South, SectorID::new(0, 6)),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn get_centre_sector_neighbours_with_direction() {
		let origin = (0.0, 0.0);
		let size = (200.0, 200.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(5, 7);
		let result = dimensions.get_compass_dir_and_ids_of_neighbouring_sectors(&sector_id);
		let actual = vec![
			(CompassDir::North, SectorID::new(5, 6)),
			(CompassDir::East, SectorID::new(6, 7)),
			(CompassDir::South, SectorID::new(5, 8)),
			(CompassDir::West, SectorID::new(4, 7)),
		];
		assert_eq!(actual, result);
	}
	#[test]
	fn sector_id_compass_dir_north() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::North, &sector_id);
		let actual = SectorID::new(1, 0);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_east() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::East, &sector_id);
		let actual = SectorID::new(2, 1);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_south() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::South, &sector_id);
		let actual = SectorID::new(1, 2);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_west() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::West, &sector_id);
		let actual = SectorID::new(0, 1);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_northeast() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::NorthEast, &sector_id);
		let actual = SectorID::new(2, 0);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_southeast() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::SouthEast, &sector_id);
		let actual = SectorID::new(2, 2);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_southwest() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::SouthWest, &sector_id);
		let actual = SectorID::new(0, 2);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_northwest() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 1);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::NorthWest, &sector_id);
		let actual = SectorID::new(0, 0);
		assert_eq!(actual, result.unwrap());
	}
	#[test]
	fn sector_id_compass_dir_oob() {
		let origin = (0.0, 0.0);
		let size = (300.0, 300.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(1, 0);
		let result = dimensions.get_sector_id_from_compass_dir(CompassDir::North, &sector_id);
		assert!(result.is_none())
	}
	#[test]
	#[cfg(feature = "2d")]
	fn get_xy() {
		let origin = (0.0, 0.0);
		let size = (1920.0, 1920.0);
		let world_unit_size = 64.0;
		let actor_radius = 16.0;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(2, 1);
		let field_id = FieldCell::new(6, 2);
		let actual = Vec2::new(704.0, 192.0);
		let result = dimensions
			.get_xy_from_field_sector(sector_id, field_id)
			.unwrap();
		assert_eq!(actual, result);
	}
	#[test]
	#[cfg(feature = "3d")]
	fn get_xyz() {
		let origin = (0.0, 0.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(2, 1);
		let field_id = FieldCell::new(6, 2);
		let actual = Vec3::new(11.0, 0.0, -3.0);
		let result = dimensions
			.get_xyz_from_field_sector(sector_id, field_id)
			.unwrap();
		assert_eq!(actual, result);
	}

	#[test]
	#[cfg(feature = "2d")]
	fn get_xy_origin_offset() {
		let origin = (224.0, -84.0);
		let size = (1920.0, 1920.0);
		let world_unit_size = 64.0;
		let actor_radius = 16.0;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(2, 1);
		let field_id = FieldCell::new(6, 2);
		let actual = Vec2::new(928.0, 108.0);
		let result = dimensions
			.get_xy_from_field_sector(sector_id, field_id)
			.unwrap();
		assert_eq!(actual, result);
	}

	#[test]
	#[cfg(feature = "3d")]
	fn get_xyz_origin_offset() {
		let origin = (-53.0, -162.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let sector_id = SectorID::new(2, 1);
		let field_id = FieldCell::new(6, 2);
		let actual = Vec3::new(-42.0, 0.0, -165.0);
		let result = dimensions
			.get_xyz_from_field_sector(sector_id, field_id)
			.unwrap();
		assert_eq!(actual, result);
	}

	#[test]
	#[cfg(feature = "2d")]
	fn get_sid_fc_from_xy_origin_offset() {
		let origin = (224.0, -84.0);
		let size = (1920.0, 1920.0);
		let world_unit_size = 64.0;
		let actor_radius = 16.0;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec2::new(928.0, 108.0);
		let (sector, cell) = dimensions
			.get_sector_and_field_cell_from_xy(position)
			.unwrap();

		let actual_sector = SectorID::new(2, 1);
		assert_eq!(actual_sector, sector);
		let actual_cell = FieldCell::new(6, 2);
		assert_eq!(actual_cell, cell);
	}

	#[test]
	#[cfg(feature = "3d")]
	fn get_sid_fc_from_xyz_origin_offset() {
		let origin = (-53.0, -162.0);
		let size = (30.0, 30.0);
		let world_unit_size = 1.0;
		let actor_radius = 0.5;
		let dimensions = Dimensions::new(origin, size, world_unit_size, actor_radius);
		let position = Vec3::new(-42.0, 0.0, -165.0);
		let (sector, cell) = dimensions
			.get_sector_and_field_cell_from_xyz(position)
			.unwrap();

		let actual_sector = SectorID::new(2, 1);
		assert_eq!(actual_sector, sector);
		let actual_cell = FieldCell::new(6, 2);
		assert_eq!(actual_cell, cell);
	}
}