def __init__(self): super(PhysicsObject, self).__init__() self.forces = [] self.mass = 1 self.accel = QVector2D(0.0, 0.0) self.velocity = QVector2D(0.0, 0.0) return
def __init__(self): self.worldPosition = QVector2D(0.0, 0.0) self.worldRotation = 0.0 self.worldScale = QVector2D(1.0, 1.0) self.worldTransform = QTransform() self.parent = None self.children = [] self.transformChanged = True self.attachedObject = None self.aabb = QRectF(0.0, 0.0, 0.0, 0.0) self.update_transform() return
def computeAim(self): middle = QVector2D(500, 500) delta_vec = middle - self.lastSighting['pos'] if delta_vec.length() != 0: delta_vec *= 200 / delta_vec.length() return self.lastSighting['pos'] + delta_vec else: return self.lastSighting['pos']
def spawnSceneEdges(self): scene_edge = SceneEdgeRemover() self.scene.spawn(scene_edge) scene_edge.set_world_position(QVector2D(-500, 0)) scene_edge = SceneEdgeRemover() self.scene.spawn(scene_edge) scene_edge.set_world_rotation(90) scene_edge.set_world_position(QVector2D(0, -500)) scene_edge = SceneEdgeRemover() self.scene.spawn(scene_edge) scene_edge.set_world_rotation(90) scene_edge.set_world_position(QVector2D(1000, -500)) scene_edge = SceneEdgeRemover() self.scene.spawn(scene_edge) scene_edge.set_world_position(QVector2D(-500, 1000)) return
def spawn_boid(self): w = random.randint(40, self.scene.get_width() - 40) h = random.randint(40, self.scene.get_height() - 40) r = random.randint(0, 360) sx = random.random() * 0.2 + 0.9 sy = random.random() * 0.2 + 0.9 boid = Boid(QColor(200, 100, 100)) self.scene.spawn(boid) boid.set_world_position(QVector2D(w, h)) boid.set_world_rotation(r) boid.set_world_scale(QVector2D(sx, sy)) f = Physics.Force() f.direction = QVector2D(0.0, 1.0) f.isDirectionLocal = True f.accelPart = random.random() * 0.0001 boid.add_force(f) return
def run(self): while True: if self.robotsInView != {}: vecs = [] myPosition = QVector2D(self.x, self.y) for id in self.targetIds: robot = self.robotsInView[id] v = (myPosition - robot['pos']).normalized() v *= (1 / robot['dist']) vecs.append(v) wall_vecs = [] distances = [] for rect in self.wallsInView: rect_center = QVector2D(rect.center().x(), rect.center().y()) direction = (myPosition - rect_center).normalized() distance = (myPosition - rect_center).length() if distance < 200: wall_vecs.append(direction) distances.append(distance) if len(distances) == 0: avg_distance = 1000 else: avg_distance = sum(distances) / len(distances) result_wall_vec = sumvectors(wall_vecs).normalized() result_wall_vec *= 3 * ( 1 / avg_distance ) # wall vector counts 3 times as much as a robot vecs.append(result_wall_vec) self.aim_direction = sumvectors(vecs).normalized() self.moveInDirection(self.aim_direction) self.msleep(DAEMON_SLEEP)
def __init__(self, robotId, targetId): super().__init__(robotId) self.targetId = targetId self.lastSighting = { 'x': 500, 'y': 500, 'pos': QVector2D(0, 0), 'dist': 0, 'angle': 0, 'timestamp': 1000 } self.previousSighting = { 'x': 501, 'y': 501, 'pos': QVector2D(0, 0), 'dist': 0, 'angle': 0, 'timestamp': 0 } self.aimPos = QVector2D(500, 500) self.state = "Searching"
def update_physics(self, ms): arad = self.get_world_rotation() * math.pi / 180.0 sina = math.sin(arad) cosa = math.cos(arad) # compute total force and apply total_force_accel = QVector2D(0.0, 0.0) total_force_vel = QVector2D(0.0, 0.0) for force in self.forces: direction = force.direction if force.isDirectionLocal: direction = QVector2D(direction[0] * cosa - direction[1] * sina, direction[1] * cosa + direction[0] * sina) total_force_accel = total_force_accel + force.accelPart * direction total_force_vel = total_force_vel + force.velPart * direction vel_from_accel = self.accel vel_from_forces = total_force_vel / self.mass self.accel = total_force_accel / self.mass self.velocity += vel_from_accel * ms final_vel = self.velocity + vel_from_forces self.move(final_vel * ms) return
def circleRectCollision(pos, r, rect): rect_center = QVector2D(rect.center()) vec = pos - rect_center length = vec.length() # scale the vector so it has length l-r vec *= (length - r) / length # This is the point of the circle which is closest to the rectangle closest_point = (rect_center + vec).toPointF() if rect.contains(closest_point): if pos.x() >= rect_center.x() and pos.y() >= rect_center.y(): corner = rect.bottomRight() elif pos.x() >= rect_center.x() and pos.y() <= rect_center.y(): corner = rect.topRight() elif pos.x() <= rect_center.x() and pos.y() >= rect_center.y(): corner = rect.bottomLeft() elif pos.x() <= rect_center.x() and pos.y() <= rect_center.y(): corner = rect.topLeft() overlap = QVector2D(corner - closest_point) return overlap else: return None
def sumvectors(vecs): result = QVector2D(0, 0) for v in vecs: result += v return result
def angleToVector(angle): return QVector2D(math.cos(math.radians(angle)), math.sin(math.radians(angle)))
def get_world_direction(self): arad = self.get_world_rotation() * math.pi / 180.0 return QVector2D(math.sin(arad), math.cos(arad))
class SceneObject(QObject): frontDirection = QVector2D(0.0, 1.0) def __init__(self): super(SceneObject, self).__init__() self.node = None self.model = None self.drawAabb = False return def spawn(self, scene_node): assert self.node is None self.node = scene_node scene_node.attach_object(self) return def despawn(self): if self.node is not None: self.node.detach_object(self) self.node = None return def draw(self, painter): self.model.draw(painter, self.node.get_world_transform()) if self.is_draw_aabb(): painter.setBrush(QBrush(QColor(255, 255, 255))) painter.drawRect(self.get_world_aabb()) return def set_model(self, model): self.model = model return def get_model(self): return self.model def get_scene_node(self): return self.node def get_aabb(self): return self.model.aabb def get_world_aabb(self): if self.node is None: return QRectF() else: return self.node.get_world_transform().mapRect(self.get_aabb()) def set_world_position(self, pos): self.node.set_world_position(pos) return def move(self, pos): self.node.set_world_position(pos + self.node.get_world_position()) return def get_world_position(self): return self.node.get_world_position() def move_local(self, move): # Transform 'move' to objects local coords (rotate it) arad = self.get_world_rotation() * math.pi / 180.0 sina = math.sin(arad) cosa = math.cos(arad) self.move( (move[0] * cosa + move[1] * sina, move[1] * cosa + move[0] * sina)) return def set_world_rotation(self, angle): self.node.set_world_rotation(angle) return def rotate(self, angle): self.node.set_world_rotation(self.node.get_world_rotation() + angle) return def get_world_rotation(self): return self.node.get_world_rotation() def set_world_scale(self, scale): self.node.set_world_scale(scale) return def scale(self, scale): self.node.set_world_scale(self.node.get_world_scale() * scale) return def get_world_scale(self): return self.node.get_world_scale() def get_world_direction(self): arad = self.get_world_rotation() * math.pi / 180.0 return QVector2D(math.sin(arad), math.cos(arad)) def set_world_direction(self, dir): arad = math.atan2(dir[0], dir[1]) self.set_world_rotation(arad * 180.0 / math.pi) return def is_draw_aabb(self): return self.drawAabb def set_draw_aabb(self, value): self.drawAabb = value return
def __init__(self, robotId, targetIds): super().__init__(robotId) self.targetIds = targetIds self.aim_direction = QVector2D(1, 0)
def __init__(self, accel=0.0, vel=0.0, dir=QVector2D(0.0, 1.0)): self.accelPart = accel self.velPart = vel self.direction = dir self.isDirectionLocal = True return