2016-04-27 54 views
0

由于某些原因,当我尝试在MATLAB中反转当前速度的符号时,它不会执行此操作。例如,我从velocity_x = 3开始,velocity_y = 3(我画圆圈碰撞)。符号反转不起作用

现在的检查条件里面我需要扭转的迹象,我做了以下内容:

% This doesn't work: 
velocity_x = -velocity_x; 
velocity_y = -velocity_y; 

这些表现似乎并不管用。即使在变量列表中它仍然显示为-3,球只是抽动而不是朝相反的方向行进。但是当我简单地把数字放在那里时,它可以正常工作!

% This works perfectly fine: 
velocity_x = -3; 
velocity_y = -3; 

这里是整个循环:

velocity_x = 3; 
velocity_y = 3; 

% While is not commanded to exit the loop 
while exit_loop == false 

[b1_x_c, b1_y_c] = getCenter(b1); 

xMove(b1, velocity_x); 
yMove(b1, velocity_y); 

if ((b1_x_c + radius + 1) >= WINDOW_WIDTH) || ((b1_y_c + radius + 1) >= WINDOW_HEIGHT) 

    velocity_x = -1 * velocity_x; 
    velocity_y = -1 * velocity_y; 

elseif ((b1_x_c - radius - 1) <= 0) || ((b1_y_c - radius - 1) <= 0) 

    velocity_x = (-1) * velocity_x; 
    velocity_y = (-1) * velocity_y; 

end 

redraw; 


end % of the while loop 

回答

1

当你在ifelseif条件满足区域,符号可以改变每个周期转 - 速度值3 -3 3 -3等等...

你必须使用一些标志来表明该标志已经被改变,并且不会改变它,直到该区域将离开(一种迟滞)

+0

Aaaah!你是对的!谢谢! – Micard