Skip to content

[FEATURE] Environment-wise gravity#1324

Merged
duburcqa merged 25 commits intoGenesis-Embodied-AI:mainfrom
LeonLiu4:feature/env-wise-gravity
Jul 3, 2025
Merged

[FEATURE] Environment-wise gravity#1324
duburcqa merged 25 commits intoGenesis-Embodied-AI:mainfrom
LeonLiu4:feature/env-wise-gravity

Conversation

@LeonLiu4
Copy link
Contributor

@LeonLiu4 LeonLiu4 commented Jun 26, 2025

This PR introduces support for environment-specific gravity settings. Different environments in the simulation can now have distinct gravity configurations, enabling more flexible and realistic physics behavior tailored to each context.

Example use case with two spheres:

import torch
import genesis as gs

gs.init(backend=gs.cpu)

scene = gs.Scene(
    show_viewer=True,
    sim_options=gs.options.SimOptions(
        dt=0.01,
        substeps=1,
    ),
)

sphere = scene.add_entity(gs.morphs.Sphere())
scene.build(n_envs=2)

scene.sim.set_gravity(torch.tensor([0.0, 0.0, -9.8]), envs_idx=0)
scene.sim.set_gravity(torch.tensor([0.0, 0.0, 9.8]), envs_idx=1)

for _ in range(200):
    scene.step()

Addresses: #1113

Below is a video demonstrating two parallel environments one with more negative gravity making the drone fall while the other hovers:

video.mp4

@LeonLiu4 LeonLiu4 force-pushed the feature/env-wise-gravity branch from 6947269 to d506fff Compare June 27, 2025 00:04
@YilingQiao
Copy link
Collaborator

Could you resolve the conflicts? Thanks!

@duburcqa
Copy link
Collaborator

I don't understand why do you have to change the logic this much just for changing the way the gravity is initialised.

Moreover, it would be nice to add a setter for the gravity (both pre- and post-build).

@LeonLiu4 LeonLiu4 force-pushed the feature/env-wise-gravity branch from e32580d to ebcd09e Compare June 27, 2025 17:21
@LeonLiu4 LeonLiu4 force-pushed the feature/env-wise-gravity branch from 91a1a91 to 88fe5fe Compare June 27, 2025 17:42
Copy link
Collaborator

@duburcqa duburcqa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, different solvers may have different gravity. This is kind of hacky and should be removed at some point, but for now that is how it is. This means that what you are doing is not going to work.

However, it is not possible to just update the code block at line 25 to take into account the batch size because it is unknown at this point and will only be specified during build. Anyway, all options should be batch-size agnostic by design. I do not think it is acceptable to have to options that only work for a specific batch size as you are currently doing.

As a result, I would suggest to force option.gravity being a vector, and a base build method to Solver class that would be responsible for both setting the batch size and initialising the gravity field. Then add a setter with the following signature to classSolver:

def set_gravity(self, gravity, envs_idx=None):

if envs_idx=None, set the gravity for all environments as you are doing, otherwise set the gravity for one specific environment. Just pass without doing anything if self._gravity = None.

Then, remove all your changes at scene-level for now, and add a set_gravity setter that would call set_gravity for all the underlying solvers sequentially.

@LeonLiu4 LeonLiu4 force-pushed the feature/env-wise-gravity branch from 663fa80 to 8b0a056 Compare June 30, 2025 19:19
@LeonLiu4 LeonLiu4 requested a review from duburcqa June 30, 2025 20:46
@LeonLiu4 LeonLiu4 force-pushed the feature/env-wise-gravity branch from bbb5c0e to 5342756 Compare June 30, 2025 22:30
@LeonLiu4 LeonLiu4 requested a review from duburcqa June 30, 2025 22:34
@duburcqa duburcqa self-requested a review July 2, 2025 18:14
duburcqa
duburcqa previously approved these changes Jul 2, 2025
@duburcqa duburcqa enabled auto-merge (squash) July 2, 2025 20:46
duburcqa
duburcqa previously approved these changes Jul 2, 2025
auto-merge was automatically disabled July 2, 2025 22:03

Head branch was pushed to by a user without write access

duburcqa
duburcqa previously approved these changes Jul 3, 2025
@duburcqa duburcqa enabled auto-merge (squash) July 3, 2025 05:23
@YilingQiao
Copy link
Collaborator

@LeonLiu4 could you please fix the unit test? Next time you can run them on you local branch first.

duburcqa
duburcqa previously approved these changes Jul 3, 2025
@duburcqa duburcqa merged commit b0f6479 into Genesis-Embodied-AI:main Jul 3, 2025
13 checks passed
@LeonLiu4 LeonLiu4 deleted the feature/env-wise-gravity branch July 3, 2025 17:11
Milotrince pushed a commit to Milotrince/Genesis that referenced this pull request Jul 5, 2025
chris-la-humalab pushed a commit to HumaLab/Genesis that referenced this pull request Aug 1, 2025
YilingQiao pushed a commit to YilingQiao/Genesis that referenced this pull request Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants