1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use super::super::{HasSteeringBehavior, SteeringAcceleration, SteeringAccelerationCalculator,
                   SteeringBehavior};
use nalgebra::{distance, Point3};
use alga::general::Real;
use alga::general::AbstractModule;
use std::cell::RefMut;
use std::cell::RefCell;
use std::rc::Rc;

///This behavior aims to arrive at target position and stop. It is like Seek
///behavior but aiming to be at zero speed on target.
#[builder(pattern = "immutable")]
#[derive(Builder)]
pub struct Arrive<T>
where
    T: Real,
{
    /// Common behavior attributes
    pub behavior: RefCell<SteeringBehavior<T>>,
    /// Stop if we are close enough.
    pub tolerance: T,
    /// Reduce the speed if we are close enough
    pub deceleration_radius: T,
    /// How quick should we aproach target.
    pub time_to_target: T,
}


impl<T: Real> HasSteeringBehavior<T> for Arrive<T> {
    fn get_steering_behavior(&mut self) -> RefMut<SteeringBehavior<T>> {
        self.behavior.borrow_mut()
    }
}

impl<T: Real> SteeringAccelerationCalculator<T> for Arrive<T> {
    fn calculate_real_steering(
        &self,
        steering_acceleration: Rc<RefCell<SteeringAcceleration<T>>>,
    ) -> Rc<RefCell<SteeringAcceleration<T>>> {
        let behavior = self.behavior.borrow();
        let mut sa = steering_acceleration.borrow_mut();
        sa.linear = *behavior.target.borrow().get_position() -
            *behavior.owner.borrow().get_position();
        let to_target = distance(&Point3::from_coordinates(sa.linear), &Point3::origin());

        if to_target <= self.tolerance {
            sa.set_zero();
        }
        let mut target_speed = match self.behavior.borrow().limiter {
            Some(ref lim) => lim.borrow().get_max_linear_speed(),
            None => T::one(),
        };
        if to_target <= self.deceleration_radius {
            target_speed *= to_target / self.deceleration_radius;
        }
        sa.linear = sa.linear.multiply_by(target_speed / to_target);
        sa.linear -= *behavior.owner.borrow().get_linear_velocity();
        sa.linear = sa.linear.multiply_by(T::one() / self.time_to_target);
        sa.angular = T::zero();
        steering_acceleration.clone()
    }
}