author | Wuzzy <Wuzzy2@mail.ru> |
Tue, 22 Jan 2019 07:17:07 +0100 | |
changeset 14680 | 46ac6fbde6f6 |
parent 14200 | abbb74b9cb62 |
child 14737 | 8e74d4eb89f5 |
permissions | -rw-r--r-- |
14199 | 1 |
mod common; |
2 |
mod physics; |
|
3 |
mod grid; |
|
4 |
mod collision; |
|
14165 | 5 |
|
14199 | 6 |
use fpnum::FPNum; |
14200 | 7 |
use integral_geometry::Size; |
14165 | 8 |
use land2d::Land2D; |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
9 |
|
14199 | 10 |
use crate::{ |
14200 | 11 |
common::{ |
12 |
GearId, |
|
13 |
GearData, |
|
14 |
GearDataAggregator, |
|
15 |
GearDataProcessor |
|
16 |
}, |
|
14199 | 17 |
physics::{ |
18 |
PhysicsProcessor, |
|
19 |
PhysicsData |
|
20 |
}, |
|
21 |
collision::{ |
|
22 |
CollisionProcessor, |
|
23 |
CollisionData, |
|
24 |
ContactData |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
25 |
} |
14199 | 26 |
}; |
14080
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 { |
14199 | 29 |
gear_id: GearId, |
14080
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 { |
14199 | 36 |
physics: PhysicsProcessor, |
37 |
collision: CollisionProcessor, |
|
14080
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 |
|
14200 | 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 |
||
14080
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 { |
14200 | 54 |
pub fn new(world_size: Size) -> Self { |
55 |
Self { |
|
56 |
physics: PhysicsProcessor::new(), |
|
57 |
collision: CollisionProcessor::new(world_size) |
|
58 |
} |
|
59 |
} |
|
60 |
||
14165 | 61 |
pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) { |
14199 | 62 |
let updates = self.physics.process(time_step); |
63 |
self.collision.process(land, &updates); |
|
14165 | 64 |
} |
65 |
||
14200 | 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); |
|
14080
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 { |
14200 | 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; |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
84 |
|
14200 | 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 |
} |
|
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
108 |
} |