author | raptor <buckyballreaction@gmail.com> |
Tue, 13 Aug 2019 13:10:12 -0600 | |
branch | 0.9.25 |
changeset 15331 | 93b43cf0b792 |
parent 14179 | abbb74b9cb62 |
child 14716 | 8e74d4eb89f5 |
permissions | -rw-r--r-- |
14178 | 1 |
mod common; |
2 |
mod physics; |
|
3 |
mod grid; |
|
4 |
mod collision; |
|
14144 | 5 |
|
14178 | 6 |
use fpnum::FPNum; |
14179 | 7 |
use integral_geometry::Size; |
14144 | 8 |
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
|
9 |
|
14178 | 10 |
use crate::{ |
14179 | 11 |
common::{ |
12 |
GearId, |
|
13 |
GearData, |
|
14 |
GearDataAggregator, |
|
15 |
GearDataProcessor |
|
16 |
}, |
|
14178 | 17 |
physics::{ |
18 |
PhysicsProcessor, |
|
19 |
PhysicsData |
|
20 |
}, |
|
21 |
collision::{ |
|
22 |
CollisionProcessor, |
|
23 |
CollisionData, |
|
24 |
ContactData |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
25 |
} |
14178 | 26 |
}; |
14059
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 { |
14178 | 29 |
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
|
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 { |
14178 | 36 |
physics: PhysicsProcessor, |
37 |
collision: CollisionProcessor, |
|
14059
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 |
|
14179 | 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 |
||
14059
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 { |
14179 | 54 |
pub fn new(world_size: Size) -> Self { |
55 |
Self { |
|
56 |
physics: PhysicsProcessor::new(), |
|
57 |
collision: CollisionProcessor::new(world_size) |
|
58 |
} |
|
59 |
} |
|
60 |
||
14144 | 61 |
pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) { |
14178 | 62 |
let updates = self.physics.process(time_step); |
63 |
self.collision.process(land, &updates); |
|
14144 | 64 |
} |
65 |
||
14179 | 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); |
|
14059
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 { |
14179 | 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; |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
84 |
|
14179 | 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 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
108 |
} |