def apply_drag(self, strength=0.1): neg_x = self.velocity.x * -1 neg_y = self.velocity.y * -1 drag_vector = Vector(neg_x, neg_y) drag_vector.normalize() drag_magnitude = self.velocity.magnitude_sq * strength drag_force = drag_vector * drag_magnitude self.apply_force(drag_force)
def __init__(self, start_x=width / 2, start_y=height / 2): self.location = Vector(start_x, start_y) self.acceleration = Vector(0.01, 0.01) self.desired_velocity = Vector(0, 0) self.max_speed = 10 self.max_turning = 0.5 self.view_range = 100 centre = Vector(width / 2, height / 2) direction = Vector(self.location.x, self.location.y) - centre direction.normalize() self.velocity = direction * self.max_speed self.set_colour()
def align(self, vehicles, method="max"): sum = Vector(0, 0) count = 0 for other in vehicles: d = Vector.distance(self.location, other.location) if d > 0 and d < self.view_range: sum += other.velocity count += 1 if count > 0: sum /= count if method == "max": sum.normalize() sum *= self.max_speed if method == "average": pass steering_force = sum - self.velocity steering_force.limit(self.max_turning) self.applyForce(steering_force)
def boundaries(self, w, h): d = 50 desired = None if self.pos.x < d: desired = Vector(self.maxspeed, self.vel.y) elif self.pos.x > (w - d): desired = Vector(-self.maxspeed, self.vel.y) if self.pos.y < d: desired = Vector(self.vel.x, self.maxspeed) elif self.pos.y > (h - d): desired = Vector(self.vel.x, -self.maxspeed) if desired: desired.normalize() desired *= self.maxspeed steer = desired - self.vel steer.limit(self.maxforce) self.applyForce(steer)