google / brax

Massively parallel rigidbody physics simulation on accelerator hardware.
Apache License 2.0
2.14k stars 234 forks source link

Different behavior from gym.Mujoco: the walker body penetrate the ground #464

Closed lkevinzc closed 4 months ago

lkevinzc commented 4 months ago

I am testing the walker2d environment with brax. I provide no action (all-zero vector) and just let the gravity pull the walker down. However, I found the walker body will penetrate the ground, which gives very different physics behaviors from the one with gym.

brax gym
Screencastfrom01-03-2024174523-ezgif com-video-to-gif-converter walker_no_control-ezgif com-optimize

I'm using the following codes for brax:

import jax
import jax.numpy as jnp

from brax.envs import walker2d
from brax.io.html import render

env = walker2d.Walker2d()
state = env.reset(jax.random.PRNGKey(0))

env.step = jax.jit(env.step)

no_action = jnp.zeros(env.action_size)

rollout = []
for _ in range(500):
    rollout.append(state.pipeline_state)
    state = env.step(state, no_action)

html_str = render(env.sys, rollout)
with open("./walker_no_control.html", "w") as f:
    f.write(html_str)

and for gym:

import gymnasium as gym
import numpy as np
import skvideo.io

env = gym.make("Walker2d-v4", render_mode="rgb_array")
env.reset()
frames = []

no_action = np.zeros(np.cumprod(env.action_space.shape))
for _ in range(500):
    frames.append(env.render())
    env.step(no_action)

frames_np = np.stack(frames)
writer = skvideo.io.FFmpegWriter(f"walker_no_control.mp4", verbosity=1)
for i in range(len(frames_np)):
    writer.writeFrame(frames_np[i, :, :, :])
writer.close()

Could anyone help to explain why, and give some solutions?

lkevinzc commented 4 months ago

Solved. In the xml file, for the floor geom and all the body geom, modify the contype or conaffinity such that they are compatible to pass the collision detection filtering.

Ref: [1] https://mujoco.readthedocs.io/en/stable/XMLreference.html#body-geom [2] https://mujoco.readthedocs.io/en/stable/computation/index.html#collision-detection