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