aboutsummaryrefslogtreecommitdiff
path: root/src/dynamics/solver/generic_velocity_ground_constraint.rs
blob: 91bb9fb7d49a87180a72a6940c826fc8ed07c69c (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::dynamics::solver::VelocityGroundConstraint;
use crate::dynamics::{IntegrationParameters, MultibodyJointSet, RigidBodySet, RigidBodyVelocity};
use crate::geometry::{ContactManifold, ContactManifoldIndex};
use crate::math::{Point, Real, DIM, MAX_MANIFOLD_POINTS};
use crate::utils::WCross;

use super::{
    AnyVelocityConstraint, VelocityGroundConstraintElement, VelocityGroundConstraintNormalPart,
};
#[cfg(feature = "dim2")]
use crate::utils::WBasis;
use na::DVector;

#[derive(Copy, Clone, Debug)]
pub(crate) struct GenericVelocityGroundConstraint {
    // We just build the generic constraint on top of the velocity constraint,
    // adding some information we can use in the generic case.
    pub velocity_constraint: VelocityGroundConstraint,
    pub j_id: usize,
    pub ndofs2: usize,
}

impl GenericVelocityGroundConstraint {
    pub fn generate(
        params: &IntegrationParameters,
        manifold_id: ContactManifoldIndex,
        manifold: &ContactManifold,
        bodies: &RigidBodySet,
        multibodies: &MultibodyJointSet,
        out_constraints: &mut Vec<AnyVelocityConstraint>,
        jacobians: &mut DVector<Real>,
        jacobian_id: &mut usize,
        insert_at: Option<usize>,
    ) {
        let cfm_factor = params.cfm_factor();
        let inv_dt = params.inv_dt();
        let erp_inv_dt = params.erp_inv_dt();

        let mut handle1 = manifold.data.rigid_body1;
        let mut handle2 = manifold.data.rigid_body2;
        let flipped = manifold.data.relative_dominance < 0;

        let (force_dir1, flipped_multiplier) = if flipped {
            std::mem::swap(&mut handle1, &mut handle2);
            (manifold.data.normal, -1.0)
        } else {
            (-manifold.data.normal, 1.0)
        };

        let (vels1, world_com1) = if let Some(handle1) = handle1 {
            let rb1 = &bodies[handle1];
            (rb1.vels, rb1.mprops.world_com)
        } else {
            (RigidBodyVelocity::zero(), Point::origin())
        };

        let rb2 = &bodies[handle2.unwrap()];
        let (vels2, mprops2) = (&rb2.vels, &rb2.mprops);

        let (mb2, link_id2) = handle2
            .and_then(|h| multibodies.rigid_body_link(h))
            .map(|m| (&multibodies[m.multibody], m.id))
            .unwrap();
        let mj_lambda2 = mb2.solver_id;

        #[cfg(feature = "dim2")]
        let tangents1 = force_dir1.orthonormal_basis();
        #[cfg(feature = "dim3")]
        let tangents1 =
            super::compute_tangent_contact_directions(&force_dir1, &vels1.linvel, &vels2.linvel);

        let multibodies_ndof = mb2.ndofs();
        // For each solver contact we generate DIM constraints, and each constraints appends
        // the multibodies jacobian and weighted jacobians
        let required_jacobian_len =
            *jacobian_id + manifold.data.solver_contacts.len() * multibodies_ndof * 2 * DIM;

        if jacobians.nrows() < required_jacobian_len && !cfg!(feature = "parallel") {
            jacobians.resize_vertically_mut(required_jacobian_len, 0.0);
        }

        for (_l, manifold_points) in manifold
            .data
            .solver_contacts
            .chunks(MAX_MANIFOLD_POINTS)
            .enumerate()
        {
            let chunk_j_id = *jacobian_id;
            let mut is_fast_contact = false;
            let mut constraint = VelocityGroundConstraint {
                dir1: force_dir1,
                #[cfg(feature = "dim3")]
                tangent1: tangents1[0],
                elements: [VelocityGroundConstraintElement::zero(); MAX_MANIFOLD_POINTS],
                im2: mprops2.effective_inv_mass,
                cfm_factor,
                limit: 0.0,
                mj_lambda2,
                manifold_id,
                manifold_contact_id: [0; MAX_MANIFOLD_POINTS],
                num_contacts: manifold_points.len() as u8,
            };

            for k in 0..manifold_points.len() {
                let manifold_point = &manifold_points[k];
                let dp1 = manifold_point.point - world_com1;
                let dp2 = manifold_point.point - mprops2.world_com;

                let vel1 = vels1.linvel + vels1.angvel.gcross(dp1);
                let vel2 = vels2.linvel + vels2.angvel.gcross(dp2);

                constraint.limit = manifold_point.friction;
                constraint.manifold_contact_id[k] = manifold_point.contact_id;

                // Normal part.
                {
                    let torque_dir2 = dp2.gcross(-force_dir1);
                    let inv_r2 = mb2
                        .fill_jacobians(
                            link_id2,
                            -force_dir1,
                            #[cfg(feature = "dim2")]
                            na::vector!(torque_dir2),
                            #[cfg(feature = "dim3")]
                            torque_dir2,
                            jacobian_id,
                            jacobians,
                        )
                        .0;

                    let r = crate::utils::inv(inv_r2);

                    let is_bouncy = manifold_point.is_bouncy() as u32 as Real;
                    let is_resting = 1.0 - is_bouncy;

                    let dvel = (vel1 - vel2).dot(&force_dir1);
                    let mut rhs_wo_bias = (1.0 + is_bouncy * manifold_point.restitution) * dvel;
                    rhs_wo_bias += manifold_point.dist.max(0.0) * inv_dt;
                    rhs_wo_bias *= is_bouncy + is_resting;
                    let rhs_bias =
                        /* is_resting * */ erp_inv_dt * manifold_point.dist.clamp(-params.max_penetration_correction, 0.0);

                    let rhs = rhs_wo_bias + rhs_bias;
                    is_fast_contact =
                        is_fast_contact || (-rhs * params.dt > rb2.ccd.ccd_thickness * 0.5);

                    constraint.elements[k].normal_part = VelocityGroundConstraintNormalPart {
                        gcross2: na::zero(), // Unused for generic constraints.
                        rhs,
                        rhs_wo_bias,
                        impulse: na::zero(),
                        r,
                    };
                }

                // Tangent parts.
                {
                    constraint.elements[k].tangent_part.impulse = na::zero();

                    for j in 0..DIM - 1 {
                        let torque_dir2 = dp2.gcross(-tangents1[j]);
                        let inv_r2 = mb2
                            .fill_jacobians(
                                link_id2,
                                -tangents1[j],
                                #[cfg(feature = "dim2")]
                                na::vector![torque_dir2],
                                #[cfg(feature = "dim3")]
                                torque_dir2,
                                jacobian_id,
                                jacobians,
                            )
                            .0;

                        let r = crate::utils::inv(inv_r2);

                        let rhs = (vel1 - vel2
                            + flipped_multiplier * manifold_point.tangent_velocity)
                            .dot(&tangents1[j]);

                        constraint.elements[k].tangent_part.rhs[j] = rhs;

                        // FIXME: in 3D, we should take into account gcross[0].dot(gcross[1])
                        // in lhs. See the corresponding code on the `velocity_constraint.rs`
                        // file.
                        constraint.elements[k].tangent_part.r[j] = r;
                    }
                }
            }

            constraint.cfm_factor = if is_fast_contact { 1.0 } else { cfm_factor };

            let constraint = GenericVelocityGroundConstraint {
                velocity_constraint: constraint,
                j_id: chunk_j_id,
                ndofs2: mb2.ndofs(),
            };

            if let Some(at) = insert_at {
                out_constraints[at + _l] =
                    AnyVelocityConstraint::NongroupedGenericGround(constraint);
            } else {
                out_constraints.push(AnyVelocityConstraint::NongroupedGenericGround(constraint));
            }
        }
    }

    pub fn solve(
        &mut self,
        jacobians: &DVector<Real>,
        generic_mj_lambdas: &mut DVector<Real>,
        solve_restitution: bool,
        solve_friction: bool,
    ) {
        let mj_lambda2 = self.velocity_constraint.mj_lambda2 as usize;

        let elements = &mut self.velocity_constraint.elements
            [..self.velocity_constraint.num_contacts as usize];
        VelocityGroundConstraintElement::generic_solve_group(
            self.velocity_constraint.cfm_factor,
            elements,
            jacobians,
            self.velocity_constraint.limit,
            self.ndofs2,
            self.j_id,
            mj_lambda2,
            generic_mj_lambdas,
            solve_restitution,
            solve_friction,
        );