def view(self, world, center, direction): self.reset() x0, y0 = center rx, ry = direction theta = np.arctan2(ry, rx) rx, ry = np.cos(theta), np.sin(theta) px, py = np.cos(theta + np.pi / 2), np.sin(theta + np.pi / 2) for b in world.b2.bodies: x, y = b.position dx, dy = x - x0, y - y0 alngr = rx * dx + ry * dy alngp = px * dx + py * dy if -self.x_threshold / 2 < alngr < self.x_threshold / 2 and -self.y_threshold / 2 < alngp < self.y_threshold / 2: bx, by = self.ownbody.position if b.userData['type'] == 'candy': self.b2.CreateStaticBody( position=(bx - alngr, by - alngp), shapes=circleShape(radius=5.0), linearDamping=0.0, bullet=True, userData={'color': (128, 255, 128)}) if b.userData['type'] == 'obstacle': self.b2.CreateStaticBody( position=(bx - alngr, by - alngp), shapes=circleShape(radius=20.0), linearDamping=0.0, bullet=True, userData={'color': (192, 128, 128)}) self.drawer.install() self.drawer.draw_world(self.b2) return self.drawer.screen
def reset(self): self.drawer.clear_screen() for b in self.b2.bodies: self.b2.DestroyBody(b) self.ownbody = self.b2.CreateStaticBody( position=(self.x_threshold / 2, self.y_threshold / 2), shapes=circleShape(radius=5.0), linearDamping=0.0, bullet=True, userData={'color': (255, 255, 0)})
def add_obstacle(self): if 30 < self.x_pos < self.x_threshold - 30 and 30 < self.y_pos < self.y_threshold - 30: self.b2.CreateStaticBody( position=(self.x_pos, self.y_pos), shapes=circleShape(radius=20), linearDamping=0.0, bullet=True, userData={ 'world': self.b2, 'type': 'obstacle', 'ax': 0, 'ay': 0, 'color': (192, 128, 128) }, )