author | alfadur |
Wed, 28 Aug 2019 01:27:34 +0300 | |
changeset 15372 | 7a3ed957cee9 |
parent 15353 | 35c331f9308e |
child 15380 | 6e3e5be8b2e2 |
permissions | -rw-r--r-- |
14716 | 1 |
pub mod collision; |
2 |
pub mod common; |
|
15305 | 3 |
mod data; |
14178 | 4 |
mod grid; |
14716 | 5 |
pub mod physics; |
15270 | 6 |
pub mod time; |
14144 | 7 |
|
14179 | 8 |
use integral_geometry::Size; |
14144 | 9 |
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
|
10 |
|
14178 | 11 |
use crate::{ |
14716 | 12 |
collision::{CollisionData, CollisionProcessor, ContactData}, |
15275 | 13 |
common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId, Millis}, |
14716 | 14 |
physics::{PhysicsData, PhysicsProcessor}, |
15270 | 15 |
time::TimeProcessor, |
14178 | 16 |
}; |
14059
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 { |
14178 | 19 |
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
|
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, |
14716 | 22 |
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
|
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 { |
15274 | 26 |
allocator: GearAllocator, |
14178 | 27 |
physics: PhysicsProcessor, |
28 |
collision: CollisionProcessor, |
|
15270 | 29 |
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
|
30 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
31 |
|
14179 | 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 |
} |
|
14716 | 39 |
}; |
14179 | 40 |
} |
41 |
||
42 |
processor_map!(PhysicsData => physics); |
|
43 |
processor_map!(CollisionData => collision); |
|
44 |
||
14059
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 { |
14179 | 46 |
pub fn new(world_size: Size) -> Self { |
47 |
Self { |
|
15274 | 48 |
allocator: GearAllocator::new(), |
14179 | 49 |
physics: PhysicsProcessor::new(), |
14716 | 50 |
collision: CollisionProcessor::new(world_size), |
15270 | 51 |
time: TimeProcessor::new(), |
14179 | 52 |
} |
53 |
} |
|
54 |
||
15274 | 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 |
||
15275 | 68 |
pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) { |
14178 | 69 |
let updates = self.physics.process(time_step); |
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
14716
diff
changeset
|
70 |
let collision = self.collision.process(land, &updates); |
15270 | 71 |
let events = self.time.process(time_step); |
14144 | 72 |
} |
73 |
||
14179 | 74 |
pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T) |
14716 | 75 |
where |
76 |
T: GearData, |
|
77 |
Self: GearDataAggregator<T>, |
|
14179 | 78 |
{ |
79 |
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
|
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 { |
14179 | 85 |
use crate::{ |
14716 | 86 |
collision::{CircleBounds, CollisionData}, |
15353 | 87 |
common::Millis, |
14179 | 88 |
physics::PhysicsData, |
14716 | 89 |
World, |
14179 | 90 |
}; |
14716 | 91 |
use fpnum::{fp, FPNum, FPPoint}; |
14179 | 92 |
use integral_geometry::Size; |
93 |
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
|
94 |
|
14179 | 95 |
#[test] |
96 |
fn data_flow() { |
|
97 |
let world_size = Size::new(2048, 2048); |
|
98 |
||
99 |
let mut world = World::new(world_size); |
|
15353 | 100 |
let gear_id = std::num::NonZeroU16::new(46631).unwrap(); |
14179 | 101 |
|
14716 | 102 |
world.add_gear_data( |
103 |
gear_id, |
|
104 |
PhysicsData { |
|
105 |
position: FPPoint::zero(), |
|
106 |
velocity: FPPoint::unit_y(), |
|
107 |
}, |
|
108 |
); |
|
14179 | 109 |
|
14716 | 110 |
world.add_gear_data( |
111 |
gear_id, |
|
112 |
CollisionData { |
|
113 |
bounds: CircleBounds { |
|
114 |
center: FPPoint::zero(), |
|
115 |
radius: fp!(10), |
|
116 |
}, |
|
117 |
}, |
|
118 |
); |
|
14179 | 119 |
|
120 |
let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0); |
|
121 |
||
15353 | 122 |
world.step(Millis::new(1), &land); |
14179 | 123 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff
changeset
|
124 |
} |