def resetPositions(self): for team in range(len(self.spawns)): for index in range(self.BOTS_PER_TEAM): actual_index = team + index * len(self.spawns) if actual_index >= len(self.robots): break # Generate a uniformly random point in radius of spawn for bot. diff_radius = Randomiser.random() * self.BOT_SPAWN_RADIUS diff_angle = Randomiser.random() * 2 * np.pi self.robots[actual_index].body.position = [ self.spawns[team][index][0][0] + diff_radius * np.cos(diff_angle), self.spawns[team][index][0][1] + diff_radius * np.sin(diff_angle), ] self.robots[actual_index].body.angle = self.spawns[team][ index][1] * np.pi / 180 self.robots[actual_index].body.velocity = (0.0, 0.0) self.robots[actual_index].body.angular_velocity = 0 # Generate position for ball. diff_radius = Randomiser.random() * self.BALL_SPAWN_RADIUS diff_angle = Randomiser.random() * 2 * np.pi ScriptLoader.instance.object_map["IR_BALL"].body.position = [ diff_radius * np.cos(diff_angle), diff_radius * np.sin(diff_angle) - 18, ] ScriptLoader.instance.object_map["IR_BALL"].body.velocity = (0.0, 0.0) for idx in range(len(self.bot_penalties)): if self.bot_penalties[idx] > 0: self.finishPenalty(idx)
def _DistanceFromSensor(self, startPosition, centreRotation): top_length = self.MAX_RAYCAST while top_length > 0: endPosition = startPosition + top_length * np.array( [np.cos(centreRotation), np.sin(centreRotation)]) # Ignore all ignored objects by setting the category on them. cats = [] for obj in self.ignore_objects: if isinstance(obj, PhysicsObject): for shape in obj.shapes: cats.append(shape.filter.categories) shape.filter = pymunk.ShapeFilter(categories=0b1) raycast = World.instance.space.segment_query_first( [float(v) for v in startPosition], [float(v) for v in endPosition], self.RAYCAST_RADIUS, pymunk.ShapeFilter(mask=STATIC_CATEGORY | DYNAMIC_CATEGORY), ) i = 0 for obj in self.ignore_objects: if isinstance(obj, PhysicsObject): for shape in obj.shapes: shape.filter = pymunk.ShapeFilter(categories=cats[i]) i += 1 if raycast == None: if top_length == self.MAX_RAYCAST or ( not ScriptLoader.RANDOMISE_SENSORS): return max(0, min(self.MAX_RAYCAST, top_length + self.offset)) # If randomiser, linearly scale result by angle between surface normal of raycasted point. return max( 0, min( self.MAX_RAYCAST, top_length + (1 + (self.last_angle_diff + self.STATIC_RANDOM_ANGLE * Randomiser.random()) * (Randomiser.random() - 0.5) * self.ANGLE_RANDOM_AMPLITUDE / np.pi * 2) + self.offset, ), ) else: opposite_angle = centreRotation + np.pi while opposite_angle > raycast.normal.angle + np.pi: opposite_angle -= 2 * np.pi while opposite_angle < raycast.normal.angle - np.pi: opposite_angle += 2 * np.pi self.last_angle_diff = abs(opposite_angle - raycast.normal.angle) top_length = raycast.alpha * top_length - self.ACCEPTANCE_LEVEL return max(0, min(self.MAX_RAYCAST, self.offset))
def _SenseValueAboutPosition(self, centrePosition, valueGetter): # Randomly sample value from SENSOR_POINTS chosen around the centrePosition. points = [Randomiser.random() * self.SENSOR_RADIUS for _ in range(self.SENSOR_POINTS)] for x in range(len(points)): angle = Randomiser.random() * 2 * np.pi points[x] = valueGetter(np.array([np.cos(angle) * points[x], np.cos(angle) * points[x]]) + centrePosition) # For some reason the color module hangs otherwise :/ if hasattr(points[x], "r"): points[x] = np.array([points[x].r, points[x].g, points[x].b]) total = points[0] for x in range(1, len(points)): total += points[x] return total / len(points)
def resetPositions(self): for i, robot in enumerate(self.robots): diff_radius = Randomiser.random() * self.BOT_SPAWN_RADIUS diff_angle = Randomiser.random() * 2 * np.pi robot.body.position = [ self.BOT_SPAWN_POSITION[i][0][0] * self.TILE_LENGTH + diff_radius * np.cos(diff_angle), self.BOT_SPAWN_POSITION[i][0][1] * self.TILE_LENGTH + diff_radius * np.sin(diff_angle), ] robot.body.angle = ( ( self.BOT_SPAWN_POSITION[i][1] + self.BOT_SPAWN_ANGLE[0] + Randomiser.random() * (self.BOT_SPAWN_ANGLE[1] - self.BOT_SPAWN_ANGLE[0]) ) * np.pi / 180 ) robot.body.velocity = (0.0, 0.0) robot.body.angular_velocity = 0
def calibrate(self): """ Set the sensor so the current bearing is interpreted as 0. Example usage: ``` >>> compass.calibrate() >>> # Rotates 10 degrees to the left >>> compass.value() 10 ``` """ self._setRelative() self.current_offset = 0 if ScriptLoader.RANDOMISE_SENSORS: self.current_sample_point = Randomiser.random() * self.NOISE_SAMPLE_HEIGHT