python - Getting dot to move along curved trajectory -
python - Getting dot to move along curved trajectory -
i'm trying create dot moves around screen, bounces off edges, , curves in random direction every 50 frames or so.
what i've done ball move , bounce off of screen edges. please note uses psychopy:
win = visual.window(size=(1600, 900), fullscr=false, screen=0, allowgui=false, allowstencil=false, units='pix', monitor='testmonitor', colorspace=u'rgb', color=[0.51,0.51,0.51]) keys = event.builderkeyresponse() dot1 = visual.circle(win=win, name='dot1',units='pix', radius=10, edges=32, ori=0, pos=(0,0), linewidth=1, linecolor='red', linecolorspace='rgb', fillcolor='red', fillcolorspace='rgb', opacity=1,interpolate=true) x_change = 10 y_change = 10 while true: dot1.pos+=(x_change,y_change) if dot1.pos[0] > 790 or dot1.pos[0] < -790: x_change = x_change * -1 if dot1.pos[1] > 440 or dot1.pos[1] < -440: y_change = y_change * -1 dot1.draw() win.flip() if event.getkeys(keylist=["escape"]): core.quit()
i imagine require trig, barely understand. point me in right direction? variables need, , how should manipulated?
the general strategy this: count frames within loop , alter x_change
, y_change
new angle on desired frame (e.g. every 50 frames). i'll utilize angle , speed explicitly set values of x_change
, y_change
using trigonometry:
# new stuff: import numpy np framen = 50 # set angle in first loop iteration speed = 14 # initial speed in whatever unit stimulus use. angle = 45 # initial angle in degrees x_change = np.cos(angle) * speed # initial y_change = np.sin(angle) * speed # initial while true: # more new stuff: alter direction angle (in degrees) if framen == 50: angle_current = np.rad2deg(np.arctan(y_change / float(x_change))) # float prevent integer partition angle_change = np.random.randint(-180, 180) # alter interval liking or set constant value or angle = angle_current + angle_change # new angle x_change = np.cos(angle) * speed y_change = np.sin(angle) * speed framen = 0 framen += 1 dot1.pos+=(x_change,y_change) if dot1.pos[0] > 790 or dot1.pos[0] < -790: x_change = x_change * -1 if dot1.pos[1] > 440 or dot1.pos[1] < -440: y_change = y_change * -1 dot1.draw() win.flip() if event.getkeys(keylist=["escape"]): core.quit()
options more randomness:
you can command speed (e.g. settingspeed = np.random.randint(1, 20)
) you can command frame alter angle @ next time (framen = np.random.randint(40, 60)
you can alter interval of angle alter noted above. python python-2.7 psychopy
Comments
Post a Comment