author | alfadur |
Tue, 30 Jul 2019 19:53:23 +0300 | |
changeset 15281 | 8095853811a6 |
parent 15275 | 66c987015f2d |
child 15305 | 0076bf602969 |
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 |
|
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::{ |
14716 | 11 |
collision::{CollisionData, CollisionProcessor, ContactData}, |
15275 | 12 |
common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId, Millis}, |
14716 | 13 |
physics::{PhysicsData, PhysicsProcessor}, |
15270 | 14 |
time::TimeProcessor, |
14178 | 15 |
}; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
16 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
17 |
pub struct JoinedData { |
14178 | 18 |
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
|
19 |
physics: PhysicsData, |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
20 |
collision: CollisionData, |
14716 | 21 |
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
|
22 |
} |
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 |
pub struct World { |
15274 | 25 |
allocator: GearAllocator, |
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 { |
|
15274 | 47 |
allocator: GearAllocator::new(), |
14179 | 48 |
physics: PhysicsProcessor::new(), |
14716 | 49 |
collision: CollisionProcessor::new(world_size), |
15270 | 50 |
time: TimeProcessor::new(), |
14179 | 51 |
} |
52 |
} |
|
53 |
||
15274 | 54 |
#[inline] |
55 |
pub fn new_gear(&mut self) -> Option<GearId> { |
|
56 |
self.allocator.alloc() |
|
57 |
} |
|
58 |
||
59 |
#[inline] |
|
60 |
pub fn delete_gear(&mut self, gear_id: GearId) { |
|
61 |
self.physics.remove(gear_id); |
|
62 |
self.collision.remove(gear_id); |
|
63 |
self.time.cancel(gear_id); |
|
64 |
self.allocator.free(gear_id) |
|
65 |
} |
|
66 |
||
15275 | 67 |
pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
14178 | 68 |
let updates = self.physics.process(time_step); |
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
14716
diff
changeset
|
69 |
let collision = self.collision.process(land, &updates); |
15270 | 70 |
let events = self.time.process(time_step); |
14144 | 71 |
} |
72 |
||
14179 | 73 |
pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T) |
14716 | 74 |
where |
75 |
T: GearData, |
|
76 |
Self: GearDataAggregator<T>, |
|
14179 | 77 |
{ |
78 |
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
|
79 |
} |
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 |
#[cfg(test)] |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
83 |
mod tests { |
14179 | 84 |
use crate::{ |
14716 | 85 |
collision::{CircleBounds, CollisionData}, |
14179 | 86 |
physics::PhysicsData, |
14716 | 87 |
World, |
14179 | 88 |
}; |
14716 | 89 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 90 |
use integral_geometry::Size; |
91 |
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
|
92 |
|
14179 | 93 |
#[test] |
94 |
fn data_flow() { |
|
95 |
let world_size = Size::new(2048, 2048); |
|
96 |
||
97 |
let mut world = World::new(world_size); |
|
98 |
let gear_id = 46631; |
|
99 |
||
14716 | 100 |
world.add_gear_data( |
101 |
gear_id, |
|
102 |
PhysicsData { |
|
103 |
position: FPPoint::zero(), |
|
104 |
velocity: FPPoint::unit_y(), |
|
105 |
}, |
|
106 |
); |
|
14179 | 107 |
|
14716 | 108 |
world.add_gear_data( |
109 |
gear_id, |
|
110 |
CollisionData { |
|
111 |
bounds: CircleBounds { |
|
112 |
center: FPPoint::zero(), |
|
113 |
radius: fp!(10), |
|
114 |
}, |
|
115 |
}, |
|
116 |
); |
|
14179 | 117 |
|
118 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
119 |
||
120 |
world.step(fp!(1), &land); |
|
121 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
122 |
} |