random natural motion - obtaining plot coordinates
5 ビュー (過去 30 日間)
古いコメントを表示
Hi there,
I'm trying to create short clips of randomly (but somewhat naturally) moving objects. However, the function I wrote to provide me some pseudorandom plot positions gets me pretty jagged positions. So either there is something wrong with my rationale for creating these positions on with my function. Either way I'd very much appreciate any comments.
My rationale is as follows: create random vectors using a small range of stepsizes and direction angles. The maximum direction angle is in between 0 and 90 degrees to create a somewhat natural flow, unless we reach the edge of the screen then the angle is between 90 and 180 degrees to flip the direction. Next I sue pol2cart to continuously update the plotting coordinates based on the previous angle and a random new angle.
This is the code:
function [p1X p1Y] = getRandomPositions2(sx, sy, npoints, maxrange, stimsize, moviesize)
%%create random vectors for displacement
rng('shuffle'); %%shuffle the random number generator to make sure you get a different sequence for each trial
radius = randi([1, maxrange],npoints,1); %%generate random radius
angle = deg2rad(randi([0,90],npoints,1)); %%generate random angle
angle(1) = deg2rad(randi([1,360],1,1)); %%first direction
fangle1 = deg2rad(randi([90,180],npoints,1)); %%some random flip angle for turning around in corner
[tx ty] = pol2cart(angle(1),radius(1));
p1X(1) = sx + tx; % get startpoint x
p1Y(1) = sy + ty; % get startpoint y
% loop trough frame positions
for iframe=1:(npoints-1)
% as long as we're not at the screen edges keep moving within angle(i)
if (p1X(iframe)<=(moviesize-stimsize))&&(p1X(iframe)>=stimsize)&&(p1Y(iframe)<=(moviesize-stimsize))&&(p1Y(iframe)>=stimsize)
angle(iframe+1) = angle(iframe)+angle(iframe+1); % get angle based on previous angle, convert to radians and update
% if we hit the screens edge moving once with fangle(i)
else
angle(iframe+1) = angle(iframe)+fangle1(iframe+1); % flip angle based on previous angle, convert to radians and update
end
[tx ty] = pol2cart(angle(iframe+1),radius(iframe+1)); % get x and y directions in pixels
p1X(iframe+1) = p1X(iframe)+tx;
p1Y(iframe+1) = p1Y(iframe)+ty;
end
end
Any comments, tips or suggestions would be of great help!!
0 件のコメント
回答 (1 件)
Image Analyst
2013 年 12 月 16 日
If you want the movement to be not so drastic and jagged, then restrict your next angle to your current angle plus or minus some amount, like 10 degrees so that the movement has to go into a 20 degree cone fanning out from the current point and direction. It won't be able to make abrupt 90 142 or 180 changes in direction then.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!