pub fn move_check_collision<BC: BlockContainer>(&mut self, world: &BC, delta: Vector3<f64>) -> Vector3<f64> {
        if self.intersect_world(world) {
            self.pos += delta;
            return delta;
        }
        let mut res = Vector3::new(0.0, 0.0, 0.0);

        let x_step = (delta.x.abs() / self.size_x).ceil() as u32;
        let y_step = (delta.y.abs() / self.size_y).ceil() as u32;
        let z_step = (delta.z.abs() / self.size_z).ceil() as u32;

        let ddx = delta.x / (x_step as f64);
        let ddy = delta.y / (y_step as f64);
        let ddz = delta.z / (z_step as f64);

        let old_x = self.pos.x;

        for _ in 0..x_step {
            self.pos.x += ddx;
            if self.intersect_world(world) {
                self.pos.x -= ddx; // canceling the last step

                let mut min_d = 0.0;
                let mut max_d = ddx.abs();

                while max_d - min_d > 0.001 {
                    // binary search the max delta
                    let med = (min_d + max_d) / 2.0;
                    self.pos.x += med * ddx.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.x -= med * ddx.signum();
                }

                self.pos.x += ddx.signum() * (min_d) / 2.0;
                break;
            }
        }

        res.x = self.pos.x - old_x;
        let old_y = self.pos.y;

        for _ in 0..y_step {
            self.pos.y += ddy;
            if self.intersect_world(world) {
                self.pos.y -= ddy;
                let mut min_d = 0.0;
                let mut max_d = ddy.abs();

                while max_d - min_d > 0.001 {
                    let med = (min_d + max_d) / 2.0;
                    self.pos.y += med * ddy.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.y -= med * ddy.signum();
                }

                self.pos.y += ddy.signum() * (min_d) / 2.0;
                break;
            }
        }

        res.y = self.pos.y - old_y;
        let old_z = self.pos.z;

        for _ in 0..z_step {
            self.pos.z += ddz;
            if self.intersect_world(world) {
                self.pos.z -= ddz;

                let mut min_d = 0.0;
                let mut max_d = ddz.abs();

                while max_d - min_d > 0.001 {
                    let med = (min_d + max_d) / 2.0;
                    self.pos.z += med * ddz.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.z -= med * ddz.signum();
                }

                self.pos.z += ddz.signum() * (min_d) / 2.0;
                break;
            }
        }

        res.z = self.pos.z - old_z;
        return res;
    }
/// Try to move the box in the world and stop the movement if it goes trough a block
/// Return the actual deplacement
Try to move the box in the world and stop the movement if it goes trough a block
Return the actual deplacement
Try to move the box in the world and stop the movement if it goes trough a block Return the actual deplacement
pub fn move_check_collision<BC: BlockContainer>(&mut self, world: &BC, delta: Vector3<f64>) -> Vector3<f64> {
        if self.intersect_world(world) {
            self.pos += delta;
            return delta;
        }
        let mut res = Vector3::new(0.0, 0.0, 0.0);
        let x_step = (delta.x.abs() / self.size_x).ceil() as u32;
        let y_step = (delta.y.abs() / self.size_y).ceil() as u32;
        let z_step = (delta.z.abs() / self.size_z).ceil() as u32;
        let ddx = delta.x / (x_step as f64);
        let ddy = delta.y / (y_step as f64);
        let ddz = delta.z / (z_step as f64);
        let old_x = self.pos.x;
        for _ in 0..x_step {
            self.pos.x += ddx;
            if self.intersect_world(world) {
                self.pos.x -= ddx; // canceling the last step
                let mut min_d = 0.0;
                let mut max_d = ddx.abs();
                while max_d - min_d > 0.001 {
                    // binary search the max delta
                    let med = (min_d + max_d) / 2.0;
                    self.pos.x += med * ddx.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.x -= med * ddx.signum();
                }
                self.pos.x += ddx.signum() * (min_d) / 2.0;
                break;
            }
        }
        res.x = self.pos.x - old_x;
        let old_y = self.pos.y;
        for _ in 0..y_step {
            self.pos.y += ddy;
            if self.intersect_world(world) {
                self.pos.y -= ddy;
                let mut min_d = 0.0;
                let mut max_d = ddy.abs();
                while max_d - min_d > 0.001 {
                    let med = (min_d + max_d) / 2.0;
                    self.pos.y += med * ddy.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.y -= med * ddy.signum();
                }
                self.pos.y += ddy.signum() * (min_d) / 2.0;
                break;
            }
        }
        res.y = self.pos.y - old_y;
        let old_z = self.pos.z;
        for _ in 0..z_step {
            self.pos.z += ddz;
            if self.intersect_world(world) {
                self.pos.z -= ddz;
                let mut min_d = 0.0;
                let mut max_d = ddz.abs();
                while max_d - min_d > 0.001 {
                    let med = (min_d + max_d) / 2.0;
                    self.pos.z += med * ddz.signum();
                    if self.intersect_world(world) {
                        max_d = med;
                    } else {
                        min_d = med;
                    }
                    self.pos.z -= med * ddz.signum();
                }
                self.pos.z += ddz.signum() * (min_d) / 2.0;
                break;
            }
        }
        res.z = self.pos.z - old_z;
        return res;
    }
pub fn move_check_collision < BC : BlockContainer > ( & mut self , world : & BC , delta : Vector3 < f64 > ) -> Vector3 < f64 > { if self . intersect_world ( world ) { self . pos += delta ; return delta ; } let mut res = Vector3 :: new ( 0.0 , 0.0 , 0.0 ) ; let x_step = ( delta . x . abs ( ) / self . size_x ) . ceil ( ) as u32 ; let y_step = ( delta . y . abs ( ) / self . size_y ) . ceil ( ) as u32 ; let z_step = ( delta . z . abs ( ) / self . size_z ) . ceil ( ) as u32 ; let ddx = delta . x / ( x_step as f64 ) ; let ddy = delta . y / ( y_step as f64 ) ; let ddz = delta . z / ( z_step as f64 ) ; let old_x = self . pos . x ; for _ in 0 .. x_step { self . pos . x += ddx ; if self . intersect_world ( world ) { self . pos . x -= ddx ; // canceling the last step let mut min_d = 0.0 ; let mut max_d = ddx . abs ( ) ; while max_d - min_d > 0.001 { // binary search the max delta let med = ( min_d + max_d ) / 2.0 ; self . pos . x += med * ddx . signum ( ) ; if self . intersect_world ( world ) { max_d = med ; } else { min_d = med ; } self . pos . x -= med * ddx . signum ( ) ; } self . pos . x += ddx . signum ( ) * ( min_d ) / 2.0 ; break ; } } res . x = self . pos . x - old_x ; let old_y = self . pos . y ; for _ in 0 .. y_step { self . pos . y += ddy ; if self . intersect_world ( world ) { self . pos . y -= ddy ; let mut min_d = 0.0 ; let mut max_d = ddy . abs ( ) ; while max_d - min_d > 0.001 { let med = ( min_d + max_d ) / 2.0 ; self . pos . y += med * ddy . signum ( ) ; if self . intersect_world ( world ) { max_d = med ; } else { min_d = med ; } self . pos . y -= med * ddy . signum ( ) ; } self . pos . y += ddy . signum ( ) * ( min_d ) / 2.0 ; break ; } } res . y = self . pos . y - old_y ; let old_z = self . pos . z ; for _ in 0 .. z_step { self . pos . z += ddz ; if self . intersect_world ( world ) { self . pos . z -= ddz ; let mut min_d = 0.0 ; let mut max_d = ddz . abs ( ) ; while max_d - min_d > 0.001 { let med = ( min_d + max_d ) / 2.0 ; self . pos . z += med * ddz . signum ( ) ; if self . intersect_world ( world ) { max_d = med ; } else { min_d = med ; } self . pos . z -= med * ddz . signum ( ) ; } self . pos . z += ddz . signum ( ) * ( min_d ) / 2.0 ; break ; } } res . z = self . pos . z - old_z ; return res ; }
Try to move the box in the world and stop the movement if it goes trough a block
Return the actual deplacement
Try to move the box in the world and stop the movement if it goes trough a block Return the actual deplacement
&mut self
world
&BC
delta
Vector3<f64>
&mut self
world
&BC
delta
Vector3<f64>