def is_collision(cls): ''' Return if a collision occured between the ships AND the intersection point. ''' pos_own = cls.own_ship.get_pos(scaled=True) pos_opp = cls.opp_ship.get_pos(scaled=True) offset = np.array(pos_own - pos_opp, dtype='int32') threshold = 1.5 * Dimension.scale(Spec.DIM_SHIP[0]) if get_norm(offset) > threshold: return # check collision mask_own = cls.own_ship.get_mask() mask_opp = cls.opp_ship.get_mask() mask_inter = mask_opp.overlap_mask(mask_own, offset) if mask_inter.count() > 0: cls.intersect = pos_opp + np.array(mask_inter.centroid()) return True else: return False
def compute_speed(self): '''Update the speed of the ship''' self.speed += self.acc norm_speed = get_norm(self.speed) if norm_speed == 0: return self.speed *= Spec.AIR_RESISTANCE**max(1, np.log10(norm_speed))
def get_acc(self, scalar=False): ''' Return the acceleration of the ship, if `scalar=False`: return the vector acceleration, else return the norm of the vector. ''' if scalar: return get_norm(self.acc) else: return self.acc
def get_speed(self, scalar=False): ''' Return the speed of the ship, if `scalar=False`: return the vector speed, else return the norm of the vector. ''' if scalar: return get_norm(self.speed) else: return self.speed
def check_collision_ship(cls, player, is_bullet_damage=True): ''' Check if ship has been hit by one of the bullet. If yes, handeln collision. ''' ship = player.ship threshold = Dimension.scale(get_norm(Spec.DIM_SHIP)) pos_ship = ship.get_pos(scaled=True) center_ship = ship.get_pos(center=True, scaled=True) for bullet in cls.bullets: if bullet.team == player.team or bullet.id in cls.recent_ids['id']: continue pos_bullet = bullet.get_pos(scaled=True) offset_to_center = np.array(pos_bullet - center_ship, dtype='int32') if get_norm(offset_to_center) > threshold: continue # check for collision mask_ship = ship.get_mask() offset = np.array(pos_bullet - pos_ship, dtype='int32') intersect = mask_ship.overlap(bullet.mask, offset) if not intersect is None: if not is_bullet_damage: bullet.damage = 0 ship.handeln_collision(bullet, pos_bullet) cls.handeln_collision_effect(bullet, pos_bullet)
def get_key_by_pos(self, pos): ''' Given a (unscaled) position, return the key of the nearest block. ''' distances = {} pos = np.array(pos) # compute every distances for key, center in self.abs_centers.items(): distances[key] = get_norm(center - pos) # get key of min distance key = min(distances.keys(), key=distances.get) return key