def trback(endpos, d, theta, dims, timestep = 1000, drawscreen = None):
    tnew = PhysicsTable(dims)
    v = d/timestep * 1000
    vx = v * math.cos(theta)
    vy = v * math.sin(theta)
    b = tnew.addBall(endpos,(vx,vy))
    for i in range(timestep):
        tnew.physicsstep()
        if drawscreen and (timestep % 50) == 0:
            drawscreen.blit(tnew.draw(),(0,0))
            pg.display.flip()
    pos = (b.x,b.y)
    tht = (math.atan(b.v[1]/b.v[0]) + math.pi) % (2*math.pi)
    bn = b.bounces
    if drawscreen: time.sleep(2)
    return pos,tht,bn
Exemple #2
0
def trback(endpos, d, theta, dims,exd = 0,fail = None, timestep = 1000, drawscreen = None):
    tnew = PhysicsTable(dims)
    v = (d+exd)/timestep * 1000
    tbch = int(1000*d/v)
    #print tbch, d, v
    vx = v * math.cos(theta)
    vy = v * math.sin(theta)
    b = tnew.addBall(endpos,(vx,vy))
    for i in range(timestep):
        tnew.physicsstep()
        if i == tbch: bn = b.bounces
        if fail and tnew.fails(fail): return None,None,None,None
        if drawscreen and (i % 20) == 0:
            drawscreen.blit(tnew.draw(),(0,0))
            pg.display.flip()
    pos = (b.x,b.y)
    # NOTE: Need to fix periodicity of pi here...
    tht = (math.atan(b.v[1]/b.v[0]) + math.pi)
    if b.v[0] < 0: tht += math.pi
    tht = tht % (2*math.pi)
    #bn = b.bounces
    runupsteps = tnew.sincecol
    runupd = v * runupsteps
    if drawscreen: time.sleep(2)
    return pos,tht,bn,runupd
Exemple #3
0
def trback(endpos, d, theta, dims,exd = 0,fail = None, timestep = 1000, drawscreen = None):
    tnew = PhysicsTable(dims, active = True)
    v = (d+exd)/timestep * 1000
    tbch = int(1000*d/v)
    #print tbch, d, v
    vx = v * math.cos(theta)
    vy = v * math.sin(theta)
    b = tnew.addBall(endpos,(vx,vy))
    for i in range(timestep):
        tnew.physicsstep()
        if i == tbch:
            bn = b.bounces
            tnew.deactivate()
            occpos = (b.x,b.y)
            occv = b.v
            tnew.sincecol = 0
        if fail and tnew.fails(fail): return None,None,None,None
        if drawscreen and (i % 20) == 0:
            drawscreen.blit(tnew.draw(),(0,0))
            pg.display.flip()
    pos = (b.x,b.y)
    # Find angle of velocity
    tht = (math.atan(b.v[1]/b.v[0]) + math.pi)
    if b.v[0] < 0: tht += math.pi
    tht = tht % (2*math.pi)
    # And for the velocity at occlusion
    occtht = (math.atan(occv[1]/occv[0]) + math.pi)
    if occv[0] < 0: occtht += math.pi
    occtht = occtht % (2*math.pi)

    # Find the number of bounces prior to occlusion
    postb = b.bounces - bn
    
    runupsteps = tnew.sincecol
    #print exd, runupsteps, (v*runupsteps/1000)
    runupd = exd - (v * runupsteps)/1000
    if drawscreen: time.sleep(2)
    return pos,tht,bn,runupd, occpos, occtht, postb
Exemple #4
0
def runinstructs(samples,screen,leadsteps):
    # Welcome and let people just move the paddle around
    if display_instructions(ipinstruct.welcome,screen,keymove = False): safequit()
    table = PhysicsTable((1000,900))
    table.addPaddle((50,450),paddlelen,'v',True)
    toffset = (int((screen.get_size()[0]-1000)/2),100 + int((screen.get_size()[1]-900-100)/2))
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)
            elif event.type == MOUSEBUTTONDOWN: running = False
            
        if pg.mouse.get_focused():
            mp = pg.mouse.get_pos()
            mpoff = (mp[0] - toffset[0], mp[1] - toffset[1])
            r = table.step(1/frrate,True,mpoff)
        else:
            r = table.step(1/frrate,False,None)

        screen.fill(pg.Color('White'))
        prtexttop("Click to continue",screen,toffset[1],toffset[0],toffset[0]+1000,None)
        tscreen = table.draw()
        screen.blit(tscreen,toffset)
        pg.display.flip()

    # Give instructions and show examples building in complexity
    if display_instructions(ipinstruct.basictrial,screen,keymove=False): safequit()
    runtrial(samples[0],screen,leadsteps,None,None,noocclude = True)
    
    if display_instructions(ipinstruct.slanttrial,screen,keymove=False): safequit()
    runtrial(samples[1],screen,leadsteps,None,None,noocclude = True)
    
    if display_instructions(ipinstruct.bouncetrial,screen,keymove=False): safequit()
    runtrial(samples[2],screen,leadsteps,None,None,noocclude = True)
    
    if display_instructions(ipinstruct.smalltrial,screen,keymove=False): safequit()
    runtrial(samples[3],screen,leadsteps,None,None,noocclude = True)
    
    if display_instructions(ipinstruct.largetrial,screen,keymove=False): safequit()
    runtrial(samples[4],screen,leadsteps,None,None,noocclude = True)
    
    if display_instructions(ipinstruct.occludetrial,screen,keymove=False): safequit()
    runtrial(samples[5],screen,leadsteps,None,None)
    
    if display_instructions(ipinstruct.preexamples,screen,keymove=False): safequit()
    for i in range(6,12):
        runtrial(samples[i],screen,leadsteps,None,None)

    if display_instructions(ipinstruct.postinstruct,screen,keymove=False): safequit()
Exemple #5
0
def runtrial(trial, screen, leads,score, sID,outfile = None, noocclude = False):
    its = 0
    prebounce = 0
    running = True
    catch = None
    failarea = pg.Rect((0,0),(55,trial.dim[1]))
    paddley = None
    toffset = (int((screen.get_size()[0]-trial.dim[0])/2),100 + int((screen.get_size()[1]-trial.dim[1]-100)/2))
    table = PhysicsTable(trial.dim, ball_rad = ballsize, badzone = failarea)
    
    table.addPaddle((50,trial.dim[1]/2),paddlelen,'v',True)
    targx = 50 + ballsize

    clock = pg.time.Clock()

    # Let the subject get set up
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)
            elif event.type == MOUSEBUTTONDOWN: running = False
            
        if pg.mouse.get_focused():
            mp = pg.mouse.get_pos()
            mpoff = (mp[0] - toffset[0], mp[1] - toffset[1])
            r = table.step(1/frrate,True,mpoff,xtarg = targx)
        else:
            r = table.step(1/frrate,False,None,xtarg = targx)

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop("Click to shoot the ball",screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        screen.blit(tscreen,toffset)
        pg.display.flip()

    # Add the ball and start the trial
    table.addBall(trial.initpos,trial.vel)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)

        if pg.mouse.get_focused():
            mp = pg.mouse.get_pos()
            mpoff = (mp[0] - toffset[0], mp[1] - toffset[1])
            r = table.step(1/frrate,True,mpoff,xtarg = targx)
        else:
            r = table.step(1/frrate,False,None,xtarg = targx)
        its += 1
        if its == leads: prebounce = table.ball.bounces

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop(None,screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        if its > leadsteps and noocclude == False: pg.draw.rect(tscreen,pg.Color('Grey'),Rect((53,1),(trial.dim[0]-54,trial.dim[1]-2)))
        screen.blit(tscreen,toffset)
        pg.display.flip()

        catch = table.catches()
        if catch == True or catch == False:
            running = False
            paddley = table.padend
            if paddley is None:
                print table.paddle.getcenter()[1]
                print table.ball.getpos()
                raise Exception('DID NOT CAPTURE PADDLE LOCATION')

    # Show results of trial
    if catch == True:
        fintxt = 'Nice catch! Click for the next trial'
        if score is not None: score += 1
    else: fintxt = 'You missed. Click for the next trial'
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)
            elif event.type == MOUSEBUTTONDOWN: running = False

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop(fintxt,screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        screen.blit(tscreen,toffset)
        pg.display.flip()

    # Write to output file
    if outfile: outfile.write(sID+','+str(trial.n)+','+trial.dimtxt+','+str(trial.dist)+','+str(trial.bounces)+','+str(catch)+','+str(trial.endpt[1])+','+str(paddley)+'\n')

    pixelsoff = abs(paddley - trial.endpt[1])
    
    return catch, pixelsoff
    tht = (math.atan(b.v[1]/b.v[0]) + math.pi) % (2*math.pi)
    bn = b.bounces
    if drawscreen: time.sleep(2)
    return pos,tht,bn

if __name__ == '__main__':
    pg.init()
    screen = pg.display.set_mode((800,600))
    clock = pg.time.Clock()
    running = True

    stpt = (50,150)
    tabledim = (800,400)
    frrate = 50

    table = PhysicsTable(tabledim)
    #table.addBasket(stpt,(40,100),'r')
    #pg.mouse.set_visible(False)

    d = 800
    v = 50
    #ts = [.25*math.pi,1.75*math.pi]
    #tdim = (tabledim[0] - table.rad, tabledim[1] - table.rad)
    for i in range(1):
        thend = ((random.random()-0.5) * math.pi) % (2*math.pi)
        #print thend
        #thend = ts[i]
        bpt, theta, n = trback((90,200),d,thend,tabledim,drawscreen = None)
        print theta
        vx = v * math.cos(theta)
        vy = v * math.sin(theta)
Exemple #7
0
        dist = ds[t[1]]
        bounce = t[2]
        failer = pg.Rect((0,0),(49,dm[1]))
        needsfinding = True

        while needsfinding:
            # Randomly initialize where to end up
            epos = (xend,random.randint(int(dm[1]*.2),int(dm[1]*.8)))
            thend = ((random.random()-0.5) * 0.9 * math.pi) % (2*math.pi)

            spos, etht, bn, runup = trback(epos,dist,thend,dm, extrad,failer)

            # Test whether it fits the number of bounces & run up distance
            if spos is not None and bn == bounce and runup > 200:
                # Then make sure it simulates out correctly
                fwdtbl = PhysicsTable(dm, badzone = pg.Rect((0,0),(xend,dm[1])))
                bvel = (v*math.cos(etht),v*math.sin(etht))
                fwdtbl.addBall(spos,bvel)
                running = True
                its = 0
                prebounce = 0
                while running:
                    fwdtbl.step(1/frrate,False)
                    its += 1
                    if its == leadsteps:
                        prebounce = fwdtbl.ball.bounces
                    pball = fwdtbl.ball.getpos()
                    if pball[0] <= epos[0]:
                        running = False
                        dx = pball[0]-epos[0]
                        dy = pball[1]-epos[1]