def object_collision(sprite1, sprite2): normal = [sprite1.origin[0] - sprite2.origin[0], sprite1.origin[1] - sprite2.origin[1]] unit_normal = vector.unit(normal) unit_tangent = [-unit_normal[1], unit_normal[0]] neg_unit_normal = vector.times(-1, unit_normal) sprite1.pos = vector.plus(sprite1.pos, unit_normal) sprite1.set_position(sprite1.pos) sprite2.pos = vector.plus(sprite2.pos, neg_unit_normal) sprite2.set_position(sprite2.pos) v1n = vector.dotp(unit_normal, sprite1.speed) v1t = vector.dotp(unit_tangent, sprite1.speed) v2n = vector.dotp(unit_normal, sprite2.speed) v2t = vector.dotp(unit_tangent, sprite2.speed) u1n = (v1n * (sprite1.mass - sprite2.mass) + 2 * sprite2.mass * v2n) / (sprite1.mass + sprite2.mass) u2n = (v2n * (sprite2.mass - sprite1.mass) + 2 * sprite1.mass * v1n) / (sprite1.mass + sprite2.mass) sprite1_tangent = vector.times(v1t, unit_tangent) sprite1_normal = vector.times(u1n, unit_normal) sprite2_tangent = vector.times(v2t, unit_tangent) sprite2_normal = vector.times(u2n, unit_normal) sprite1.speed = vector.plus(sprite1_tangent, sprite1_normal) sprite2.speed = vector.plus(sprite2_tangent, sprite2_normal)
def can_go(self, terrain, direction): distance_vector = vector.times(1.5 * terrain.tile_size + 1, direction) tile_pos = vector.plus(distance_vector, self.origin) tile = terrain.get_tile(tile_pos) if tile.color == colors['grass']: return False else: return True
def surrounding_tiles(self, tile, multiplier=1): surrounding = [] for direction in compass.DIRECTIONS: distance = vector.times(self.tile_size * multiplier, direction) pos = vector.plus(tile.origin, distance) found_tile = self.get_tile(pos) if found_tile.size != 0: surrounding.append(found_tile) return surrounding
def SGD(users,items,record,speed,penalty): ''' Pu is the user's feature vector Qi is the item's feature vector e is the prediction error the default rate for each is 1 SGD will iterate update Pu and Qi based on the following: Pu <--- Pu + speed * (e * Qi - penalty * Pu) Qi <--- Qi + speed * (e * Pu - penalty * Qi) ''' Pu = users[record[0]]['feature'] Qi = items[record[1]]['feature'] e = float(record[2]) - vector.dot(Pu,Qi) pu = vector.plus(Pu , vector.num_dot(speed , vector.sub(vector.num_dot(e , Qi) , vector.num_dot(penalty , Pu) ) ) ) Qi = vector.plus(Qi , vector.num_dot(speed , vector.sub(vector.num_dot(e , Pu) , vector.num_dot(penalty , Qi) ) ) ) Pu = pu users[record[0]]['feature'] = Pu items[record[1]]['feature'] = Qi
def apply_speed(self): self.pos = vector.plus(self.pos, self.speed) self.set_position(self.pos)
def apply_speed(self): movement = vector.times(self.speed, self.direction) new_pos = vector.plus(self.pos, movement) self.set_position(new_pos) self.distance_travelled += vector.mag(movement)
def move(self): self.pos = vector.plus(self.pos, self.speed) self.set_position(self.pos)
def grab(self): mouse = pygame.mouse.get_pos() #if self.rect.collidepoint(mouse): direction_vector = [mouse[0] - self.origin[0], mouse[1] - self.origin[1]] unit_direction = vector.unit(direction_vector) self.speed = vector.plus(self.speed, unit_direction)