rust/hwphysics/src/lib.rs
author alfadur
Tue, 06 Nov 2018 19:44:20 +0300
changeset 14144 165e43c3ed59
parent 14059 c6745a1c827a
child 14178 a4c17cfaa4c9
permissions -rw-r--r--
pull land into collision detector
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     1
use std::{
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     2
    ops::RangeInclusive
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     3
};
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     4
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
     5
use fpnum::*;
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
     6
use integral_geometry::{
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
     7
    Point, Size, GridIndex
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
     8
};
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
     9
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
    10
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    11
type Index = u16;
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    12
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    13
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    14
struct PhysicsData {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    15
    position: FPPoint,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    16
    velocity: FPPoint,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    17
}
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
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    20
struct  CollisionData {
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    21
    bounds: CircleBounds
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
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    25
struct ContactData {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    26
    elasticity: FPNum,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    27
    friction: FPNum
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    28
}
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
pub struct PhysicsCollection {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    31
    positions: Vec<FPPoint>,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    32
    velocities: Vec<FPPoint>
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
impl PhysicsCollection {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    36
    fn push(&mut self, data: PhysicsData) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    37
        self.positions.push(data.position);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    38
        self.velocities.push(data.velocity);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    39
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    40
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    41
    fn iter_mut_pos(&mut self) -> impl Iterator<Item = (&mut FPPoint, &FPPoint)> {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    42
        self.positions.iter_mut().zip(self.velocities.iter())
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    43
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    44
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    45
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    46
pub struct JoinedData {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    47
    physics: PhysicsData,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    48
    collision: CollisionData,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    49
    contact: ContactData
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    50
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    51
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    52
pub struct World {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    53
    enabled_physics: PhysicsCollection,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    54
    disabled_physics: Vec<PhysicsData>,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    55
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    56
    enabled_collision: Vec<CollisionData>,
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    57
    disabled_collision: Vec<CollisionData>,
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    58
    grid: Grid,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    59
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    60
    physics_cleanup: Vec<PhysicsData>,
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    61
    collision_output: Vec<(Index, Index)>,
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    62
    land_collision_output: Vec<Index>
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    63
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    64
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    65
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    66
struct CircleBounds {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    67
    center: FPPoint,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    68
    radius: FPNum
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
impl CircleBounds {
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    72
    pub fn intersects(&self, other: &CircleBounds) -> bool {
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    73
        (other.center - self.center).is_in_range(self.radius + other.radius)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    74
    }
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    75
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    76
    pub fn rows(&self) -> impl Iterator<Item = (usize, RangeInclusive<usize>)> {
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    77
        let radius = self.radius.abs_round() as usize;
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    78
        let center = Point::from_fppoint(&self.center);
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    79
        (center.y as usize - radius..=center.y as usize + radius)
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    80
            .map(move |row| (row, center.x as usize - radius..=center.x as usize + radius))
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
    81
    }
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    82
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    83
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    84
fn fppoint_round(point: &FPPoint) -> Point {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    85
    Point::new(point.x().round() as i32, point.y().round() as i32)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    86
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    87
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    88
struct GridBin {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    89
    refs: Vec<Index>,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    90
    static_entries: Vec<CircleBounds>,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    91
    dynamic_entries: Vec<CircleBounds>
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    92
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    93
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    94
impl GridBin {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    95
    fn new() -> Self {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    96
        Self {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    97
            refs: vec![],
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    98
            static_entries: vec![],
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
    99
            dynamic_entries: vec![]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   100
        }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   101
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   102
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   103
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   104
const GRID_BIN_SIZE: usize = 256;
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   105
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   106
struct Grid {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   107
    bins: Vec<GridBin>,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   108
    space_size: Size,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   109
    bins_count: Size,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   110
    index: GridIndex
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   111
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   112
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   113
impl Grid {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   114
    fn new(size: Size) -> Self {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   115
        assert!(size.is_power_of_two());
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   116
        let bins_count =
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   117
            Size::new(size.width / GRID_BIN_SIZE,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   118
                      size.height / GRID_BIN_SIZE);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   119
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   120
        Self {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   121
            bins: (0..bins_count.area()).map(|_| GridBin::new()).collect(),
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   122
            space_size: size,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   123
            bins_count,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   124
            index: Size::square(GRID_BIN_SIZE).to_grid_index()
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   125
        }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   126
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   127
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   128
    fn bin_index(&self, position: &FPPoint) -> Point {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   129
        self.index.map(fppoint_round(position))
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   130
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   131
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   132
    fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   133
        let index = self.bin_index(position);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   134
        &mut self.bins[index.x as usize * self.bins_count.width + index.y as usize]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   135
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   136
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   137
    fn insert_static(&mut self, index: Index, position: &FPPoint, bounds: &CircleBounds) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   138
        self.lookup_bin(position).static_entries.push(*bounds)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   139
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   140
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   141
    fn insert_dynamic(&mut self, index: Index, position: &FPPoint, bounds: &CircleBounds) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   142
        self.lookup_bin(position).dynamic_entries.push(*bounds)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   143
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   144
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   145
    fn check_collisions(&self, collisions: &mut Vec<(Index, Index)>) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   146
        for bin in &self.bins {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   147
            for bounds in &bin.dynamic_entries {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   148
                for other in &bin.dynamic_entries {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   149
                    if bounds.intersects(other) && bounds != other {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   150
                        collisions.push((0, 0))
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   151
                    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   152
                }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   153
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   154
                for other in &bin.static_entries {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   155
                    if bounds.intersects(other) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   156
                        collisions.push((0, 0))
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   157
                    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   158
                }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   159
            }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   160
        }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   161
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   162
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   163
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   164
impl World {
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   165
    pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   166
        for (pos, vel) in self.enabled_physics.iter_mut_pos() {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   167
            *pos += *vel
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   168
        }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   169
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   170
        self.grid.check_collisions(&mut self.collision_output);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   171
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   172
14144
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   173
    fn check_land_collisions(&mut self, land: &Land2D<u32>) {
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   174
        for collision in &self.enabled_collision {
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   175
            if collision.bounds.rows().any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0)) {
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   176
                self.land_collision_output.push(0)
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   177
            }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   178
        }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   179
    }
165e43c3ed59 pull land into collision detector
alfadur
parents: 14059
diff changeset
   180
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   181
    pub fn add_gear(&mut self, data: JoinedData) {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   182
        if data.physics.velocity == FPPoint::zero() {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   183
            self.disabled_physics.push(data.physics);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   184
            self.grid.insert_static(0, &data.physics.position, &data.collision.bounds);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   185
        } else {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   186
            self.enabled_physics.push(data.physics);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   187
            self.grid.insert_dynamic(0, &data.physics.position, &data.collision.bounds);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   188
        }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   189
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   190
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   191
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   192
#[cfg(test)]
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   193
mod tests {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   194
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
diff changeset
   195
}