author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 10 Dec 2018 08:55:45 +0100 | |
changeset 14409 | 9561bf634b21 |
parent 14184 | abbb74b9cb62 |
child 14721 | 8e74d4eb89f5 |
permissions | -rw-r--r-- |
14183 | 1 |
mod common; |
2 |
mod physics; |
|
3 |
mod grid; |
|
4 |
mod collision; |
|
14149 | 5 |
|
14183 | 6 |
use fpnum::FPNum; |
14184 | 7 |
use integral_geometry::Size; |
14149 | 8 |
use land2d::Land2D; |
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
9 |
|
14183 | 10 |
use crate::{ |
14184 | 11 |
common::{ |
12 |
GearId, |
|
13 |
GearData, |
|
14 |
GearDataAggregator, |
|
15 |
GearDataProcessor |
|
16 |
}, |
|
14183 | 17 |
physics::{ |
18 |
PhysicsProcessor, |
|
19 |
PhysicsData |
|
20 |
}, |
|
21 |
collision::{ |
|
22 |
CollisionProcessor, |
|
23 |
CollisionData, |
|
24 |
ContactData |
|
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
25 |
} |
14183 | 26 |
}; |
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
27 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
28 |
pub struct JoinedData { |
14183 | 29 |
gear_id: GearId, |
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
30 |
physics: PhysicsData, |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
31 |
collision: CollisionData, |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
32 |
contact: ContactData |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
33 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
34 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
35 |
pub struct World { |
14183 | 36 |
physics: PhysicsProcessor, |
37 |
collision: CollisionProcessor, |
|
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
38 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
39 |
|
14184 | 40 |
macro_rules! processor_map { |
41 |
( $data_type: ident => $field: ident ) => { |
|
42 |
impl GearDataAggregator<$data_type> for World { |
|
43 |
fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> { |
|
44 |
&mut self.$field |
|
45 |
} |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
processor_map!(PhysicsData => physics); |
|
51 |
processor_map!(CollisionData => collision); |
|
52 |
||
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
53 |
impl World { |
14184 | 54 |
pub fn new(world_size: Size) -> Self { |
55 |
Self { |
|
56 |
physics: PhysicsProcessor::new(), |
|
57 |
collision: CollisionProcessor::new(world_size) |
|
58 |
} |
|
59 |
} |
|
60 |
||
14149 | 61 |
pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) { |
14183 | 62 |
let updates = self.physics.process(time_step); |
63 |
self.collision.process(land, &updates); |
|
14149 | 64 |
} |
65 |
||
14184 | 66 |
pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T) |
67 |
where T: GearData, |
|
68 |
Self: GearDataAggregator<T> |
|
69 |
{ |
|
70 |
self.find_processor().add(gear_id, data); |
|
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
71 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
72 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
73 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
74 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
75 |
mod tests { |
14184 | 76 |
use crate::{ |
77 |
World, |
|
78 |
physics::PhysicsData, |
|
79 |
collision::{CollisionData, CircleBounds} |
|
80 |
}; |
|
81 |
use fpnum::{FPNum, FPPoint, fp}; |
|
82 |
use integral_geometry::Size; |
|
83 |
use land2d::Land2D; |
|
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
84 |
|
14184 | 85 |
#[test] |
86 |
fn data_flow() { |
|
87 |
let world_size = Size::new(2048, 2048); |
|
88 |
||
89 |
let mut world = World::new(world_size); |
|
90 |
let gear_id = 46631; |
|
91 |
||
92 |
world.add_gear_data(gear_id, PhysicsData { |
|
93 |
position: FPPoint::zero(), |
|
94 |
velocity: FPPoint::unit_y() |
|
95 |
}); |
|
96 |
||
97 |
world.add_gear_data(gear_id, CollisionData { |
|
98 |
bounds: CircleBounds { |
|
99 |
center: FPPoint::zero(), |
|
100 |
radius: fp!(10) |
|
101 |
} |
|
102 |
}); |
|
103 |
||
104 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
105 |
||
106 |
world.step(fp!(1), &land); |
|
107 |
} |
|
14064
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
108 |
} |