rust/hwphysics/src/lib.rs
author sheepluva
Sun, 08 Dec 2019 02:09:19 +0100
branchhedgeroid
changeset 15520 de08a3ae4f8d
parent 15388 701ad89a9f2a
permissions -rw-r--r--
hedgeroid/frontlib: update proto and weapons number
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;
15310
0076bf602969 start gear data group implementation
alfadur
parents: 15286
diff changeset
     3
mod data;
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
     4
mod grid;
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     5
pub mod physics;
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
     6
pub mod time;
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
     7
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
     8
use integral_geometry::Size;
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
     9
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
    10
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    11
use crate::{
15386
52844baced17 add gravity
alfadur
parents: 15385
diff changeset
    12
    collision::CollisionProcessor,
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    13
    common::{GearAllocator, GearId, Millis},
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    14
    data::GearDataManager,
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    15
    physics::PhysicsProcessor,
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    16
    time::TimeProcessor,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    17
};
14064
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    18
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    19
pub struct World {
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    20
    allocator: GearAllocator,
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    21
    data: GearDataManager,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    22
    physics: PhysicsProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14149
diff changeset
    23
    collision: CollisionProcessor,
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    24
    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
    25
}
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
impl World {
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    28
    pub fn new(world_size: Size) -> Self {
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    29
        let mut data = GearDataManager::new();
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    30
        PhysicsProcessor::register_components(&mut data);
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    31
        CollisionProcessor::register_components(&mut data);
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    32
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    33
        Self {
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    34
            data,
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    35
            allocator: GearAllocator::new(),
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    36
            physics: PhysicsProcessor::new(),
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    37
            collision: CollisionProcessor::new(world_size),
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    38
            time: TimeProcessor::new(),
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
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    42
    #[inline]
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    43
    pub fn new_gear(&mut self) -> Option<GearId> {
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    44
        self.allocator.alloc()
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    45
    }
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    46
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    47
    #[inline]
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    48
    pub fn delete_gear(&mut self, gear_id: GearId) {
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    49
        self.data.remove_all(gear_id);
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    50
        self.collision.remove(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    51
        self.time.cancel(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    52
        self.allocator.free(gear_id)
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    53
    }
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
    54
15280
66c987015f2d replace time with milliseconds
alfadur
parents: 15279
diff changeset
    55
    pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) {
15388
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    56
        let updates = if time_step == Millis::new(1) {
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    57
            self.physics.process_single_tick(&mut self.data)
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    58
        } else {
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    59
            self.physics
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    60
                .process_multiple_ticks(&mut self.data, time_step)
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15386
diff changeset
    61
        };
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    62
        let collisions = self.collision.process(land, &updates);
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    63
        let events = self.time.process(time_step);
14149
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
    64
    }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14064
diff changeset
    65
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    66
    #[inline]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    67
    pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) {
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    68
        self.data.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
    69
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    70
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    71
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    72
#[cfg(test)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    73
mod tests {
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    74
    use crate::{
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    75
        collision::{CircleBounds, CollisionData},
15358
35c331f9308e fix world test
alfadur
parents: 15310
diff changeset
    76
        common::Millis,
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    77
        physics::{PositionData, VelocityData},
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    78
        World,
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    79
    };
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    80
    use fpnum::{fp, FPNum, FPPoint};
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    81
    use integral_geometry::Size;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    82
    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
    83
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    84
    #[test]
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    85
    fn data_flow() {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    86
        let world_size = Size::new(2048, 2048);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    87
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    88
        let mut world = World::new(world_size);
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    89
        let gear_id = world.new_gear().unwrap();
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    90
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    91
        world.add_gear_data(gear_id, &PositionData(FPPoint::zero()));
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    92
        world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y()));
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    93
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    94
        world.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    95
            gear_id,
15385
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15358
diff changeset
    96
            &CollisionData {
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    97
                bounds: CircleBounds {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    98
                    center: FPPoint::zero(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    99
                    radius: fp!(10),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   100
                },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   101
            },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
   102
        );
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   103
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   104
        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
   105
15358
35c331f9308e fix world test
alfadur
parents: 15310
diff changeset
   106
        world.step(Millis::new(1), &land);
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
   107
    }
14064
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   108
}