rust/hwphysics/src/lib.rs
author alfadur
Tue, 30 Jul 2019 19:53:23 +0300
changeset 15286 8095853811a6
parent 15280 66c987015f2d
child 15310 0076bf602969
permissions -rw-r--r--
add gear lookup to the physics processor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     1
pub mod collision;
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     2
pub mod common;
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
     3
mod grid;
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     4
pub mod physics;
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
     5
pub mod time;
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
     6
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
     7
use integral_geometry::Size;
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
     8
use land2d::Land2D;
14064
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
     9
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    10
use crate::{
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    11
    collision::{CollisionData, CollisionProcessor, ContactData},
15280
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
    12
    common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId, Millis},
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    13
    physics::{PhysicsData, PhysicsProcessor},
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    14
    time::TimeProcessor,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    15
};
14064
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 {
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    18
    gear_id: GearId,
14064
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,
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    21
    contact: ContactData,
14064
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 {
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    25
    allocator: GearAllocator,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    26
    physics: PhysicsProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    27
    collision: CollisionProcessor,
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    28
    time: TimeProcessor,
14064
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
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    31
macro_rules! processor_map {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    32
    ( $data_type: ident => $field: ident ) => {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    33
        impl GearDataAggregator<$data_type> for World {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    34
            fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    35
                &mut self.$field
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    36
            }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    37
        }
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    38
    };
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    39
}
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    40
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    41
processor_map!(PhysicsData => physics);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    42
processor_map!(CollisionData => collision);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    43
14064
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 {
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    45
    pub fn new(world_size: Size) -> Self {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    46
        Self {
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    47
            allocator: GearAllocator::new(),
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    48
            physics: PhysicsProcessor::new(),
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    49
            collision: CollisionProcessor::new(world_size),
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    50
            time: TimeProcessor::new(),
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    51
        }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    52
    }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    53
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    54
    #[inline]
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    55
    pub fn new_gear(&mut self) -> Option<GearId> {
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    56
        self.allocator.alloc()
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    57
    }
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    58
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    59
    #[inline]
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    60
    pub fn delete_gear(&mut self, gear_id: GearId) {
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    61
        self.physics.remove(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    62
        self.collision.remove(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    63
        self.time.cancel(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    64
        self.allocator.free(gear_id)
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    65
    }
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    66
15280
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
    67
    pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) {
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    68
        let updates = self.physics.process(time_step);
15271
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 14721
diff changeset
    69
        let collision = self.collision.process(land, &updates);
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    70
        let events = self.time.process(time_step);
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
    71
    }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
    72
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    73
    pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T)
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    74
    where
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    75
        T: GearData,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    76
        Self: GearDataAggregator<T>,
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    77
    {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    78
        self.find_processor().add(gear_id, data);
14064
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 {
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    84
    use crate::{
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    85
        collision::{CircleBounds, CollisionData},
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    86
        physics::PhysicsData,
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    87
        World,
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    88
    };
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    89
    use fpnum::{fp, FPNum, FPPoint};
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    90
    use integral_geometry::Size;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    91
    use land2d::Land2D;
14064
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    92
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    93
    #[test]
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    94
    fn data_flow() {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    95
        let world_size = Size::new(2048, 2048);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    96
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    97
        let mut world = World::new(world_size);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    98
        let gear_id = 46631;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    99
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   100
        world.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   101
            gear_id,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   102
            PhysicsData {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   103
                position: FPPoint::zero(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   104
                velocity: FPPoint::unit_y(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   105
            },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   106
        );
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   107
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   108
        world.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   109
            gear_id,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   110
            CollisionData {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   111
                bounds: CircleBounds {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   112
                    center: FPPoint::zero(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   113
                    radius: fp!(10),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   114
                },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   115
            },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   116
        );
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   117
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   118
        let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   119
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   120
        world.step(fp!(1), &land);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   121
    }
14064
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   122
}