LiamKhoaLe commited on
Commit
5b0f63c
·
1 Parent(s): 896eec6

Shift the robot centre point to its body's center. Allow offset around it body when moving

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -207,7 +207,7 @@ class Robot:
207
  if self.png.shape[-1] != 4:
208
  raise ValueError("Sprite image must have 4 channels (RGBA)")
209
  self.png = np.array(Image.open(sprite).convert("RGBA").resize((40,40)))
210
- self.pos = [0,0]; self.speed=speed
211
  def step(self, path):
212
  while path:
213
  dx, dy = path[0][0] - self.pos[0], path[0][1] - self.pos[1]
@@ -482,26 +482,26 @@ def _pipeline(uid,img_path):
482
  cv2.circle(frame, (x, y), 6, color, -1)
483
  # Robot displacement
484
  robot.step(path)
485
- rx, ry = robot.pos
486
  sp = robot.png
487
- # Calculate actual available space (clamp to 640x640)
488
- h, w = frame.shape[:2]
489
- end_x = min(rx + 40, w)
490
- end_y = min(ry + 40, h)
491
- crop_w = end_x - rx
492
- crop_h = end_y - ry
493
- # Crop sprite and alpha accordingly
494
- sprite_rgb = sp[:crop_h, :crop_w, :3]
495
- alpha = sp[:crop_h, :crop_w, 3] / 255.0
 
 
 
496
  alpha = np.stack([alpha] * 3, axis=-1)
497
- # Crop background region
498
- bgroi = frame[ry:end_y, rx:end_x]
499
- blended = (alpha * sprite_rgb + (1 - alpha) * bgroi).astype(np.uint8)
500
- # Update frame with blended result
501
- frame[ry:end_y, rx:end_x] = blended
502
  # collection check
503
  for o in objs:
504
- if not o["col"] and np.hypot(o["pos"][0]-rx,o["pos"][1]-ry)<=20:
505
  o["col"]=True
506
  vw.write(frame)
507
  if all(o["col"] for o in objs): break
 
207
  if self.png.shape[-1] != 4:
208
  raise ValueError("Sprite image must have 4 channels (RGBA)")
209
  self.png = np.array(Image.open(sprite).convert("RGBA").resize((40,40)))
210
+ self.pos = [20,20]; self.speed=speed
211
  def step(self, path):
212
  while path:
213
  dx, dy = path[0][0] - self.pos[0], path[0][1] - self.pos[1]
 
482
  cv2.circle(frame, (x, y), 6, color, -1)
483
  # Robot displacement
484
  robot.step(path)
 
485
  sp = robot.png
486
+ sprite_h, sprite_w = sp.shape[:2]
487
+ rx, ry = robot.pos
488
+ x1, y1 = rx - sprite_w // 2, ry - sprite_h // 2
489
+ x2, y2 = x1 + sprite_w, y1 + sprite_h
490
+ # Clip boundaries to image size
491
+ x1_clip, x2_clip = max(0, x1), min(frame.shape[1], x2)
492
+ y1_clip, y2_clip = max(0, y1), min(frame.shape[0], y2)
493
+ # Adjust sprite crop accordingly
494
+ sx1, sy1 = x1_clip - x1, y1_clip - y1
495
+ sx2, sy2 = sprite_w - (x2 - x2_clip), sprite_h - (y2 - y2_clip)
496
+ sprite_crop = sp[sy1:sy2, sx1:sx2]
497
+ alpha = sprite_crop[:, :, 3] / 255.0
498
  alpha = np.stack([alpha] * 3, axis=-1)
499
+ bgroi = frame[y1_clip:y2_clip, x1_clip:x2_clip]
500
+ blended = (alpha * sprite_crop[:, :, :3] + (1 - alpha) * bgroi).astype(np.uint8)
501
+ frame[y1_clip:y2_clip, x1_clip:x2_clip] = blended
 
 
502
  # collection check
503
  for o in objs:
504
+ if not o["col"] and np.hypot(o["pos"][0]-robot.pos[0], o["pos"][1]-robot.pos[1]) <= 20:
505
  o["col"]=True
506
  vw.write(frame)
507
  if all(o["col"] for o in objs): break