author | alfadur |
Thu, 25 Jul 2019 19:58:19 +0300 | |
changeset 15270 | 7446258fab98 |
parent 15266 | b58f98bbc120 |
child 15274 | 42b710b0f883 |
permissions | -rw-r--r-- |
14716 | 1 |
pub mod collision; |
2 |
pub mod common; |
|
14178 | 3 |
mod grid; |
14716 | 4 |
pub mod physics; |
15270 | 5 |
pub mod time; |
14144 | 6 |
|
14178 | 7 |
use fpnum::FPNum; |
14179 | 8 |
use integral_geometry::Size; |
14144 | 9 |
use land2d::Land2D; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
10 |
|
14178 | 11 |
use crate::{ |
14716 | 12 |
collision::{CollisionData, CollisionProcessor, ContactData}, |
13 |
common::{GearData, GearDataAggregator, GearDataProcessor, GearId}, |
|
14 |
physics::{PhysicsData, PhysicsProcessor}, |
|
15270 | 15 |
time::TimeProcessor, |
14178 | 16 |
}; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
17 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
18 |
pub struct JoinedData { |
14178 | 19 |
gear_id: GearId, |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
20 |
physics: PhysicsData, |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
21 |
collision: CollisionData, |
14716 | 22 |
contact: ContactData, |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
23 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
24 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
25 |
pub struct World { |
14178 | 26 |
physics: PhysicsProcessor, |
27 |
collision: CollisionProcessor, |
|
15270 | 28 |
time: TimeProcessor, |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
29 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
30 |
|
14179 | 31 |
macro_rules! processor_map { |
32 |
( $data_type: ident => $field: ident ) => { |
|
33 |
impl GearDataAggregator<$data_type> for World { |
|
34 |
fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> { |
|
35 |
&mut self.$field |
|
36 |
} |
|
37 |
} |
|
14716 | 38 |
}; |
14179 | 39 |
} |
40 |
||
41 |
processor_map!(PhysicsData => physics); |
|
42 |
processor_map!(CollisionData => collision); |
|
43 |
||
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
44 |
impl World { |
14179 | 45 |
pub fn new(world_size: Size) -> Self { |
46 |
Self { |
|
47 |
physics: PhysicsProcessor::new(), |
|
14716 | 48 |
collision: CollisionProcessor::new(world_size), |
15270 | 49 |
time: TimeProcessor::new(), |
14179 | 50 |
} |
51 |
} |
|
52 |
||
14144 | 53 |
pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) { |
14178 | 54 |
let updates = self.physics.process(time_step); |
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
14716
diff
changeset
|
55 |
let collision = self.collision.process(land, &updates); |
15270 | 56 |
let events = self.time.process(time_step); |
14144 | 57 |
} |
58 |
||
14179 | 59 |
pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T) |
14716 | 60 |
where |
61 |
T: GearData, |
|
62 |
Self: GearDataAggregator<T>, |
|
14179 | 63 |
{ |
64 |
self.find_processor().add(gear_id, data); |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
65 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
66 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
67 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
68 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
69 |
mod tests { |
14179 | 70 |
use crate::{ |
14716 | 71 |
collision::{CircleBounds, CollisionData}, |
14179 | 72 |
physics::PhysicsData, |
14716 | 73 |
World, |
14179 | 74 |
}; |
14716 | 75 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 76 |
use integral_geometry::Size; |
77 |
use land2d::Land2D; |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
78 |
|
14179 | 79 |
#[test] |
80 |
fn data_flow() { |
|
81 |
let world_size = Size::new(2048, 2048); |
|
82 |
||
83 |
let mut world = World::new(world_size); |
|
84 |
let gear_id = 46631; |
|
85 |
||
14716 | 86 |
world.add_gear_data( |
87 |
gear_id, |
|
88 |
PhysicsData { |
|
89 |
position: FPPoint::zero(), |
|
90 |
velocity: FPPoint::unit_y(), |
|
91 |
}, |
|
92 |
); |
|
14179 | 93 |
|
14716 | 94 |
world.add_gear_data( |
95 |
gear_id, |
|
96 |
CollisionData { |
|
97 |
bounds: CircleBounds { |
|
98 |
center: FPPoint::zero(), |
|
99 |
radius: fp!(10), |
|
100 |
}, |
|
101 |
}, |
|
102 |
); |
|
14179 | 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 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
108 |
} |