Move objects along axis

Hi,

Have spent a few days introducing myself to both Matlab and PsychToolbox using the various guides and online tutorials as I need to build an experiment. Having cribbed and tweaked bits of the pbtutorials I have now manged to get three coloured squares evenly spaced at the top of the screen (code pasted below). So far so good. My next task is to have those squares move at an independently controllable speed from their position at the top of the screen to one at the bottom (move down the y, stay constant on the x). I’m (maybe foolishly) assuming that this is relatively simple to code but all the guides seem to be coding for something more complex. I was hoping the “Moving Line Demo” would help me out but whilst I can run the demo I can’t view the code (or don’t know how). Any pointers would be much appreciated, thank you.

sca;
close all;
clearvars;

PsychDefaultSetup(2);

screens = Screen(‘Screens’);

screenNumber = max(screens);

white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);

[window, windowRect] = PsychImaging(‘OpenWindow’, screenNumber, white/2);

[screenXpixels, screenYpixels] = Screen(‘WindowSize’, window);

[xCenter, yCenter] = RectCenter(windowRect);

baseRect = [0 0 100 100];

squareXpos = [xCenter 0.25 xCenter xCenter 1.75];
squareYpos = [yCenter
0.1 yCenter
0.1 yCenter*0.1];
numSqaures = length(squareXpos);

allColors = [1 0 0 ; 0 1 0 ; 0 0 1 ]’;

allRects = nan(4, 3);
for i = 1:numSqaures
allRects(:, i) = CenterRectOnPointd(baseRect,…
squareXpos(i), squareYpos(i));
end

Screen(‘FillRect’, window, allColors, allRects);

Screen(‘Flip’, window);

KbStrokeWait;

sca;

I don’t know if there is a demo for your purpose, but given your current code, it is quite easy to make it “move”. All you need to do is a loop for video frames, where you update the Y Coordinates of the squares. The number of pixels of increment/decrement will control the moving speed. Some pseudo code:

for i = 1:nFrames
    allRects([2 4],:) = allRects([2 4],:) + dY; % dY: pixels per frame
    Screen(‘FillRect’, window, allColors, allRects);
    Screen(‘Flip’, window);
end

That’s great, thank you for the response. I won’t get back to it for a day or two but will report back.