def apply_force_from_coords(ox, oy, position, current_drone): """Apply a simple short range repulsive force from a particle at the given coordinates on this particle.""" ax = 0 ay = 0 radius = 4 empty = PVector(0, 0) dx = ox - position.x dy = oy - position.y if dx == dy == 0: return empty.return_as_vector( ) # no directional force from particle at same location r2 = max(dx * dx + dy * dy, current_drone.min_r2) if r2 > radius: return empty.return_as_vector() # out of force range r = math.sqrt(r2) # Very simple short range repulsive force coef = (1 - current_drone.cutoff / r) / r2 / current_drone.mass ax += coef * dx ay += coef * dy direction = PVector(ax, ay) direction.normalize() return direction.return_as_vector()
def tend_to_place(agents, current): target = PVector(0, -14) position = PVector(current.xyz[0], current.xyz[1]) target.subVector(position) target.divScalar(50) target.normalize() return target.return_as_vector()
def __init__(self, m, x, y): self.mass = m self.radius = 16 self.color = (239, 242, 63) self.location = PVector(x, y) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0)
def __init__(self, origin, direction, speed, btype): pygame.sprite.Sprite.__init__(self, self.containers) self.btype = btype if btype == "fast": self.image = Bullet.fastimage self.rect = self.image.get_rect() elif btype == "strong": self.image = Bullet.strongimage self.rect = Bullet.strongrect elif btype == "tenseconds": self.image = Bullet.tensecondsimage self.rect = Bullet.tensecondsrect else: self.image = Bullet.fastimage self.rect = Bullet.fastrect self.rect = self.image.get_rect() self.origin = PVector(origin[0], origin[1]) self.aim = PVector(direction[0], direction[1]) self.speed = speed self.direction = self.aim - self.origin self.direction.mag = speed self.pos = [0, 0] self.pos[0] = self.origin[0] self.pos[1] = self.origin[1] self.rect = self.image.get_rect() self.mask = pygame.mask.from_surface(self.image) self.rect.center = (self.pos[0], self.pos[1])
def __init__(self, m, x, y): self.mass = m self.radius = m * 5 self.color = random.choice(colorlist) self.location = PVector(x, y) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0)
def update(self): if not self.zombified: if not self.chased_by: # if not being chased, just make a random movement r = random() if r >= 0.50: self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed)) else: # move away from the zombie chasing this human change_x = 0 change_y = 0 if self.chased_by.location.x > self.location.x: change_x = -self.speed elif self.chased_by.location.x < self.location.x: change_x = self.speed if self.chased_by.location.y > self.location.y: change_y = -self.speed elif self.chased_by.location.y < self.location.y: change_y = self.speed self.velocity = PVector(change_x, change_y) Being.update(self)
def __init__(self, x, y, image_path=""): super(Sprite, self).__init__(x, y) # Placeholder for an image for the character # TODO: Replace this with an animation object that returns an image # every frame self.width = 32 self.height = 42 self.image = pygame.Surface([self.width, self.height]) self.image.fill(pygame.Color('cyan')) # Collider self.collider_t = Collider(self, 10, 10, xoff=0, yoff=-25) self.collider_b = Collider(self, 10, 10, xoff=0, yoff=25, color='blue') self.collider_rt = Collider(self, 10, 10, xoff=20, yoff=-15, color='red') self.collider_rb = Collider(self, 10, 10, xoff=20, yoff=15, color='red') self.collider_lt = Collider(self, 10, 10, xoff=-20, yoff=-15, color='yellow') self.collider_lb = Collider(self, 10, 10, xoff=-20, yoff=15, color='yellow') self.colliders = [ self.collider_t, self.collider_b, self.collider_rt, self.collider_rb, self.collider_lt, self.collider_lb ] # Possible states of the character. self.movingleft = False self.movingright = False self.jumping = False # Constant values that determine the movement of the character self.acc_force_val = 1.5 self.fri_force_val = .25 self.grv_force_val = .2 self.dec_force_val = 3 self.jmp_force_val = 10 self.jmp_force_val_end = 4 self.acc_force = PVector(self.acc_force_val, 0) self.dec_force = PVector(self.dec_force_val, 0) self.max_velo = 6
def __init__(self, im): super().__init__(im) # self.pos = (x, y) self.im = im self.rotspeed = randint(-5, 5) self.angle = 90 self.location = PVector(randint(0, HEIGHT), randint(0, WIDTH)) self.velocity = PVector(0, 0) self.topspeed = randint(6, 12)
def __init__(self): self.radius = 16 # Position und Velocity self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63)
def __init__(self, location=PVector(0, 0), velocity=PVector(0, 0), acceleration=PVector(0, 0), topspeed=10): self.location = location self.velocity = velocity self.acceleration = acceleration self.topspeed = topspeed
def __init__(self): super().__init__(WIDTH, HEIGHT, TITLE) arcade.set_background_color((49, 197, 244)) self.movers = [] for _ in range(NO_MOVERS): self.movers.append(Mover(random.uniform(0.5, 2.5), 15, HEIGHT - 15)) self.wind = PVector(0.01, 0.0) self.gravity = PVector(0, -0.1)
def __init__(self, canvas, width, height): self.canvas = canvas self.color = (0, 0, 200) self.speed = 4 self.sight_dist = 10 self.location = PVector(randint(0, width), randint(0, height)) self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed))
def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(0, 0) self.topspeed = 10
def __init__(self, x, y): self.acceleration = PVector(0, 0) self.velocity = PVector(0, 0) self.location = PVector(x, y) self.color = (98, 199, 119) self.r = 8.0 self.maxspeed = 5 self.maxforce = 0.1 self.d = 25
def __init__(self): super().__init__(WIDTH, HEIGHT, TITLE) arcade.set_background_color(((149, 224, 245))) self.movers = [] for i in range(NO_MOVERS): self.movers.append(Mover(random.uniform(0.5, 2.5), 15, HEIGHT - 15)) m = self.movers[i].mass self.gravity = PVector(0, -0.1*m) self.wind = PVector(0.005, 0.0)
def __init__(self): self.radius = 16 # Position, Velocity, and Acceleration self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) self.acceleration = PVector(0.001, -0.01) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63)
def __init__(self): # Position and Velocity self.location = PVector(WIDTH / 2, HEIGHT / 2) self.prev_location = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) # Farbe self.color = (255, 255, 178) self.count = 0 self.paused = False self.stepsize = 4
def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(random.uniform(-2, 2), random.uniform(-2, 2)) # self.acceleration = PVector(-0.001, 0.01) self.topspeed = 10
def __init__(self): self.radius = 16 # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-2, 2) v_y = random.randrange(-2, 2) self.velocity = PVector(v_x, v_y) # Farbe self.color = (239, 242, 63)
def __init__(self): self.radius = random.randrange(10, 20) # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Farbe self.color = random.choice(colorlist)
def __init__(self, tshape, tcolor, x, y): t.Turtle.__init__(self) self.penup() self.shape(tshape) self.color(tcolor) self.speed = 1 self.max_speed = 10 self.location = PVector(x, y) self.setpos(self.location.x, self.location.y) self.acceleration = PVector(0, 0.05) self.velocity = PVector(r.uniform(-1.0, 1.0), r.uniform(-2.0, 0.0)) self.lifespan = 255
def __init__(self): self.radius = random.randrange(10, 20) self.mouse = PVector(200, 200) # Position und Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = random.choice(colorlist)
def velavg (uav_name,blockListDict): alt_d=8 position=PVector(current.xyz[0],current.xyz[1]) close_drones=find_neighbours_in_radius(current,1000) if len(close_drones)==0: empty=PVector(0,0) velocity=PVector(current.v_ned_d[0],current.v_ned_d[1]) current.set_v_2D_alt_lya(velocity.return_as_vector(),-alt_d) return empty.return_as_vector() velx=sum_all_vel_x(close_drones,current) velx=(velx/len(close_drones)) vely=sum_all_vel_x(close_drones,current) vely=(vely/len(close_drones)) vel_vector=numpy.array([velx,vely]) vel_vector=normalize(vel_vector) return vel_vector
def separation(current): alt_d = 8 position = PVector(current.xyz[0], current.xyz[1]) close_drones = find_neighbours_in_radius(current, 1000) if len(close_drones) == 0: empty = PVector(0, 0) velocity = PVector(current.v_ned_d[0], current.v_ned_d[1]) current.set_v_2D_alt_lya(velocity.return_as_vector(), -alt_d) return empty.return_as_vector() dx = sub_all_x(close_drones, current) dx = (dx / len(close_drones)) dy = sub_all_y(close_drones, current) dy = (dy / len(close_drones)) sep_vector = numpy.array([dx, dy]) sep_vector = normalize(sep_vector) return sep_vector
def seek(target, position): desired = target c_position = PVector(position[0], position[1]) desired.subVector(c_position) #A vector pointing from the position to the target #Scale to maximum speed desired.normalize() #desired.mult(maxspeed); return desired
def separation (uav_name,blockListDict): alt_d=8 pos=get_position(uav_name,blockListDict) close_drones=blockListDict position=PVector(pos[0],pos[1]) #close_drones=find_neighbours_in_radius(current,1000) if len(close_drones)==0: empty=PVector(0,0) #velocity=PVector(current.v_ned_d[0],current.v_ned_d[1]) #current.set_v_2D_alt_lya(velocity.return_as_vector(),-alt_d) return empty.return_as_vector() dx=sub_all_x(uav_name,blockListDict) dx=(dx/len(blockListDict)) dy=sub_all_y(uav_name,blockListDict) dy=(dy/len(blockListDict)) sep_vector=numpy.array([dx,dy]) sep_vector=normalize(sep_vector) return sep_vector
def track(self, enemies): if not self.btype == "tenseconds": return closest = None for e in enemies: vector = PVector(e.pos[0], e.pos[1]) - PVector( self.pos[0], self.pos[1]) if closest == None or vector.mag < closest.mag: closest = vector self.direction = closest if self.direction == None: mpos = pygame.mouse.get_pos() self.direction = PVector(mpos[0], mpos[1]) - self.origin self.direction.mag = self.speed
def cohesion(current): alt_d = 8 position = PVector(current.xyz[0], current.xyz[1]) close_drones = find_neighbours_in_radius(current, 1000) if len(close_drones) == 0: empty = PVector(0, 0) velocity = PVector(current.v_ned_d[0], current.v_ned_d[1]) current.set_v_2D_alt_lya(velocity.return_as_vector(), -alt_d) return empty.return_as_vector() sx = sum_all_x(close_drones, current) sx = (sx / len(close_drones)) sx = sx - current.xyz[0] sy = sum_all_y(close_drones, current) sy = (sy / len(close_drones)) sy = sy - current.xyz[1] cohesion_vec = numpy.array([(sx - position.x), (sy - position.y)]) cohesion_vec = normalize(cohesion_vec) return cohesion_vec
def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() mouse = PVector(mouse_x, mouse_y) dir = mouse - self.location dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity)
def cohesion(boids, current_drone): neighbordist = 80 sum_vector = PVector(0, 0) count = 0 position = current_drone.xyz c_vector = PVector(position[0], position[1]) for other in boids: if other.tag != current_drone.tag: other_position = other.xyz o_vector = PVector(other_position[0], other_position[1]) distance = c_vector.distance(o_vector) if distance < neighbordist: sum_vector.addVector(o_vector) count = count + 1 if count > 0: sum_vector.divScalar(count) return seek(sum_vector, current_drone.xyz).return_as_vector() else: return PVector(0, 0).return_as_vector()