Skip to content

Commit 82f3613

Browse files
committed
Adds event term function to randomize collider offsets (#1753)
This MR adds an event term for randomizing the collider's rest and contact offsets. - New feature (non-breaking change which adds functionality) - [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with `./isaaclab.sh --format` - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have updated the changelog and the corresponding version in the extension's `config/extension.toml` file - [x] I have added my name to the `CONTRIBUTORS.md` or my name already exists there
1 parent e787e9b commit 82f3613

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

source/isaaclab/isaaclab/envs/mdp/events.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,63 @@ def randomize_rigid_body_mass(
237237
asset.root_physx_view.set_inertias(inertias, env_ids)
238238

239239

240+
def randomize_rigid_body_collider_offsets(
241+
env: ManagerBasedEnv,
242+
env_ids: torch.Tensor | None,
243+
asset_cfg: SceneEntityCfg,
244+
rest_offset_distribution_params: tuple[float, float] | None = None,
245+
contact_offset_distribution_params: tuple[float, float] | None = None,
246+
distribution: Literal["uniform", "log_uniform", "gaussian"] = "uniform",
247+
):
248+
"""Randomize the collider parameters of rigid bodies in an asset by adding, scaling, or setting random values.
249+
250+
This function allows randomizing the collider parameters of the asset, such as rest and contact offsets.
251+
These correspond to the physics engine collider properties that affect the collision checking.
252+
253+
The function samples random values from the given distribution parameters and applies the operation to
254+
the collider properties. It then sets the values into the physics simulation. If the distribution parameters
255+
are not provided for a particular property, the function does not modify the property.
256+
257+
Currently, the distribution parameters are applied as absolute values.
258+
259+
.. tip::
260+
This function uses CPU tensors to assign the collision properties. It is recommended to use this function
261+
only during the initialization of the environment.
262+
"""
263+
# extract the used quantities (to enable type-hinting)
264+
asset: RigidObject | Articulation = env.scene[asset_cfg.name]
265+
266+
# resolve environment ids
267+
if env_ids is None:
268+
env_ids = torch.arange(env.scene.num_envs, device="cpu")
269+
270+
# sample collider properties from the given ranges and set into the physics simulation
271+
# -- rest offsets
272+
if rest_offset_distribution_params is not None:
273+
rest_offset = asset.root_physx_view.get_rest_offsets().clone()
274+
rest_offset = _randomize_prop_by_op(
275+
rest_offset,
276+
rest_offset_distribution_params,
277+
None,
278+
slice(None),
279+
operation="abs",
280+
distribution=distribution,
281+
)
282+
asset.root_physx_view.set_rest_offsets(rest_offset, env_ids.cpu())
283+
# -- contact offsets
284+
if contact_offset_distribution_params is not None:
285+
contact_offset = asset.root_physx_view.get_contact_offsets().clone()
286+
contact_offset = _randomize_prop_by_op(
287+
contact_offset,
288+
contact_offset_distribution_params,
289+
None,
290+
slice(None),
291+
operation="abs",
292+
distribution=distribution,
293+
)
294+
asset.root_physx_view.set_contact_offsets(contact_offset, env_ids.cpu())
295+
296+
240297
def randomize_physics_scene_gravity(
241298
env: ManagerBasedEnv,
242299
env_ids: torch.Tensor | None,

0 commit comments

Comments
 (0)