I am new to PsychoPy and am trying to figure out what is possible in the builder before I launch into the coder to try and make my life simpler while coding this project.
I currently have a stimulus envelope grating. I was wondering if there was a way to easily:
A: have the grating envelopes move around the screen
B: have the orientation of the gratings “slide” back and forth across a continuum of set end parameters
Not sure if any or all of this is possible, just asking for some insight before I jump into a new program. Thanks!
You can definitely move them around the screen! To make something move, you just need to use a Code component’s Each Frame tab to set
.pos according to
t is the time elapsed (in seconds) since the routine started, so if you wanted to move
myObject to the right over time you would do:
myObject.pos = t
To slow it down, speed it up, etc. you can transform t however you like, e.g. divide it by a constant to slow it down, multiply it by a constant to speed it up, add/subtract constants to change the start point. You could even use
+= to make it speed up exponentially!
I believe you can do the same thing with
.ori to change the orientation each frame, but I’m less familiar with envelope gratings so that one you may have to have a play with
Thanks for the advice!
So would this need to be coded in the coder? Or is it possible in the builder? The idea is to hopefully have multiple gratings bouncing off the walls of the screen and each other. I was curious if there were easy aspects of this available in the builder.
I am sure I will need to hardcode a lot of this. I was just trying to see what was available in the builder first, to make things as easy as possible before jumping into the coder.
For Todd’s example above, you could simply put
in the position field of your grating component, and set that field to “set every frame” for continuous updating.
For things that require more than a single code expression, as Todd notes, you can insert a code component in Builder (from the “custom” component panel). That allows you to insert small snippets of code that run at the right time (at the beginning of the experiment, of the routine, on each frame, etc). Very seldom will you need to program an experiment from scratch. Using code components to customise a Builder experiment generally allows enough control to have the best of both worlds (doing most things via the GUI while also being able to use custom code).
Thank you so much! I used your suggestions and now my gradient runs off the screen to the right! haha. Definitely a step in the right direction.
Any suggestions on randomizing direction for each trial? Not entirely sure how the randomizing work in python, especially in the builder.
Also if I were to try create “walls” around the edge of the screen for the grating to bounce off of. what type of custom component would you suggest? And how would I go about placing it? Thanks!
If you look at the beginning of Builder-generated script, you’ll see that it imports a number of functions from
from numpy.random import random, randint, normal, shuffle
There is lots of documentation available online on how to use those functions (e.g. https://numpy.org/doc/stable/reference/random/index.html). You could also import additional ones, like
choice to select between a list of multipliers like
[1, -1] to control a direction of movement.
That isn’t a custom component, just geometry. You just need to control the motion within the logic of your code. e.g. monitor the location of the stimulus. When it reaches, say, the right hand boundary of the screen, reverse its horizontal direction of motion.
e.g. in a code component, in the “begin routine” tab, randomly choose either 1 or -1 for each of the horizontal and vertical directions of motion. Also choose a starting x, y location. Also set a value for the velocity (i.e. the increment to applied on each frame).
In the “each frame” tab, add the direction * the velocity value to each component of the current stimulus location. If, say, the x component meets or exceeds the left or right hand boundary of the screen, multiply the horizontal direction value by -1. Do the same for the vertical coordinate. Your stimulus will now bounce off the walls.