Blender V5.3
GEO_xpbd_constraint_friction_edge.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
10namespace blender::xpbd {
11
13 : public TemplatedVelocityConstraintSet<FrictionEdgeConstraintSet> {
14 private:
15 int geo_i_;
16 /* Constraint index for each point. */
17 Span<int2> point_pairs_;
18 Span<float3> separating_axes_;
19 Span<float3> contact_velocities_;
20 /* Constraint multiplier lambda for the normal displacement divided by time step. */
21 Span<float> dynamic_friction_terms_;
22 Span<float> lambdas_normal_;
23 Span<float> point_mix_factors_;
24 MutableSpan<float> lambdas_;
25
26 public:
27 static constexpr StringRefNull debug_name = "Friction";
28
30 const Span<int2> point_pairs,
31 const Span<float3> separating_axes,
32 const Span<float3> contact_velocities,
33 const Span<float> dynamic_friction_terms,
34 const Span<float> lambdas_normal,
35 const Span<float> point_mix_factors,
36 MutableSpan<float> lambdas)
37 : TemplatedVelocityConstraintSet<FrictionEdgeConstraintSet>(point_pairs.size(), {geo_i}),
38 geo_i_(geo_i),
39 point_pairs_(point_pairs),
40 separating_axes_(separating_axes),
41 contact_velocities_(contact_velocities),
42 dynamic_friction_terms_(dynamic_friction_terms),
43 lambdas_normal_(lambdas_normal),
44 point_mix_factors_(point_mix_factors),
45 lambdas_(lambdas)
46 {
47 }
48
49 void reset_force(const int constraint_i) const
50 {
51 lambdas_[constraint_i] = 0.0f;
52 }
53
54 template<typename UpdaterT>
56 UpdaterT &updater,
57 const int constraint_i) const
58 {
59 const int2 point_pair = point_pairs_[constraint_i];
60 const float segment_factor = point_mix_factors_[constraint_i];
61
62 /* Effective weight/mass */
63 const float inv_m0 = params.inv_mass(geo_i_, point_pair[0]);
64 const float inv_m1 = params.inv_mass(geo_i_, point_pair[1]);
65 const float weight0 = (1.0f - segment_factor) * inv_m0;
66 const float weight1 = segment_factor * inv_m1;
67 /* Should be inactive if both are zero. */
68 BLI_assert(weight0 > 0.0f || weight1 > 0.0f);
69
70 const float dynamic_friction = dynamic_friction_terms_[constraint_i] *
72 const float lambda_normal = lambdas_normal_[constraint_i];
73 const float3 &axis = separating_axes_[constraint_i];
74 const float3 &contact_velocity = contact_velocities_[constraint_i];
75 const float3 &velocity = math::interpolate(params.velocity(geo_i_, point_pair[0]),
76 params.velocity(geo_i_, point_pair[1]),
77 segment_factor) -
78 contact_velocity;
79 const float3 velocity_tangent = velocity - math::dot(velocity, axis) * axis;
80 float residual;
81 const float3 gradient = math::normalize_and_get_length(velocity_tangent, residual);
82 const float delta_lambda = std::min(dynamic_friction * lambda_normal,
83 residual / (weight0 + weight1));
84
85 lambdas_[constraint_i] += delta_lambda;
86 updater.update_velocity(geo_i_, point_pair[0], -weight0 * gradient * delta_lambda);
87 updater.update_velocity(geo_i_, point_pair[1], -weight1 * gradient * delta_lambda);
88 }
89};
90
91} // namespace blender::xpbd
const float3 & velocity(int geo_i, int point_i) const
float inv_mass(int geo_i, int point_i) const
void solve_single(const ConstraintSetParams &params, UpdaterT &updater, const int constraint_i) const
FrictionEdgeConstraintSet(const int geo_i, const Span< int2 > point_pairs, const Span< float3 > separating_axes, const Span< float3 > contact_velocities, const Span< float > dynamic_friction_terms, const Span< float > lambdas_normal, const Span< float > point_mix_factors, MutableSpan< float > lambdas)
TemplatedVelocityConstraintSet(int constraints_num, Vector< int > affected_geo_indices)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T interpolate(const T &a, const T &b, const FactorT &t)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< int32_t, 2 > int2
VecBase< float, 3 > float3