"""Watch the lesson's ball in MuJoCo's viewer window, dropped from 1 m again every 2.5 seconds.

Run: python3 site/ml/foundations/examples/view_ball.py
The same model as mujoco_first_steps.py. The viewer only draws; mj_step does the physics, at real-time speed.
Drag with the left mouse button to turn the camera; double-click the ball, then Ctrl + right-drag to push it.
"""
import time

import mujoco
import mujoco.viewer

from mujoco_first_steps import make_model

model = make_model()
model.vis.headlight.ambient[:], model.vis.headlight.diffuse[:] = [0.5] * 3, [0.6] * 3   # brighter, for watching only
model.geom_rgba[:] = [[0.75, 0.75, 0.72, 1], [0.18, 0.45, 0.85, 1]]                     # floor grey, ball blue
data = mujoco.MjData(model)
with mujoco.viewer.launch_passive(model, data) as viewer:
    viewer.cam.lookat[:], viewer.cam.distance = [0, 0, 0.5], 1.9        # look at the drop from the side
    viewer.cam.azimuth, viewer.cam.elevation = 90, -5
    while viewer.is_running():
        if data.time == 0 or data.time > 2.5:                            # start a new drop
            mujoco.mj_resetData(model, data)
            data.qpos[0] = 1.0
        start = time.time()
        mujoco.mj_step(model, data)
        viewer.sync()
        time.sleep(max(0.0, model.opt.timestep - (time.time() - start)))
time.sleep(0.5)                                                          # let the window thread finish
