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 For each field without an actor-pair override, contact between a colliding actor and a
29 * collider uses the collider's contact parameter (not the colliding actor's). The exceptions are:
30 * - For friction and dissipation coefficients (@ref viscousFrictionCoefficient,
31 * @ref coulombFrictionCoefficient, @ref normalViscousDampingCoefficient), the geometric mean of
32 * the colliding and collider's coefficients is used. This disables friction/dissipation if
33 * either of them does.
34 * - For penalty coefficient (@ref penaltyCoefficient), friction velocity threshold
35 * (@ref frictionFalloffVel) and presliding displacement (@ref preslidingDisplacement), the
36 * geometric mean of the colliding and collider's values is used, except if the collider is
37 * static in which case the colliding's values are used.
38 * - Elasto-plastic friction is preferred (@ref preferElastoPlasticFriction) only if both the
39 * colliding and the collider prefer it.
40 *
41 * @see Scene::SetContactPairParamsOverride
42 */
44 ContactParams() = default;
45
46 /**
47 * @brief Construct ContactParams with all parameters explicitly specified.
48 *
49 * @param[in] penaltyCoefficient See @ref penaltyCoefficient.
50 * @param[in] penaltySmoothingHalfDistance See @ref penaltySmoothingHalfDistance.
51 * @param[in] penaltyThresholdDefault See @ref penaltyThresholdDefault.
52 * @param[in] penaltyThresholdExtraPadding See @ref penaltyThresholdExtraPadding.
53 * @param[in] collidingPenaltyThresholdPadding See @ref collidingPenaltyThresholdPadding.
54 * @param[in] frictionWithColliderNormal See @ref frictionWithColliderNormal.
55 * @param[in] maxAlignmentNormals See @ref maxAlignmentNormals.
56 * @param[in] alignmentSmoothingFraction See @ref alignmentSmoothingFraction.
57 * @param[in] viscousFrictionCoefficient See @ref viscousFrictionCoefficient.
58 * @param[in] coulombFrictionCoefficient See @ref coulombFrictionCoefficient.
59 * @param[in] preferElastoPlasticFriction See @ref preferElastoPlasticFriction.
60 * @param[in] frictionFalloffVel See @ref frictionFalloffVel.
61 * @param[in] preslidingDisplacement See @ref preslidingDisplacement.
62 * @param[in] normalViscousDampingCoefficient See @ref normalViscousDampingCoefficient.
63 * @param[in] distanceErrorBound See @ref distanceErrorBound.
64 * @param[in] objScale See @ref objScale.
65 * @param[in] collidingPenaltyLengthScale See @ref collidingPenaltyLengthScale.
66 *
67 * @note Must take every parameter in order.
68 * @note Used when converting types to/from the C API.
69 */
88
89 // Parameters for penalty contact.
90
91 /**
92 * @brief Stiffness of the contact penalty force [Pa/m].
93 *
94 * @note Must be finite and strictly positive.
95 * @note Higher penalties create stiffer contacts and reduce penetration.
96 * @note Arbitrarily large penalties may degrade stability.
97 * @note The default penalty is appropriate for actors with default density. For actors with much
98 * higher/lower density than the default density, the penalty coefficient may need to be
99 * increased/decreased accordingly.
100 * @note Without an actor-pair override for this field, the value used in a collision is the
101 * geometric mean of the colliding and collider's coefficients. The exception is if the collider
102 * is static, in which case the colliding's penalty is used.
103 * @note The penalty coefficient is additionally scaled by length-scale corrections when the
104 * colliding or collider integrates contact over a non-2D manifold (e.g., rod, shell). See @ref
105 * collidingPenaltyLengthScale.
106 */
108
109 /**
110 * @brief PolyReLU smoothing half-width [m].
111 *
112 * @details The penalty force transitions from 0 to linear over the decreasing distance range
113 * (penaltyThreshold, penaltyThreshold - 2 * @ref penaltySmoothingHalfDistance).
114 *
115 * @note Must be finite and not negative.
116 * @note Larger smoothing distances improve stability but may increase penetration.
117 * @note Smoothing distance is expected to be small relative to the collider geometry.
118 */
120
121 /**
122 * @brief Default contact detection threshold [m].
123 *
124 * @details The penalty force transitions from 0 to linear over the decreasing distance range
125 * (penaltyThreshold, penaltyThreshold - 2 * @ref penaltySmoothingHalfDistance).
126 *
127 * @note Must be finite. Negative values are legal.
128 * @note If the colliding actor has @ref ColliderType::None or @ref ColliderType::PointCloud,
129 * penaltyThreshold = penaltyThresholdDefault + @ref penaltyThresholdExtraPadding. Otherwise,
130 * penaltyThreshold = penaltyThresholdDefault.
131 * @note The colliding actor's @ref collidingPenaltyThresholdPadding, if applicable, is added to
132 * penaltyThreshold.
133 *
134 * @see penaltyThresholdExtraPadding, collidingPenaltyThresholdPadding, GetPenaltyThresholdDist
135 */
137
138 /**
139 * @brief [Experimental] Extra padding [m] added to the default contact detection threshold if the
140 * colliding actor has @ref ColliderType::None or @ref ColliderType::PointCloud.
141 *
142 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
143 * your own risk.
144 *
145 * @note Must be finite and not negative.
146 * @note Extra padding is useful to avoid tunneling through thin actors when the other actor has
147 * @ref ColliderType::None or @ref ColliderType::PointCloud.
148 *
149 * @see penaltyThresholdDefault, GetPenaltyThresholdDist
150 */
152
153 /**
154 * @brief [Experimental] Padding [m] added to the collider's contact detection threshold when this
155 * actor is the colliding actor.
156 *
157 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
158 * your own risk.
159 *
160 * @note Must be finite and not negative.
161 * @note Unlike @ref penaltyThresholdExtraPadding, which the collider applies to some colliding
162 * actors, this padding is applied by the colliding actor to any collider, except colliders with
163 * @ref ColliderType::PointCloud and SDF colliders of soft actors, which ignore it.
164 * @note Padding is useful to avoid tunneling of thin or coarsely sampled colliding actors.
165 *
166 * @see penaltyThresholdDefault, penaltyThresholdExtraPadding
167 */
169
170 // Parameters for friction
171
172 /**
173 * @brief [Experimental] Use collider's normal (normalized SDF gradient) for friction direction if
174 * true, or the colliding's surface normal at the sample point if false.
175 *
176 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
177 * your own risk.
178 *
179 * @note For co-dimensional colliding actors with ambiguous normals, the collider's normal is
180 * always used regardless of frictionWithColliderNormal.
181 */
183
184 /**
185 * @brief [Experimental] Maximum normal alignment threshold.
186 *
187 * @details Normal alignment is defined as the dot product between colliding and collider normals.
188 * Contact is disabled for sample points whose normal alignment exceeds this threshold, abruptly
189 * or over a band centered at it (see @ref alignmentSmoothingFraction). This prevents sample
190 * points from being trapped inside the collider when penetration is large.
191 *
192 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
193 * your own risk.
194 *
195 * @note Must be in [-1, 1]. -1 allows contact only for perfectly opposing normals. 1 with @ref
196 * alignmentSmoothingFraction = 0 allows all contacts with unit normals. With a smooth band,
197 * contacts at alignment 1 keep half their force.
198 * @note For co-dimensional colliding actors with ambiguous normals, contact is not disabled
199 * regardless of maxAlignmentNormals (normal alignment cannot be computed).
200 *
201 * @see alignmentSmoothingFraction
202 */
204
205 /**
206 * @brief [Experimental] Width of the band of normal alignments, centered at @ref
207 * maxAlignmentNormals, over which contact fades out, as a fraction of the range of alignments
208 * [-1, @ref maxAlignmentNormals].
209 *
210 * @details The penalty force of a contact is scaled by a C1 smoothstep of its normal alignment,
211 * from 1 at an alignment of m - w / 2 to 0 at m + w / 2, with m = @ref maxAlignmentNormals and
212 * w = @ref alignmentSmoothingFraction * (m + 1). Contacts at m keep half their force. This
213 * removes the jump of the penalty force when contact is disabled. Contacts beyond the band are
214 * disabled, and friction fading (@ref ExperimentalEvalParams::fadeFriction) ends at its top.
215 *
216 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
217 * your own risk.
218 *
219 * @note Must be in [0, 1]. 0 disables contact abruptly at @ref maxAlignmentNormals.
220 * @note With the default 0.1 and the default @ref maxAlignmentNormals = 0, contact between unit
221 * normals fades out between angles of 92.9 and 87.1 degrees (a 5.7-degree band).
222 * @note Applies only with implicit normals (@ref ExperimentalEvalParams::explicitNormals =
223 * false). With explicit normals, contact is disabled abruptly at @ref maxAlignmentNormals.
224 * @note For co-dimensional colliding actors with ambiguous normals, the scale is 1 (normal
225 * alignment cannot be computed).
226 *
227 * @see maxAlignmentNormals
228 */
230
231 /**
232 * @brief Viscous friction coefficient [s/m].
233 *
234 * @note Friction force is proportional to contact force and tangential velocity.
235 * @note Must be finite and not negative.
236 * @note Both viscousFrictionCoefficient and @ref coulombFrictionCoefficient can be >0.
237 * @note Without an actor-pair override for this field, the value used in a collision is the
238 * geometric mean of the colliding and collider's coefficients. This disables viscous friction if
239 * either of them does.
240 */
242
243 /**
244 * @brief Coulomb friction coefficient (dimensionless).
245 *
246 * @note Must be finite and not negative.
247 * @note Both @ref viscousFrictionCoefficient and coulombFrictionCoefficient can be >0.
248 * @note Without an actor-pair override for this field, the value used in a collision is the
249 * geometric mean of the colliding and collider's coefficients. This disables Coulomb friction if
250 * either of them does.
251 */
253
254 /**
255 * @brief [Experimental] Prefer elasto-plastic Coulomb friction to velocity-regularized Coulomb
256 * friction.
257 *
258 * @details Elasto-plastic friction measures the tangential displacement of each contact from its
259 * anchor, a point of the collider, so a contact holds a tangential load below the Coulomb limit
260 * without slip (see @ref preslidingDisplacement). Velocity-regularized friction acts only through
261 * slip, so held objects creep (see @ref frictionFalloffVel).
262 *
263 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
264 * your own risk.
265 *
266 * @note Contacts that prefer elasto-plastic friction use it where it is supported: with rigid
267 * colliders, static or dynamic, including articulated links, and with an integration method whose
268 * step ends at its last stage (@ref IntegrationMethod::BackwardEuler,
269 * @ref IntegrationMethod::BDF2, @ref IntegrationMethod::BDF3, @ref IntegrationMethod::DIRK22 or
270 * @ref IntegrationMethod::DIRK33). The other contacts use velocity-regularized friction.
271 * @note Friction preload is part of the actor history (see @ref Actor). A static actor given a
272 * velocity every step, like a conveyor belt, never builds preload, so contacts on it creep, as
273 * with velocity-regularized friction whose falloff velocity is @ref preslidingDisplacement over
274 * the stage step (the time step with @ref IntegrationMethod::BackwardEuler).
275 * @note Without an actor-pair override for this field, a collision prefers elasto-plastic
276 * friction only if both the colliding and the collider do, even if the collider is static.
277 */
279
280 /**
281 * @brief [Experimental] Velocity threshold for Coulomb friction smoothing [m/s].
282 *
283 * @details The Coulomb friction force smoothly transitions from 0 to full strength as tangential
284 * velocity increases from 0 to frictionFalloffVel.
285 *
286 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
287 * your own risk.
288 *
289 * @note Must be finite and not negative. Zero gives unsmoothed Coulomb friction.
290 * @note Used only by contacts with velocity-regularized friction (see
291 * @ref preferElastoPlasticFriction).
292 * @note Smaller velocity thresholds improve physical accuracy but may degrade stability.
293 * @note Without an actor-pair override for this field, the value used in a collision is the
294 * geometric mean of the colliding and collider's thresholds. The exception is if the collider is
295 * static, in which case the colliding's threshold is used.
296 */
298
299 /**
300 * @brief Presliding displacement of elasto-plastic Coulomb friction [m].
301 *
302 * @details With elasto-plastic friction (see @ref preferElastoPlasticFriction), the Coulomb
303 * friction force smoothly transitions from 0 to full strength as the tangential displacement of a
304 * contact from its anchor increases from 0 to preslidingDisplacement, and beyond it the anchor
305 * follows the contact. A held contact springs back by up to preslidingDisplacement once the load
306 * is removed.
307 *
308 * @note Smaller values reduce presliding motion and spring-back, but stiffen the tangential
309 * response.
310 * @note Within the presliding displacement, the friction force is elastic: it stores energy and
311 * adds no damping. Tangential damping comes only from @ref viscousFrictionCoefficient, zero by
312 * default, and from the time integration (@ref normalViscousDampingCoefficient acts along the
313 * normal).
314 * @note Must be finite and strictly positive.
315 * @note Without an actor-pair override for this field, the value used in a collision is the
316 * geometric mean of the colliding and collider's displacements. The exception is if the collider
317 * is static, in which case the colliding's displacement is used.
318 */
320
321 /**
322 * @brief Normal viscous damping coefficient [s/m].
323 *
324 * @details Damping force in the normal direction, proportional to the elastic normal contact
325 * force and the normal velocity. Analogous to @ref viscousFrictionCoefficient but acting in the
326 * normal direction instead of tangentially.
327 *
328 * @warning The calibration diverges as CoR approaches zero, which may cause numerical problems
329 * when approaching fully-inelastic collisions.
330 *
331 * @note Must be finite and not negative.
332 * @note Without an actor-pair override for this field, the value used in a collision is the
333 * geometric mean of the colliding and collider's coefficients. This disables normal damping if
334 * either of them does.
335 * @note The resulting coefficient of restitution (CoR) is velocity-dependent. For a
336 * characteristic impact velocity,
337 * @c experimental::CalibrateNormalViscousDampingCoefficient computes the coefficient that
338 * approximates a target CoR, while @c experimental::EffectiveCoefficientOfRestitution recovers
339 * the CoR produced by a coefficient.
340 * @note Because the damping force depends on the normal velocity at each contact point, it also
341 * introduces rolling resistance: a body rolling on a surface dissipates energy through the
342 * differing normal velocities across its contacting region.
343 */
345
346 // Experimental parameters
347
348 /**
349 * @brief [Experimental] Padding [m] added to the collider's bounding volume for collision
350 * culling.
351 *
352 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
353 * your own risk.
354 *
355 * @note Must be finite.
356 * @note Useful, for example, with approximate SDFs (e.g., deep flow map) to compensate for
357 * potentially overestimating the true distance.
358 */
360
361 /**
362 * @brief [Experimental] Object scale relative to default size (dimensionless). Used by deep flow
363 * only.
364 *
365 * @warning Deep flow is an experimental feature. It may be changed or removed in the future. Use
366 * at your own risk.
367 *
368 * @note Must be finite and strictly positive.
369 */
371
372 /**
373 * @brief [Experimental] Length scale [m] used to correct the penalty coefficient when the
374 * colliding body integrates contact traction over a lower-than-two-dimensional manifold, such as
375 * a rod or point mass. E.g., the penalty is scaled by this value if the colliding body lumps
376 * contact tractions on a line, or this value squared if lumping contact forces on a point.
377 *
378 * @warning Contact with lower-dimensional bodies is an experimental feature. It may be changed or
379 * removed in the future. Use at your own risk.
380 *
381 * @note Must be finite and strictly positive.
382 * @note This value is not used in the most common case, where contact traction is integrated over
383 * a two-dimensional surface.
384 * @note The colliding body's value is always used in a contact pair, because the colliding body
385 * determines the dimension of the contact traction integral.
386 */
388
389 /**
390 * @brief Get the total contact detection threshold.
391 *
392 * @param addPadding If true, includes @ref penaltyThresholdExtraPadding.
393 *
394 * @return Total contact detection threshold [m].
395 */
396 real GetPenaltyThresholdDist(bool addPadding) const;
397
398#if MOCHI_LANGUAGE_CPP20
399 bool operator==(ContactParams const&) const = default;
400#endif
401
402 // clang-format off
422 // clang-format on
423};
424
425/**
426 * @brief Collision detection geometry for an @ref Actor.
427 *
428 * @note This setting controls how OTHER actors detect contact with this actor. It does not affect
429 * how this actor detects contact with other actors.
430 */
431enum class ColliderType {
432 /**
433 * @brief No collision representation. Other actors cannot detect contact with this actor.
434 *
435 * @note It does NOT prevent this actor from detecting contact with other actors.
436 */
438
439 /**
440 * @brief Automatic collider type selection based on the actor's shape.
441 *
442 * @note Mochi will select the most appropriate collider type based on the actor's shape and type:
443 * - Implicit sphere shapes: @ref ColliderType::Sphere
444 * - Implicit plane shapes: @ref ColliderType::Plane
445 * - Mesh shapes on volumetric actors (e.g., rigid, soft): @ref ColliderType::Sdf
446 * - Shell and rod actors: @ref ColliderType::PointCloud
447 *
448 * @note If Auto is set on an actor, a valid collider type will be assigned even if the shape's
449 * default collider type is @ref ColliderType::None. If you want @ref ColliderType::None, set
450 * @ref ColliderType::None explicitly as your collider type.
451 *
452 * @note After initialization, @ref Actor::GetColliderType will return the selected type, not
453 * @ref ColliderType::Auto.
454 */
456
457 /**
458 * @brief Represent the actor by an approximating sphere derived from its bounding volume.
459 *
460 * @note For shapes with a sphere bounding volume, that sphere is used. For shapes with an
461 * OBB/AABB bounding volume, the inscribed sphere is used. Otherwise, a true bounding sphere is
462 * computed.
463 * @note Fast but only accurate for spherical geometries.
464 * @note Only supported for rigid actors and articulated links.
465 */
467
468 /**
469 * @brief Represent the actor by its axis-aligned bounding box (AABB), in the actor's local frame.
470 *
471 * @note Fast but only accurate for box geometries.
472 * @note Only supported for rigid actors and articulated links.
473 */
475
476 /**
477 * @brief [Experimental] Represent the actor by its surface mesh.
478 *
479 * @warning Mesh collision performs triangle-mesh distance queries during simulation and can be
480 * prohibitively slow. For mesh-backed actors, prefer @ref ColliderType::Auto or
481 * @ref ColliderType::Sdf unless measurements justify using Mesh.
482 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
483 * your own risk.
484 *
485 * @note Only supported for rigid actors and articulated links.
486 */
488
489 /**
490 * @brief Represent the actor as an infinite plane.
491 *
492 * @note Only supported for static rigid actors whose shape is an implicit plane.
493 */
495
496 /**
497 * @brief Grid-based Signed Distance Field (SDF) collision representation.
498 *
499 * @note Uses a 3D grid storing the distance to the closest point in the actor's surface.
500 * @note Accurate for closed non-intersecting geometries provided the grid resolution is
501 * sufficient.
502 * @note If the grid is not baked in the actor's Shape, it is computed at initialization
503 * (expensive), and actor creation fails if it cannot be computed.
504 * @note Also supported for soft actors via SDF mapping.
505 *
506 * @see GridSdfParams
507 */
509
510 /**
511 * @brief Point-cloud collision representation.
512 *
513 * @details Contacts are detected by proximity queries between collider-side sample points and
514 * colliding-side sample points. Only other point-cloud colliders can collide with this type.
515 */
517
518 /** @brief Number of collider type enum values. */
520};
521} // namespace superdex
522
534
535#include "contact_params_inl.h"
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:329
#define MOCHI_ENUM_END()
Definition reflection.h:330
#define MOCHI_STRUCT_END()
Definition reflection.h:333
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:327
#define MOCHI_FIELD(name)
Definition reflection.h:351
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:332
#define MOCHI_ATTRIBUTE(...)
Definition reflection.h:357
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:328
Data for an implicit box within a model file.
Definition box.h:31
Parameters for contact mechanics simulation.
real alignmentSmoothingFraction
[Experimental] Width of the band of normal alignments, centered at maxAlignmentNormals,...
real maxAlignmentNormals
[Experimental] 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 preslidingDisplacement
Presliding displacement of elasto-plastic Coulomb friction [m].
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].
bool preferElastoPlasticFriction
[Experimental] Prefer elasto-plastic Coulomb friction to velocity-regularized Coulomb friction.
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
[Experimental] Use collider's normal (normalized SDF gradient) for friction direction if true,...
real penaltyThresholdExtraPadding
[Experimental] Extra padding [m] added to the default contact detection threshold if the colliding ac...
real collidingPenaltyThresholdPadding
[Experimental] Padding [m] added to the collider's contact detection threshold when this actor is the...
real frictionFalloffVel
[Experimental] Velocity threshold for Coulomb friction smoothing [m/s].