【问题标题】:Getting dot to move along curved trajectory让点沿曲线轨迹移动
【发布时间】:2014-10-25 17:55:04
【问题描述】:

我正在尝试创建一个在屏幕上移动、从边缘反弹并每隔 50 帧左右沿随机方向弯曲的点。

我这样做是为了让球不断移动并从屏幕边缘反弹。请注意,这使用了 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()

我想这需要一些我几乎不明白的三角函数。 谁能指出我正确的方向?我需要什么变量,应该如何操作它们?

【问题讨论】:

  • 我很好奇,你希望曲线是多少?

标签: python python-2.7 psychopy


【解决方案1】:

一般策略是这样的:计算循环内的帧数并将x_changey_change 更改为所需帧上的新角度(例如每 50 帧)。我将使用三角函数明确地使用角度和速度来设置x_changey_change 的值:

# New stuff:
import numpy as np
frameN = 50  # To set angle in first loop iteration
speed = 14  # initial speed in whatever unit the 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: Change direction angle (in degrees)
    if frameN == 50:
        angle_current = np.rad2deg(np.arctan(y_change / float(x_change)))  # float to prevent integer division
        angle_change = np.random.randint(-180, 180)  # change interval to your liking or set to a constant value or something
        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()

更多随机性的选项:

  • 您可以控制速度(例如设置speed = np.random.randint(1, 20)
  • 您可以控制下次改变哪个框架(frameN = np.random.randint(40, 60)
  • 您可以如上所述更改角度变化的间隔。

【讨论】:

  • 谢谢,太好了!
猜你喜欢
  • 1970-01-01
  • 2021-06-02
  • 2011-08-06
  • 2021-02-25
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多