14199
|
1 |
use std::{
|
|
2 |
ops::RangeInclusive
|
|
3 |
};
|
|
4 |
|
|
5 |
use crate::{
|
14200
|
6 |
common::{GearId, GearData, GearDataProcessor},
|
14199
|
7 |
physics::PhysicsData,
|
|
8 |
grid::Grid
|
|
9 |
};
|
|
10 |
|
|
11 |
use fpnum::*;
|
|
12 |
use integral_geometry::{
|
|
13 |
Point, Size, GridIndex
|
|
14 |
};
|
|
15 |
use land2d::Land2D;
|
|
16 |
|
|
17 |
pub fn fppoint_round(point: &FPPoint) -> Point {
|
|
18 |
Point::new(point.x().round() as i32, point.y().round() as i32)
|
|
19 |
}
|
|
20 |
|
|
21 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
22 |
pub struct CircleBounds {
|
|
23 |
pub center: FPPoint,
|
|
24 |
pub radius: FPNum
|
|
25 |
}
|
|
26 |
|
|
27 |
impl CircleBounds {
|
|
28 |
pub fn intersects(&self, other: &CircleBounds) -> bool {
|
|
29 |
(other.center - self.center).is_in_range(self.radius + other.radius)
|
|
30 |
}
|
|
31 |
|
|
32 |
pub fn rows(&self) -> impl Iterator<Item = (usize, RangeInclusive<usize>)> {
|
|
33 |
let radius = self.radius.abs_round() as usize;
|
|
34 |
let center = Point::from_fppoint(&self.center);
|
|
35 |
(center.y as usize - radius..=center.y as usize + radius)
|
|
36 |
.map(move |row| (row, center.x as usize - radius..=center.x as usize + radius))
|
|
37 |
}
|
|
38 |
}
|
|
39 |
|
|
40 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
41 |
pub struct CollisionData {
|
|
42 |
pub bounds: CircleBounds
|
|
43 |
}
|
|
44 |
|
14200
|
45 |
impl GearData for CollisionData {}
|
|
46 |
|
14199
|
47 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
48 |
pub struct ContactData {
|
|
49 |
pub elasticity: FPNum,
|
|
50 |
pub friction: FPNum
|
|
51 |
}
|
|
52 |
|
14200
|
53 |
impl GearData for ContactData {}
|
|
54 |
|
14199
|
55 |
struct EnabledCollisionsCollection {
|
|
56 |
gear_ids: Vec<GearId>,
|
|
57 |
collisions: Vec<CollisionData>
|
|
58 |
}
|
|
59 |
|
|
60 |
impl EnabledCollisionsCollection {
|
14200
|
61 |
fn new() -> Self {
|
|
62 |
Self {
|
|
63 |
gear_ids: Vec::new(),
|
|
64 |
collisions: Vec::new()
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
14199
|
68 |
fn push(&mut self, gear_id: GearId, collision: CollisionData) {
|
|
69 |
self.gear_ids.push(gear_id);
|
|
70 |
self.collisions.push(collision);
|
|
71 |
}
|
|
72 |
|
|
73 |
fn iter(&self) -> impl Iterator<Item = (GearId, &CollisionData)> {
|
|
74 |
self.gear_ids.iter().cloned().zip(self.collisions.iter())
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
pub struct CollisionProcessor {
|
|
79 |
grid: Grid,
|
|
80 |
enabled_collisions: EnabledCollisionsCollection,
|
|
81 |
|
|
82 |
detected_collisions: DetectedCollisions,
|
|
83 |
}
|
|
84 |
|
|
85 |
pub struct DetectedCollisions {
|
|
86 |
pub pairs: Vec<(GearId, GearId)>,
|
|
87 |
pub positions: Vec<Point>
|
|
88 |
}
|
|
89 |
|
|
90 |
impl DetectedCollisions {
|
|
91 |
pub fn new(capacity: usize) -> Self {
|
|
92 |
Self {
|
|
93 |
pairs: Vec::with_capacity(capacity),
|
|
94 |
positions: Vec::with_capacity(capacity),
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
pub fn push(&mut self, contact_gear_id1: GearId, contact_gear_id2: GearId, position: &FPPoint) {
|
|
99 |
self.pairs.push((contact_gear_id1, contact_gear_id2));
|
14200
|
100 |
self.positions.push(fppoint_round(&position));
|
14199
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
impl CollisionProcessor {
|
14200
|
105 |
pub fn new(size: Size) -> Self {
|
|
106 |
Self {
|
|
107 |
grid: Grid::new(size),
|
|
108 |
enabled_collisions: EnabledCollisionsCollection::new(),
|
|
109 |
detected_collisions: DetectedCollisions::new(0)
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
pub fn process(&mut self, land: &Land2D<u32>, updates: &crate::physics::PositionUpdates) {
|
14199
|
114 |
self.grid.check_collisions(&mut self.detected_collisions);
|
|
115 |
|
|
116 |
for (gear_id, collision) in self.enabled_collisions.iter() {
|
|
117 |
if collision.bounds.rows().any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0)) {
|
14200
|
118 |
self.detected_collisions.push(gear_id, 0, &collision.bounds.center)
|
14199
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
14200
|
122 |
}
|
14199
|
123 |
|
14200
|
124 |
impl GearDataProcessor<CollisionData> for CollisionProcessor {
|
|
125 |
fn add(&mut self, gear_id: GearId, gear_data: CollisionData) {
|
|
126 |
self.grid.insert_static(gear_id, &gear_data.bounds);
|
14199
|
127 |
}
|
|
128 |
} |