nphysics2d/material/
basic_material.rs

1use na::RealField;
2
3use crate::material::{LocalMaterialProperties, Material, MaterialCombineMode, MaterialContext};
4
5use crate::math::Vector;
6
7/// Description of the state of surface of a solid.
8///
9/// Strictly speaking, the coefficient provided here only exist
10/// when considering a pair of touching surfaces. In practice, nphysics
11/// will average the coefficient of the two surfaces in contact in order
12/// to deduce the restitution/friction coefficient.
13#[derive(Copy, Clone, Debug)]
14pub struct BasicMaterial<N: RealField + Copy> {
15    /// The ID of this material for automatic lookup.
16    pub id: Option<u32>,
17    /// Restitution coefficient of the surface.
18    pub restitution: N,
19    /// Friction coefficient of the surface.
20    pub friction: N,
21    /// The fictitious velocity at the surface of this material.
22    pub surface_velocity: Option<Vector<N>>,
23    /// The way restitution coefficients are combined if no match
24    /// was found in the material lookup tables.
25    pub restitution_combine_mode: MaterialCombineMode,
26    /// The way friction coefficients are combined if no match
27    /// was found in the material lookup tables.
28    pub friction_combine_mode: MaterialCombineMode,
29}
30
31impl<N: RealField + Copy> BasicMaterial<N> {
32    /// Initialize a material with the specified restitution and friction coefficients.
33    pub fn new(restitution: N, friction: N) -> Self {
34        BasicMaterial {
35            id: None,
36            restitution,
37            friction,
38            surface_velocity: None,
39            restitution_combine_mode: MaterialCombineMode::Average,
40            friction_combine_mode: MaterialCombineMode::Average,
41        }
42    }
43}
44
45impl<N: RealField + Copy> Material<N> for BasicMaterial<N> {
46    fn local_properties(&self, context: MaterialContext<N>) -> LocalMaterialProperties<N> {
47        LocalMaterialProperties {
48            id: self.id,
49            restitution: (self.restitution, self.restitution_combine_mode),
50            friction: (self.friction, self.friction_combine_mode),
51            surface_velocity: self
52                .surface_velocity
53                .map(|v| context.position * v)
54                .unwrap_or_else(Vector::zeros),
55        }
56    }
57}
58
59impl<N: RealField + Copy> Default for BasicMaterial<N> {
60    fn default() -> Self {
61        BasicMaterial::new(N::zero(), na::convert(0.5))
62    }
63}