Skip to content

Commit 9e160e0

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 9e160e0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,61 @@ 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+
.. tip::
258+
This function uses CPU tensors to assign the collision properties. It is recommended to use this function
259+
only during the initialization of the environment.
260+
"""
261+
# extract the used quantities (to enable type-hinting)
262+
asset: RigidObject | Articulation = env.scene[asset_cfg.name]
263+
264+
# resolve environment ids
265+
if env_ids is None:
266+
env_ids = torch.arange(env.scene.num_envs, device="cpu")
267+
268+
# sample collider properties from the given ranges and set into the physics simulation
269+
# -- rest offsets
270+
if rest_offset_distribution_params is not None:
271+
rest_offset = asset.root_physx_view.get_rest_offsets().clone()
272+
rest_offset = _randomize_prop_by_op(
273+
rest_offset,
274+
rest_offset_distribution_params,
275+
None,
276+
slice(None),
277+
operation="abs",
278+
distribution=distribution,
279+
)
280+
asset.root_physx_view.set_rest_offsets(rest_offset, env_ids.cpu())
281+
# -- collision offsets
282+
if contact_offset_distribution_params is not None:
283+
contact_offset = asset.root_physx_view.get_contact_offsets().clone()
284+
contact_offset = _randomize_prop_by_op(
285+
contact_offset,
286+
contact_offset_distribution_params,
287+
None,
288+
slice(None),
289+
operation="abs",
290+
distribution=distribution,
291+
)
292+
asset.root_physx_view.set_contact_offsets(contact_offset, env_ids.cpu())
293+
294+
240295
def randomize_physics_scene_gravity(
241296
env: ManagerBasedEnv,
242297
env_ids: torch.Tensor | None,

0 commit comments

Comments
 (0)