rust/hwphysics/src/lib.rs
author nemo
Mon, 14 Jan 2019 15:01:43 -0500
branch0.9.25
changeset 14588 e27f1d1c1da9
parent 14179 abbb74b9cb62
child 14716 8e74d4eb89f5
permissions -rw-r--r--
aaagh can't just move delete like that due to our RNG scrambling... reverting on branch. we'll just have to take our chances w/ the bad code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     1
mod common;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     2
mod physics;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     3
mod grid;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
     4
mod collision;
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::{
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    11
    common::{
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    12
        GearId,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    13
        GearData,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    14
        GearDataAggregator,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    15
        GearDataProcessor
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    16
    },
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    17
    physics::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    18
        PhysicsProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    19
        PhysicsData
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    20
    },
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    21
    collision::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    22
        CollisionProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    23
        CollisionData,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    24
        ContactData
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    25
    }
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    26
};
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    27
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    28
pub struct JoinedData {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    29
    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
    30
    physics: PhysicsData,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    31
    collision: CollisionData,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    32
    contact: ContactData
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    33
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    34
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    35
pub struct World {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    36
    physics: PhysicsProcessor,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    37
    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
    38
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    39
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    40
macro_rules! processor_map {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    41
    ( $data_type: ident => $field: ident ) => {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    42
        impl GearDataAggregator<$data_type> for World {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    43
            fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    44
                &mut self.$field
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    45
            }
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
}
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    49
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    50
processor_map!(PhysicsData => physics);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    51
processor_map!(CollisionData => collision);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    52
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    53
impl World {
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    54
    pub fn new(world_size: Size) -> Self {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    55
        Self {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    56
            physics: PhysicsProcessor::new(),
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    57
            collision: CollisionProcessor::new(world_size)
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    58
        }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    59
    }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    60
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    61
    pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
14178
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    62
        let updates = self.physics.process(time_step);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14144
diff changeset
    63
        self.collision.process(land, &updates);
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    64
    }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    65
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    66
    pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T)
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    67
        where T: GearData,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    68
              Self: GearDataAggregator<T>
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    69
    {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    70
        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
    71
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    72
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    73
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    74
#[cfg(test)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    75
mod tests {
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    76
    use crate::{
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    77
        World,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    78
        physics::PhysicsData,
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    79
        collision::{CollisionData, CircleBounds}
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    80
    };
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    81
    use fpnum::{FPNum, FPPoint, fp};
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    82
    use integral_geometry::Size;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    83
    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
    84
14179
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    85
    #[test]
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    86
    fn data_flow() {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    87
        let world_size = Size::new(2048, 2048);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    88
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    89
        let mut world = World::new(world_size);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    90
        let gear_id = 46631;
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    91
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    92
        world.add_gear_data(gear_id, PhysicsData {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    93
            position: FPPoint::zero(),
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    94
            velocity: FPPoint::unit_y()
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    95
        });
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    96
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    97
        world.add_gear_data(gear_id, CollisionData {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    98
            bounds: CircleBounds {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
    99
                center: FPPoint::zero(),
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   100
                radius: fp!(10)
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   101
            }
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   102
        });
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   103
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
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: 14178
diff changeset
   105
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   106
        world.step(fp!(1), &land);
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14178
diff changeset
   107
    }
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   108
}