rust/hwphysics/src/lib.rs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 28 May 2019 22:07:56 +0200
changeset 15076 527a7414bb44
parent 14716 8e74d4eb89f5
child 15266 b58f98bbc120
permissions -rw-r--r--
ACF1: Fix 2 crate spawns being very close to each other in the difficult rope challenge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
     1
pub mod collision;
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
     2
pub mod common;
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     3
mod grid;
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
     4
pub mod physics;
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     5
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     6
use fpnum::FPNum;
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
     7
use integral_geometry::Size;
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     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
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    10
use crate::{
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    11
    collision::{CollisionData, CollisionProcessor, ContactData},
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    12
    common::{GearData, GearDataAggregator, GearDataProcessor, GearId},
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    13
    physics::{PhysicsData, PhysicsProcessor},
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    14
};
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    15
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    16
pub struct JoinedData {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    17
    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
    18
    physics: PhysicsData,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    19
    collision: CollisionData,
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    20
    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
    21
}
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
pub struct World {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    24
    physics: PhysicsProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    25
    collision: CollisionProcessor,
14059
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
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    28
macro_rules! processor_map {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    29
    ( $data_type: ident => $field: ident ) => {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    30
        impl GearDataAggregator<$data_type> for World {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    31
            fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    32
                &mut self.$field
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    33
            }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    34
        }
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    35
    };
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    36
}
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    37
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    38
processor_map!(PhysicsData => physics);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    39
processor_map!(CollisionData => collision);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    40
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    41
impl World {
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    42
    pub fn new(world_size: Size) -> Self {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    43
        Self {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    44
            physics: PhysicsProcessor::new(),
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    45
            collision: CollisionProcessor::new(world_size),
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    46
        }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    47
    }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    48
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    49
    pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    50
        let updates = self.physics.process(time_step);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    51
        self.collision.process(land, &updates);
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    52
    }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    53
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    54
    pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T)
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    55
    where
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    56
        T: GearData,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    57
        Self: GearDataAggregator<T>,
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    58
    {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    59
        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
    60
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    61
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    62
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    63
#[cfg(test)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    64
mod tests {
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    65
    use crate::{
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    66
        collision::{CircleBounds, CollisionData},
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    67
        physics::PhysicsData,
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    68
        World,
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    69
    };
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    70
    use fpnum::{fp, FPNum, FPPoint};
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    71
    use integral_geometry::Size;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    72
    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
    73
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    74
    #[test]
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    75
    fn data_flow() {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    76
        let world_size = Size::new(2048, 2048);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    77
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    78
        let mut world = World::new(world_size);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    79
        let gear_id = 46631;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    80
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    81
        world.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    82
            gear_id,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    83
            PhysicsData {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    84
                position: FPPoint::zero(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    85
                velocity: FPPoint::unit_y(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    86
            },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    87
        );
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    88
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    89
        world.add_gear_data(
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    90
            gear_id,
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    91
            CollisionData {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    92
                bounds: CircleBounds {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    93
                    center: FPPoint::zero(),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    94
                    radius: fp!(10),
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    95
                },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    96
            },
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14179
diff changeset
    97
        );
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    98
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    99
        let land = Land2D::new(Size::new(world_size.width - 2, world_size.height - 2), 0);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   100
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   101
        world.step(fp!(1), &land);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   102
    }
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   103
}