Skip to content

Commit

Permalink
Merge pull request #239 from PieterBecking/feature/camera-tracking-drone
Browse files Browse the repository at this point in the history
Feature/camera tracking drone
  • Loading branch information
zhouxian authored Dec 23, 2024
2 parents 43b11df + 8ab7eae commit da7c363
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions examples/drone/interactive_drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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,
Expand Down

0 comments on commit da7c363

Please sign in to comment.