Skip to content

Commit 26680da

Browse files
authored
[BUG FIX] Fix camera following entity for 'fix_orientation=True'. (#2038)
* Fix camera following entity for 'fix_orientation=True'. * Add follow enity example script.
1 parent 21338f1 commit 26680da

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import argparse
2+
3+
import genesis as gs
4+
5+
6+
def main():
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument("-f", "--fix", action="store_true", default=False)
9+
args = parser.parse_args()
10+
11+
gs.init()
12+
13+
scene = gs.Scene(
14+
vis_options=gs.options.VisOptions(
15+
rendered_envs_idx=(0,),
16+
),
17+
profiling_options=gs.options.ProfilingOptions(
18+
show_FPS=False,
19+
),
20+
show_viewer=False,
21+
)
22+
scene.add_entity(morph=gs.morphs.Plane())
23+
cube = scene.add_entity(
24+
gs.morphs.Box(
25+
size=(0.1, 0.1, 0.1),
26+
pos=(0.0, -0.9, 1.0),
27+
euler=(15.0, 30.0, 60.0),
28+
)
29+
)
30+
31+
cam = scene.add_camera(
32+
res=(640, 480),
33+
pos=(2.0, 0.0, 1.5),
34+
lookat=(0, 0, 0.7),
35+
fov=40,
36+
GUI=True,
37+
)
38+
cam.follow_entity(cube, fix_orientation=args.fix)
39+
40+
scene.build()
41+
42+
cube.set_dofs_velocity([0.0, 5.0, 0.0, 0.0, 0.0, 1.0])
43+
for _ in range(100):
44+
scene.step()
45+
cam.render()
46+
47+
48+
if __name__ == "__main__":
49+
main()

genesis/vis/camera.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ def follow_entity(self, entity, fixed_axis=(None, None, None), smoothing=None, f
296296
# Note that it is not possible to expand / tile because the batch size is unknown before build.
297297
pos_rel = pos_rel.unsqueeze(0)
298298

299+
if (pos_rel.abs() < gs.EPS).all():
300+
gs.raise_exception("Camera must not be co-located with base link of entity to which it is attached.")
301+
299302
self._followed_entity = entity
300303
self._follow_pos_rel = pos_rel
301304
self._follow_fixed_axis = fixed_axis
@@ -346,8 +349,8 @@ def update_following(self):
346349
camera_lookat[:] = self._follow_smoothing * camera_lookat + (1.0 - self._follow_smoothing) * entity_pos
347350
else:
348351
camera_pos[:] = entity_pos
349-
camera_lookat[:] = entity_pos
350-
352+
if not self._follow_fix_orientation:
353+
camera_lookat[:] = entity_pos
351354
camera_pos += follow_pos_rel
352355

353356
# Fix the camera's position along the specified axis if requested

0 commit comments

Comments
 (0)