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