From 9494c01b27675323a6fb8dcdb4230e4c679b9716 Mon Sep 17 00:00:00 2001 From: Pieter Becking Date: Sun, 22 Dec 2024 19:36:12 +0100 Subject: [PATCH 1/2] Camera tracking drone for easier flying --- examples/drone/interactive_drone.py | 33 ++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/drone/interactive_drone.py b/examples/drone/interactive_drone.py index a719c94d..ebc48972 100644 --- a/examples/drone/interactive_drone.py +++ b/examples/drone/interactive_drone.py @@ -91,14 +91,41 @@ 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() + + # Calculate 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,12 +143,12 @@ 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, + max_FPS=60 ) scene = gs.Scene( From 8ab7eaed20e8d7065e2ba2c3495c85813a40b45d Mon Sep 17 00:00:00 2001 From: Pieter Becking Date: Sun, 22 Dec 2024 19:38:09 +0100 Subject: [PATCH 2/2] Fixed orientation of drone forward --- examples/drone/interactive_drone.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/drone/interactive_drone.py b/examples/drone/interactive_drone.py index ebc48972..6a0e0788 100644 --- a/examples/drone/interactive_drone.py +++ b/examples/drone/interactive_drone.py @@ -98,16 +98,12 @@ def update_camera(scene, drone): drone_pos = drone.get_pos() - # Calculate camera position relative to drone - offset_x = 0.0 # centered horizontally + # 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 + 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) - ) + 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)) @@ -148,7 +144,7 @@ def main(): 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 + max_FPS=60, ) scene = gs.Scene(