rust/hwphysics/src/physics.rs
author alfadur
Fri, 09 Nov 2018 01:05:34 +0300
changeset 14199 a4c17cfaa4c9
child 14200 abbb74b9cb62
permissions -rw-r--r--
split hwphysics into modules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14199
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     1
use crate::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     2
    common::GearId
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     3
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     4
use fpnum::*;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     5
use integral_geometry::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     6
    Point, Size, GridIndex
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     7
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     8
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     9
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    10
pub struct PhysicsData {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    11
    pub position: FPPoint,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    12
    pub velocity: FPPoint,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    13
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    14
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    15
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    16
pub struct DynamicPhysicsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    17
    gear_ids: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    18
    positions: Vec<FPPoint>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    19
    velocities: Vec<FPPoint>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    20
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    21
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    22
impl DynamicPhysicsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    23
    fn len(&self) -> usize {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    24
        self.gear_ids.len()
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    25
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    26
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    27
    fn push(&mut self, id: GearId, physics: PhysicsData) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    28
        self.gear_ids.push(id);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    29
        self.positions.push(physics.position);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    30
        self.velocities.push(physics.velocity);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    31
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    32
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    33
    fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    34
        self.gear_ids.iter().cloned()
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    35
            .zip(self.positions.iter_mut()
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    36
                .zip(self.velocities.iter()))
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    37
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    38
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    39
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    40
pub struct StaticPhysicsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    41
    gear_ids: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    42
    positions: Vec<FPPoint>
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    43
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    44
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    45
impl StaticPhysicsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    46
    fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    47
        self.gear_ids.push(gear_id);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    48
        self.positions.push(physics.position);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    49
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    50
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    51
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    52
pub struct PhysicsProcessor {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    53
    dynamic_physics: DynamicPhysicsCollection,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    54
    static_physics: StaticPhysicsCollection,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    55
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    56
    physics_cleanup: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    57
    position_updates: PositionUpdate
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    58
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    59
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    60
pub struct PositionUpdate {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    61
    pub gear_ids: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    62
    pub positions: Vec<FPPoint>
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    63
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    64
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    65
impl PositionUpdate {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    66
    pub fn new(capacity: usize) -> Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    67
        Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    68
            gear_ids: Vec::with_capacity(capacity),
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    69
            positions: Vec::with_capacity(capacity),
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    70
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    71
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    72
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    73
    pub fn push(&mut self, gear_id: GearId, position: &FPPoint) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    74
        self.gear_ids.push(gear_id);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    75
        self.positions.push(*position);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    76
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    77
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    78
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    79
impl PhysicsProcessor {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    80
    pub fn process(&mut self, time_step: FPNum) -> &PositionUpdate {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    81
        for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    82
            *pos += *vel * time_step;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    83
            if !vel.is_zero() {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    84
                self.position_updates.push(gear_id, pos)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    85
            } else {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    86
                self.physics_cleanup.push(gear_id)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    87
            }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    88
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    89
        &self.position_updates
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    90
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    91
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    92
    pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    93
        if physics_data.velocity.is_zero() {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    94
            self.static_physics.push(gear_id, physics_data);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    95
        } else {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    96
            self.dynamic_physics.push(gear_id, physics_data);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    97
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    98
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    99
}