# Animating a square in a sine-wave left to right fashion

Hi all!

My lab primarily used MATLAB in the past, but we are trying to transition to online experiments at the moment.

I am trying to mimic the experiment that was built in MATLAB but I am having some difficulty.

The main issue I am having now is in trying to present a square that oscillates around a center point from left to right.

Here is the code I am using:

``````from psychopy import visual,core,event
import numpy as np
import random

# Specify Window Size
win = visual.Window(
size = (1920,1080),
units='pix',
fullscr= True,
monitor = "default",
waitBlanking = True
)

clock = core.Clock()

# Get Center of Screen
xCenter = win.size/2

# Get Frame Interval
ifi = 1000/60

rect = visual.Rect(
win, # Where to display
units="pix", # What unit
width=100, # Original width
height=100,# Original height
fillColor=[-1,-1,-1], # Black
lineColor=[-1,-1,-1]  # Black
)

amplitude = 960 * 0.25
frequency = 0.2
angFreq = 2 * np.pi * frequency
startPhase = 0
time = 0

for practice in range(5):
timestart = core.getTime()
while ((core.getTime()-timestart) < 1):
xpos = amplitude * np.sin(angFreq * time + startPhase)
squareXpos = 0+xpos
rect.pos = (squareXpos,0)
rect.draw()
win.flip()
time = time + ifi
print(rect.pos)
``````

I am using this site as a reference point and trying to MATLAB > python/psychopy.

Currently the square move extremely fast from left to right but there are three simultaneous images at once. I want to just have one square that glides from left to right.

This is my first time posting here so if I am missing anything I apologize and I will add it ASAP!

Thank you!

Hi There,

Firstly, welcome to PsychoPy and discourse!!

Secondly, here is a demo that might help. movingPolygon.psyexp (4.4 KB) Here, a polygon is set to move in a circular motion (see the position field of the polygon component) - you will just need to substitute this field with your desired input.

If you are transitioning work online, I recommend starting from the builder view, as that will allow easier translation to online studies.

Whilst this might not completely answer your question, hopefully it provides a good starting point.

Becca

Hi Becca!

Thank you for the warm welcome! This actually is exactly what I was looking for! I removed the second argument and that seemed to accomplish the movement as I wanted.

Thank you again!