示例#1
0
def createBalls(win, nBalls, radius, boundRect, speed):
    """ Create the balls to be put in the area """
    halfX = (boundRect[0] + boundRect[2]) / 2.0
    halfY = (boundRect[1] + boundRect[3]) / 2.0
    # The four rectangles that build the main rectangle
    rectanglesList = []
    rectanglesList.append([boundRect[0], boundRect[1], halfX, halfY])
    rectanglesList.append([halfX, boundRect[1], boundRect[2], halfY])
    rectanglesList.append([boundRect[0], halfY, halfX, boundRect[3]])
    rectanglesList.append([halfX, halfY, boundRect[2], boundRect[3]])

    # Generate the balls in the
    balls = []
    from random import shuffle
    from numpy.random import uniform
    from common import Geometry
    for i in range(0, nBalls):
        startPos = np.array(
            [uniform(
                rectanglesList[
                    i % 4][0] + radius, rectanglesList[i % 4][2] - radius),
             uniform(rectanglesList[i % 4][1] + radius, rectanglesList[i % 4][3] - radius)])
        direction = Geometry.normalized(
            np.array([uniform(-1, 1), uniform(-1, 1)]))
        ball = Ball(win, position=startPos, direction=direction,
                    speed=speed, radius=radius, color='Black')
        balls.append(ball)

    shuffle(balls)
    return balls, rectanglesList
def preGenerateTrajectories(ballSpeed, expInfo, allBallsList, rectangles,
                            repulsionStrength, edgerepulsionStrength,
                            centerAttraction):
    """
    Precompute the trajectories of points
    """
    trajClock = core.Clock()
    trajClock.reset()
    nFrames = int(expInfo['Duration'] / (1.0 / 60.0))
    fullFrame = [None] * nFrames  # preallocate space for the list
    from common import Geometry
    from numpy import array
    for i in range(0, nFrames):
        fullFrame[i] = dict()
        for ballListID, ballList in allBallsList.iteritems():
            boundingRectangle = rectangles[ballListID]
            for ball1 in ballList:
                force = array([0, 0])
                for ball2 in ballList:
                    if (ball1 == ball2):
                        continue
                    # balls collide
                    delta = ((ball1.pos() - ball2.pos()))
                    dist2 = ((delta**2).sum())
                    force = force + repulsionStrength * \
                        (ball1.radius * ball2.radius) * delta / (dist2 * dist2)

                # Repulsion to borders
                force[0] = force[0] + edgerepulsionStrength / \
                    ((boundingRectangle[0] - ball1.pos()[0]) ** 2)
                force[0] = force[0] - edgerepulsionStrength / \
                    ((boundingRectangle[2] - ball1.pos()[0]) ** 2)

                force[1] = force[1] + edgerepulsionStrength / \
                    ((boundingRectangle[1] - ball1.pos()[1]) ** 2)
                force[1] = force[1] - edgerepulsionStrength / \
                    ((boundingRectangle[3] - ball1.pos()[1]) ** 2)

                # Add a little attraction to the center of boundingRectangle
                dispFromCenter = Geometry.rectangleCenter(boundingRectangle)
                force = force - (ball1.pos() - dispFromCenter)

                ball1.direction = ball1.direction + centerAttraction * force

                # Renormalize direction with correct speed
                ball1.direction = Geometry.normalized(
                    ball1.direction) * ballSpeed

                # Move the ball in the computed direction and finally draw it
                ball1.move(ball1.direction)
                # if (i==1): #print displacement for debug purpose
                # print np.np.sqrt(np.dot(ball1.direction,ball1.direction))*60
                fullFrame[i][ball1] = ball1.pos()

    return fullFrame