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