SuperDex Physics C++ API
Loading...
Searching...
No Matches
contact_params.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19// PLEASE DO NOT ADD OTHER INCLUDES HERE. This header is included in the mochi_physics public API.
22
23namespace superdex {
24
25/**
26 * @brief Parameters for contact mechanics simulation.
27 *
28 * @note In contact between a colliding actor and a collider, the collider's contact parameters
29 * (not the colliding actor's) are used. The exceptions are:
30 * - For friction and dissipation coefficients (viscousFrictionCoefficient,
31 * coulombFrictionCoefficient, normalViscousDampingCoefficient), the geometric mean of the
32 * colliding and collider's coefficients is used. This disables friction/dissipation if either
33 * of them does.
34 * - For penalty coefficient (penaltyCoefficient) and friction velocity threshold
35 * (frictionFalloffVel), the geometric mean of the colliding and collider's values is used,
36 * except if the collider is static in which case the colliding's values are used.
37 */
39 ContactParams() = default;
40
41 /**
42 * @brief Construct ContactParams with all parameters explicitly specified.
43 *
44 * @param[in] penaltyCoefficient See @ref penaltyCoefficient.
45 * @param[in] penaltySmoothingHalfDistance See @ref penaltySmoothingHalfDistance.
46 * @param[in] penaltyThresholdDefault See @ref penaltyThresholdDefault.
47 * @param[in] penaltyThresholdExtraPadding See @ref penaltyThresholdExtraPadding.
48 * @param[in] frictionWithColliderNormal See @ref frictionWithColliderNormal.
49 * @param[in] maxAlignmentNormals See @ref maxAlignmentNormals.
50 * @param[in] viscousFrictionCoefficient See @ref viscousFrictionCoefficient.
51 * @param[in] coulombFrictionCoefficient See @ref coulombFrictionCoefficient.
52 * @param[in] frictionFalloffVel See @ref frictionFalloffVel.
53 * @param[in] normalViscousDampingCoefficient See @ref normalViscousDampingCoefficient.
54 * @param[in] distanceErrorBound See @ref distanceErrorBound.
55 * @param[in] objScale See @ref objScale.
56 * @param[in] collidingPenaltyLengthScale See @ref collidingPenaltyLengthScale.
57 *
58 * @note Must take every parameter in order.
59 * @note Used when converting types to/from the C API.
60 */
75
76 // Parameters for penalty contact.
77
78 /**
79 * @brief Stiffness of the contact penalty force [Pa/m].
80 *
81 * @note Must be strictly positive.
82 * @note Higher penalties create stiffer contacts and reduce penetration.
83 * @note Arbitrarily large penalties may degrade stability.
84 * @note The default penalty is appropriate for actors with default density. For actors with much
85 * higher/lower density than the default density, the penalty coefficient may need to be
86 * increased/decreased accordingly.
87 * @note The penalty coefficient used in a collision is the geometric mean of the colliding and
88 * collider's coefficients. The exception is if the collider is static, in which case the
89 * colliding's penalty is used.
90 * @note The penalty coefficient is additionally scaled by length-scale corrections when the
91 * colliding or collider integrates contact over a non-2D manifold (e.g., rod, shell). See @ref
92 * collidingPenaltyLengthScale.
93 */
95
96 /**
97 * @brief PolyReLU smoothing half-width [m].
98 *
99 * @details The penalty force transitions from 0 to linear over the decreasing distance range
100 * (penaltyThreshold, penaltyThreshold - 2 * @ref penaltySmoothingHalfDistance).
101 *
102 * @note Must not be negative.
103 * @note Larger smoothing distances improve stability but may increase penetration.
104 * @note Smoothing distance is expected to be small relative to the collider geometry.
105 */
107
108 /**
109 * @brief Default contact detection threshold [m].
110 *
111 * @details The penalty force transitions from 0 to linear over the decreasing distance range
112 * (penaltyThreshold, penaltyThreshold - 2 * @ref penaltySmoothingHalfDistance).
113 *
114 * @note Negative values are legal.
115 * @note If the colliding actor has @ref ColliderType::None or @ref ColliderType::PointCloud,
116 * penaltyThreshold = penaltyThresholdDefault + @ref penaltyThresholdExtraPadding. Otherwise,
117 * penaltyThreshold = penaltyThresholdDefault.
118 *
119 * @see penaltyThresholdExtraPadding, GetPenaltyThresholdDist
120 */
122
123 /**
124 * @brief Extra padding [m] added to the default contact detection threshold if the colliding
125 * actor has @ref ColliderType::None or @ref ColliderType::PointCloud.
126 *
127 * @note Must not be negative.
128 * @note Extra padding is useful to avoid tunneling through thin actors when the other actor has
129 * @ref ColliderType::None or @ref ColliderType::PointCloud.
130 *
131 * @see penaltyThresholdDefault, GetPenaltyThresholdDist
132 */
134
135 // Parameters for friction
136
137 /**
138 * @brief Use collider's normal (normalized SDF gradient) for friction direction if true, or the
139 * colliding's surface normal at the sample point if false.
140 *
141 * @note For co-dimensional colliding actors with ambiguous normals, the collider's normal is
142 * always used regardless of frictionWithColliderNormal.
143 */
145
146 /**
147 * @brief Maximum normal alignment threshold.
148 *
149 * @details Normal alignment is defined as the dot product between colliding and collider normals.
150 * Contact is disabled for sample points whose normal alignment exceeds this threshold. This
151 * prevents sample points from being trapped inside the collider when penetration is large.
152 *
153 * @note Valid range is [-1, 1]. -1 allows contact only for perfectly opposing normals, 1 allows
154 * all contacts.
155 * @note For co-dimensional colliding actors with ambiguous normals, contact is not disabled
156 * regardless of maxAlignmentNormals (normal alignment cannot be computed).
157 */
159
160 /**
161 * @brief Viscous friction coefficient [s/m].
162 *
163 * @note Friction force is proportional to contact force and tangential velocity.
164 * @note Must not be negative.
165 * @note Both viscousFrictionCoefficient and @ref coulombFrictionCoefficient can be >0.
166 * @note The viscous friction coefficient used in a collision is the geometric mean of the
167 * colliding and collider's coefficients. This disables viscous friction if either of them does.
168 */
170
171 /**
172 * @brief Coulomb friction coefficient (dimensionless).
173 *
174 * @note Must not be negative.
175 * @note Both @ref viscousFrictionCoefficient and coulombFrictionCoefficient can be >0.
176 * @note The Coulomb friction coefficient used in a collision is the geometric mean of the
177 * colliding and collider's coefficients. This disables Coulomb friction if either of them does.
178 */
180
181 /**
182 * @brief Velocity threshold for Coulomb friction smoothing [m/s].
183 *
184 * @details For C1Regularized, the Coulomb friction force smoothly transitions from 0 to full
185 * strength as tangential velocity increases from 0 to frictionFalloffVel (compact support). For
186 * CinfRegularized, the force asymptotically approaches full strength with no compact support
187 * boundary; frictionFalloffVel controls the regularization scale.
188 *
189 * @note Must not be negative. For CinfRegularized, a value of zero is clamped internally to
190 * avoid numerical issues.
191 * @note Smaller velocity thresholds improve physical accuracy but may degrade stability.
192 * @note The velocity threshold used in a collision is the geometric mean of the colliding and
193 * collider's thresholds. The exception is if the collider is static, in which case the
194 * colliding's threshold is used.
195 */
197
198 /**
199 * @brief Normal viscous damping coefficient [s/m].
200 *
201 * @details Damping force in the normal direction, proportional to the elastic normal contact
202 * force and the normal velocity. Analogous to @ref viscousFrictionCoefficient but acting in the
203 * normal direction instead of tangentially.
204 *
205 * @warning The calibration diverges as CoR approaches zero, which may cause numerical problems
206 * when approaching fully-inelastic collisions.
207 *
208 * @note Must not be negative.
209 * @note The normal viscous damping coefficient used in a collision is the geometric mean of the
210 * colliding and collider's coefficients. This disables normal damping if either of them does.
211 * @note The resulting coefficient of restitution (CoR) is velocity-dependent. For a
212 * characteristic impact velocity,
213 * @c experimental::CalibrateNormalViscousDampingCoefficient computes the coefficient that
214 * approximates a target CoR, while @c experimental::EffectiveCoefficientOfRestitution recovers
215 * the CoR produced by a coefficient.
216 * @note Because the damping force depends on the normal velocity at each contact point, it also
217 * introduces rolling resistance: a body rolling on a surface dissipates energy through the
218 * differing normal velocities across its contacting region.
219 */
221
222 // Experimental parameters
223
224 /**
225 * @brief [Experimental] Padding [m] added to the collider's bounding volume for collision
226 * culling.
227 *
228 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
229 * your own risk.
230 *
231 * @note Useful, for example, with approximate SDFs (e.g., deep flow map) to compensate for
232 * potentially overestimating the true distance.
233 */
235
236 /**
237 * @brief [Experimental] Object scale relative to default size (dimensionless). Used by deep flow
238 * only.
239 *
240 * @warning Deep flow is an experimental feature. It may be changed or removed in the future. Use
241 * at your own risk.
242 */
244
245 /**
246 * @brief [Experimental] Length scale [m] used to correct the penalty coefficient when the
247 * colliding body integrates contact traction over a lower-than-two-dimensional manifold, such as
248 * a rod or point mass. E.g., the penalty is scaled by this value if the colliding body lumps
249 * contact tractions on a line, or this value squared if lumping contact forces on a point.
250 *
251 * @warning Contact with lower-dimensional bodies is an experimental feature. It may be changed or
252 * removed in the future. Use at your own risk.
253 *
254 * @note This value is not used in the most common case, where contact traction is integrated over
255 * a two-dimensional surface.
256 * @note The colliding body's value is always used in a contact pair, because the colliding body
257 * determines the dimension of the contact traction integral.
258 */
260
261 /**
262 * @brief Get the total contact detection threshold.
263 *
264 * @param addPadding If true, includes @ref penaltyThresholdExtraPadding.
265 *
266 * @return Total contact detection threshold [m].
267 */
268 real GetPenaltyThresholdDist(bool addPadding) const;
269
270#if MOCHI_LANGUAGE_CPP20
271 bool operator==(ContactParams const&) const = default;
272#endif
273
274 // clang-format off
290 // clang-format on
291};
292
293/**
294 * @brief Selects which Coulomb friction smoothing model to use.
295 *
296 * @see ContactParams::frictionFalloffVel
297 */
299 /**
300 * @brief C1-smoothed Coulomb friction with compact support: friction transitions linearly from 0
301 * to full strength over [0, @ref ContactParams::frictionFalloffVel].
302 */
304
305 /**
306 * @brief C-infinity regularized Coulomb friction with no compact support: friction asymptotically
307 * approaches full strength. @ref ContactParams::frictionFalloffVel controls the regularization
308 * scale.
309 */
311
312 /** @brief Number of friction model enum values. */
314
315 /** @brief Default Coulomb friction model. */
317};
318
319} // namespace superdex
320
326
327namespace superdex {
328
329/**
330 * @brief Collision detection geometry for an @ref Actor.
331 *
332 * @note This setting controls how OTHER actors detect contact with this actor. It does not affect
333 * how this actor detects contact with other actors.
334 */
335enum class ColliderType {
336 /**
337 * @brief No collision representation. Other actors cannot detect contact with this actor.
338 *
339 * @note It does NOT prevent this actor from detecting contact with other actors.
340 */
342
343 /**
344 * @brief Automatic collider type selection based on the actor's shape.
345 *
346 * @note Mochi will select the most appropriate collider type based on the actor's shape and type:
347 * - Implicit sphere shapes: @ref ColliderType::Sphere
348 * - Implicit plane shapes: @ref ColliderType::Plane
349 * - Mesh shapes on volumetric actors (e.g., rigid, soft): @ref ColliderType::Sdf
350 * - Shell and rod actors: @ref ColliderType::PointCloud
351 *
352 * @note If Auto is set on an actor, a valid collider type will be assigned even if the shape's
353 * default collider type is @ref ColliderType::None. If you want @ref ColliderType::None, set
354 * @ref ColliderType::None explicitly as your collider type.
355 *
356 * @note After initialization, @ref Actor::GetColliderType will return the selected type, not
357 * @ref ColliderType::Auto.
358 */
360
361 /**
362 * @brief Represent the actor by an approximating sphere derived from its bounding volume.
363 *
364 * @note For shapes with a sphere bounding volume, that sphere is used. For shapes with an
365 * OBB/AABB bounding volume, the inscribed sphere is used. Otherwise, a true bounding sphere is
366 * computed.
367 * @note Fast but only accurate for spherical geometries.
368 * @note Only supported for rigid actors and articulated links.
369 */
371
372 /**
373 * @brief Represent the actor by its axis-aligned bounding box (AABB), in the actor's local frame.
374 *
375 * @note Fast but only accurate for box geometries.
376 * @note Only supported for rigid actors and articulated links.
377 */
379
380 /**
381 * @brief [Experimental] Represent the actor by its surface mesh.
382 *
383 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
384 * your own risk.
385 *
386 * @note Performance is slow.
387 * @note Only supported for rigid actors and articulated links.
388 */
390
391 /**
392 * @brief Represent the actor as an infinite plane.
393 *
394 * @note Only supported for static rigid actors whose shape is an implicit plane.
395 */
397
398 /**
399 * @brief Grid-based Signed Distance Field (SDF) collision representation.
400 *
401 * @note Uses a 3D grid storing the distance to the closest point in the actor's surface.
402 * @note Accurate for closed non-intersecting geometries provided the grid resolution is
403 * sufficient.
404 * @note If the grid is not baked in the actor's Shape, it is computed at initialization
405 * (expensive).
406 * @note Also supported for soft actors via SDF mapping.
407 *
408 * @see GridSdfParams
409 */
411
412 /**
413 * @brief Point-cloud collision representation.
414 *
415 * @details Contacts are detected by proximity queries between collider-side sample points and
416 * colliding-side sample points. Only other point-cloud colliders can collide with this type.
417 */
419
420 /** @brief Number of collider type enum values. */
422};
423} // namespace superdex
424
436
437#include "contact_params_inl.h"
CoulombFrictionModel
Selects which Coulomb friction smoothing model to use.
@ CinfRegularized
C-infinity regularized Coulomb friction with no compact support: friction asymptotically approaches f...
@ C1Regularized
C1-smoothed Coulomb friction with compact support: friction transitions linearly from 0 to full stren...
ColliderType
Collision detection geometry for an Actor.
@ Auto
Automatic collider type selection based on the actor's shape.
@ Mesh
[Experimental] Represent the actor by its surface mesh.
@ Sdf
Grid-based Signed Distance Field (SDF) collision representation.
@ PointCloud
Point-cloud collision representation.
@ None
Invalid actor type.
Definition mochi_enums.h:26
@ Count
Number of actor type enum values.
Definition mochi_enums.h:38
#define MOCHI_ENUM_COUNT(name)
Definition reflection.h:275
#define MOCHI_ENUM_END()
Definition reflection.h:276
#define MOCHI_STRUCT_END()
Definition reflection.h:279
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:273
#define MOCHI_FIELD(name)
Definition reflection.h:293
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:278
#define MOCHI_ATTRIBUTE(...)
Definition reflection.h:298
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:274
Data for an implicit box within a model file.
Definition box.h:31
Parameters for contact mechanics simulation.
real maxAlignmentNormals
Maximum normal alignment threshold.
real collidingPenaltyLengthScale
[Experimental] Length scale [m] used to correct the penalty coefficient when the colliding body integ...
bool operator==(ContactParams const &) const =default
real coulombFrictionCoefficient
Coulomb friction coefficient (dimensionless).
real penaltyThresholdDefault
Default contact detection threshold [m].
real penaltyCoefficient
Stiffness of the contact penalty force [Pa/m].
real distanceErrorBound
[Experimental] Padding [m] added to the collider's bounding volume for collision culling.
real normalViscousDampingCoefficient
Normal viscous damping coefficient [s/m].
real penaltySmoothingHalfDistance
PolyReLU smoothing half-width [m].
real objScale
[Experimental] Object scale relative to default size (dimensionless).
real GetPenaltyThresholdDist(bool addPadding) const
Get the total contact detection threshold.
real viscousFrictionCoefficient
Viscous friction coefficient [s/m].
bool frictionWithColliderNormal
Use collider's normal (normalized SDF gradient) for friction direction if true, or the colliding's su...
real penaltyThresholdExtraPadding
Extra padding [m] added to the default contact detection threshold if the colliding actor has Collide...
real frictionFalloffVel
Velocity threshold for Coulomb friction smoothing [m/s].