lundi 17 juin 2019

Moving dots in different directions in a 3D space

I have this code that moves a number of dots all in the same direction in 2D space. I want to modify it in 2 steps:

  • I want each dot to move randomly in a different direction. This code makes them move all towards the same direction.

  • I want to create a 3rd dimension where the dots move randomly as well. I don't know how to compute dz (direction in z axis).

Any help would be appreciated. Thanks in advance!

dots.nDots = 100;                       % number of dots
dots.color = [255,255,255];             % color of the dots
dots.size = 10;                         % size of dots (pixels)
dots.center = [0,0];                    % center of the field of dots (x,y)
dots.apertureSize = [12,12];            % size of rectangular aperture [w,h] in degrees*


dots.x = (rand(1,dots.nDots))*dots.apertureSize(1) + dots.center(1);    
dots.y = (rand(1,dots.nDots))*dots.apertureSize(2) + dots.center(2);   

tmp = Screen('Resolution',0);                       
display.resolution = [tmp.width,tmp.height];

display.width = 30;                                
display.dist = 50;                                  

% This generates pixel positions, but they're centered at [0,0], which is the top left corner

pixpos.x = angle2pix(display,dots.x);               % Convert the x position of the dots from visual angle to pixel.
pixpos.y = angle2pix(display,dots.y);               % Convert the y position of the dots from visual angle to pixel.

%% We need to define some timing and motion parameters, which we'll append to the 'dots' structure:

dots.speed = 3;                 %degrees/second
dots.duration = 5;              %seconds
dots.direction_deg = 30;        %degrees (clockwise from straight up)
dots.direction_rad = dots.direction_deg * pi /180; % direction converted to radians


dx = dots.speed*cos(dots.direction_rad)/display.frameRate;
dy = -dots.speed*sin(dots.direction_rad)/display.frameRate;

nFrames = secs2frames(display,dots.duration);

%% Keeping the Dots in the Aperture
% We need to deal with dots moving beyond the edge of the aperture. This requrires a couple more lines of code.

%% First we'll calculate the left, right top and bottom of the aperture (in degrees)

l = dots.center(1)-dots.apertureSize(1)/2;
r = dots.center(1)+dots.apertureSize(1)/2;
b = dots.center(2)-dots.apertureSize(2)/2;
t = dots.center(2)+dots.apertureSize(2)/2;

%% New random starting positions
%%
dots.x = (rand(1,dots.nDots)-.5)*dots.apertureSize(1) + dots.center(1);
dots.y = (rand(1,dots.nDots)-.5)*dots.apertureSize(2) + dots.center(2);

%% Let's make the dots move in the aperture we have just specified
%%
try
    for i=1:nFrames
        %convert from degrees to screen pixels
        pixpos.x = angle2pix(display,dots.x)+ display.resolution(1)/2;
        pixpos.y = angle2pix(display,dots.y)+ display.resolution(2)/2;

        Screen('DrawDots',display.windowPtr,[pixpos.x;pixpos.y], dots.size, dots.color,[0,0],1);
        %update the dot position
        dots.x = dots.x + dx;
        dots.y = dots.y + dy;

        %move the dots that are outside the aperture back one aperture
        %width.
        dots.x(dots.x<l) = dots.x(dots.x<l) + dots.apertureSize(1);
        dots.x(dots.x>r) = dots.x(dots.x>r) - dots.apertureSize(1);
        dots.y(dots.y<b) = dots.y(dots.y<b) + dots.apertureSize(2);
        dots.y(dots.y>t) = dots.y(dots.y>t) - dots.apertureSize(2);

        Screen('Flip',display.windowPtr);
    end
catch ME
    Screen('CloseAll');
    rethrow(ME)
end
Screen('CloseAll');




Aucun commentaire:

Enregistrer un commentaire