rustbatch 0.4.0

purely game dewelopment crate that offers simple but powerfull 2D rendering and some fast solutions for game world bottle necks
Documentation
/// tis module offers shooting prediction for 2D games. If you input correct data about shooter and
/// target functions below will return position where shooter should shoot.
use crate::Vect;

pub trait Entity {
    /// returns actual position of shooter or target
    fn position(&self) -> Vect;
    /// returns actual velocity of shooter or target
    /// in case of shooter you should provide Vect::ZERO if shooters speed does not affect the
    /// bullet, if yes then return the velocity you add to bullet
    fn velocity(&self) -> Vect;
}

pub trait Shooter: Entity {
    /// returns the bullet speed of shooters armament.
    /// If shooters movement affects bullets velocity then use Entity::velocity to provide
    /// difference
    fn bullet_speed(&self) -> f32;
}

/// predict returns None if its not possible to hit the target, otherwise you ll get the exact point
/// of future collision if you fire at the exact moment and targets velocity does not change while
/// the bullet is traveling to it
#[inline]
pub fn predict<S: Shooter, T: Entity>(shooter: &S, target: &T) -> Option<Vect> {
    low_predict(shooter.position(), shooter.bullet_speed(), target.velocity() - shooter.velocity(), target.position())
}

/// if you aren't comfortable with trait s you can pass a raw data.
#[inline]
pub fn low_predict(s_pos: Vect, s_speed: f32, t_vel: Vect, t_pos: Vect ) -> Option<Vect> {
    let d = t_pos - s_pos;

    let mut a = t_vel.x * t_vel.x + t_vel.y * t_vel.y - s_speed * s_speed;
    let b = 2.0 * (d.x * t_vel.x + d.y * t_vel.y);
    let c = d.x * d.x + d.y * d.y;

    let cof = b * b - 4.0 * a * c;

    if cof < 0.0 {
        return None
    }

    let dis = cof.sqrt();
    a *= 2.0;
    let (t1, t2) = ((-b + dis) / a, (-b - dis) / a);

    if t1 >= 0.0 && (t1 < t2 || t2 < 0.0) {
        return Some(t_pos + t_vel * t1);
    }

    Some(t_pos + t_vel * t2)
}