在MATLAB用户交互动画 [英] User interactive animation in matlab

查看:133
本文介绍了在MATLAB用户交互动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使MATLAB用户交互动画。它是翻译和在屏幕上旋转,并且用户不得不点击它一个正方形。如果他们点击它,他们将获得点和动画将重复。如果他们点击空格(如除正方形内的任何地方)的动画将退出,你失去就会显示出来。我有动画使用两个功能几乎已经完成。一个创建注册鼠标点击动画和另一回事。到目前为止,我可以识别鼠标点击,如果用户点击空格动画将停止,但如果用户点击多边形内的动画不会重复。我不能确定如何修改我的code使动画将重复,直到用户点击空白。我已经粘贴下面我code。任何帮助将大大AP preciated。

I am trying to make a user interactive animation in matlab. It is a square that translates and rotates across the screen and the user has to click on it. If they click on it, they will receive points and the animation will repeat. If they click on the whitespace (eg. anywhere except within the square) The animation will exit and YOU LOSE will be displayed. I have the animation almost finished using two functions. One to create the animation and another that registers the mouse click. So far I can recognize the mouse click and the animation will stop if the user clicks on the whitespace but the animation will not repeat if the user clicks within the polygon. I am unsure how to modify my code so that the animation will repeat until the user clicks the white space. I have pasted my code below. Any help would be greatly appreciated.

动画功能:

function movingPolygon
global gUserHitPolygon;
global gCurrentXVertices;
global gCurrentYVertices;
gUserHitPolygon = true;
nSides =4;
%Polar points
r=1;
theta = pi/nSides * (1:2:2*nSides-1);

%Cartesisn points
x0 = r * cos(theta);
y0 = r * sin(theta);
nFrames = 100;
xx = linspace(0,10, nFrames);
yy = xx;

rr = linspace(0, 2*pi, nFrames);
h = figure;
set(h,'WindowButtonDownFcn',   @mouseDownCallback);

for i = 1:nFrames

    rX = [cos(rr(i)), -sin(rr(i))];
    rY = [sin(rr(i)), cos(rr(i))];

    x1 = rX * [x0; y0];
    y1 = rY * [x0; y0];

    x2= x1 + xx(i);
    y2= y1 + yy(i);
    gCurrentXVertices=x2;
    gCurrentYVertices=y2;
    y=fill(x2, y2, 'b');

    xlim([0,10]); ylim([0,10]);
    hold on;
    pause(0.000000003);
    if ~gUserHitPolygon
        clear GLOBAL gUserHitPolygon gCurrentXVertices gCurrentYVertices;
        break;
    end
    delete(y);

end
end

回调函数:

function mouseDownCallback(~,~)

global UserHitPolygon;
global CurrentXVertices;
global CurrentYVertices;


xVertices = gCurrentXVertices;
yVertices = gCurrentYVertices;

% if we have valid (so non-empty) sets of x- and y-vertices then...
if ~isempty(xVertices) && ~isempty(yVertices)

    % get the coordinate on the current axis
    coordinates = get(gca,'CurrentPoint');
    coordinates = coordinates(1,1:2);

    % if the coordinate is not in the polygon, then change the
    % flag
    if ~inpolygon(coordinates(1),coordinates(2),xVertices,yVertices)
       gUserHitPolygon = false;
    end
end
end

编辑:在回调函数修复了一些错误

fixed some bugs in the callback function.

推荐答案

包括您在while循环动画

Short answer

include your animation in a while loop

无论用户做什么,动画只能播放一次,因为它不知道重复了。这个问题的答案是使用whil​​e循环一个,这将重复你动画,直到你告诉它停下来。那么你的主循环变得

No matter what a user does, the animation will only play once, because it doesn't know to repeat. The answer to this is to use a while loop, which will repeat your animation until you tell it to stop. Your main loop then becomes

done = false; % your stopping condition
while ~done
    for i = 1:nFrames

        rX = [cos(rr(i)), -sin(rr(i))];
        rY = [sin(rr(i)), cos(rr(i))];

        x1 = rX * [x0; y0];
        y1 = rY * [x0; y0];

        x2= x1 + xx(i);
        y2= y1 + yy(i);
        gCurrentXVertices=x2;
        gCurrentYVertices=y2;
        y=fill(x2, y2, 'b');

        xlim([0,10]); ylim([0,10]);
        hold on;
        pause(0.000000003);
        if ~gUserHitPolygon
            clear GLOBAL gUserHitPolygon gCurrentXVertices gCurrentYVertices;
            done = true; % set your stopping condition
            break;
        end
        delete(y);

    end
end

这篇关于在MATLAB用户交互动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆