Repository navigation
Expand file tree
/
Copy pathobstacles.py
More file actions
55 lines (48 loc) · 1.91 KB
/
Copy pathobstacles.py
File metadata and controls
55 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import pybullet as p
class Obstacle:
"""Moving obstacle that can bounce or slide within the World2D bounds."""
def __init__(self, center0, velocity, radius, bounds, stop_x=None, mode="bounce"):
self.center0 = np.array(center0, dtype=float)
self.velocity = np.array(velocity, dtype=float)
self.radius = float(radius)
self.bounds = bounds
self.stop_x = None if stop_x is None else float(stop_x)
self.mode = mode
def center(self, t):
if self.mode == "slide":
x = self.center0[0] + self.velocity[0] * t
stop_x = self.stop_x
if stop_x is None:
stop_x = self.bounds.xmax - self.radius
x = min(x, stop_x)
return np.array([x, self.center0[1]], dtype=float)
def ping_pong(p0, v, t, lo, hi):
length = hi - lo
if length <= 0.0:
return lo
p = (p0 - lo + v * t) % (2.0 * length)
if p > length:
p = 2.0 * length - p
return lo + p
lo_x = self.bounds.xmin + self.radius
hi_x = self.bounds.xmax - self.radius
lo_y = self.bounds.ymin + self.radius
hi_y = self.bounds.ymax - self.radius
return np.array(
[
ping_pong(self.center0[0], self.velocity[0], t, lo_x, hi_x),
ping_pong(self.center0[1], self.velocity[1], t, lo_y, hi_y),
],
dtype=float,
)
def update_cone_positions(cone_ids, moving_obstacles, t):
"""Updates the 3D position of the cones based on the obstacle velocity."""
for body_id, obs in zip(cone_ids, moving_obstacles):
new_center = obs.center(t)
# Keep Z height constant so it sits on the floor
p.resetBasePositionAndOrientation(
body_id,
[new_center[0], new_center[1], 0.5],
[0, 0, 0, 1]
)