Exemplo n.º 1
0
def on_update(dt):
    # going to store the old position, for collision purposes
    old_pos = vec2d(cell.x, cell.y)

    # move the cell towards it's velocity
    cell.x += cell.velocity[0] * dt
    cell.y += cell.velocity[1] * dt

    # check for collisions against wall
    # if one is found, get new position, to allow "sliding"
    # against them
    radius = cell_image.width / 2 * 0.8
    i = 0
    if cell.velocity != (0, 0):
        while i < len(lines):
            p1 = vec2d(lines[i], lines[i + 1])
            p2 = vec2d(lines[i + 2], lines[i + 3])
            line = p2 - p1
            normal = line.perpendicular_normal()
            vec = (cell.x, cell.y) + radius * normal
            intersects = line_intersect(old_pos, vec, p1, p2)
            if not intersects is None:
                new_pos = calc_new_pos(old_pos, vec, p1, p2)
                new_pos -= radius * normal
                cell.x = new_pos.x
                cell.y = new_pos.y
            elif circle_point_intersect(cell.position, radius, p1):
                direction = cell.position - p1
                length = direction.length
                move_by_distance = radius - length
                new_pos = cell.position + direction.normalized() * move_by_distance
                cell.x = new_pos.x
                cell.y = new_pos.y
            i += 4