diff --git a/examples/drone/interactive_drone.py b/examples/drone/interactive_drone.py index a719c94d..6a0e0788 100644 --- a/examples/drone/interactive_drone.py +++ b/examples/drone/interactive_drone.py @@ -91,14 +91,37 @@ def update_thrust(self): return self.rpms +def update_camera(scene, drone): + """Updates the camera position to follow the drone""" + if not scene.viewer: + return + + drone_pos = drone.get_pos() + + # Camera position relative to drone + offset_x = 0.0 # centered horizontally + offset_y = -4.0 # 4 units behind (in Y axis) + offset_z = 2.0 # 2 units above + + camera_pos = (float(drone_pos[0] + offset_x), float(drone_pos[1] + offset_y), float(drone_pos[2] + offset_z)) + + # Update camera position and look target + scene.viewer.set_camera_pose(pos=camera_pos, lookat=tuple(float(x) for x in drone_pos)) + + def run_sim(scene, drone, controller): while controller.running: try: # Update drone with current RPMs rpms = controller.update_thrust() drone.set_propellels_rpm(rpms) + # Update physics scene.step() + + # Update camera position to follow drone + update_camera(scene, drone) + time.sleep(1 / 60) # Limit simulation rate except Exception as e: print(f"Error in simulation loop: {e}") @@ -116,9 +139,9 @@ def main(): # Initialize Genesis gs.init(backend=gs.cpu) - # Create scene with enhanced camera view + # Create scene with initial camera view viewer_options = gs.options.ViewerOptions( - camera_pos=(4.0, 0.0, 2.0), + camera_pos=(0.0, -4.0, 2.0), # Now behind the drone (negative Y) camera_lookat=(0.0, 0.0, 0.5), camera_fov=45, max_FPS=60,