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
use std::rc::Rc;
use std::cell::RefCell;
use na;
use ncollide::math::Scalar;
use object::RigidBody;
use math::Point;

/// One of the two end points of a joint.
pub struct Anchor<N: Scalar, P> {
    /// The body attached to this anchor.
    pub body:     Option<Rc<RefCell<RigidBody<N>>>>,
    /// The attach position, in local coordinates of the attached body.
    pub position: P
}

impl<N: Scalar, P> Anchor<N, P> {
    /// Creates a new `Anchor` at a given `position` on a `body` local space.
    ///
    /// If `body` is `None`, the anchor is concidered to be attached to the ground and `position`
    /// is the attach point in global coordinates.
    pub fn new(body: Option<Rc<RefCell<RigidBody<N>>>>, position: P) -> Anchor<N, P> {
        Anchor {
            body:     body,
            position: position
        }
    }
}

impl<N: Scalar, P> Anchor<N, P> {
    /// The center of mass of the body attached to this anchor.
    ///
    /// Returns the zero vector if no body is attached.
    pub fn center_of_mass(&self) -> Point<N> {
        match self.body {
            Some(ref b) => {
                let rb = b.borrow();

                rb.center_of_mass().clone()
            },
            None => na::origin()
        }
    }
}