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
Aaaah!你是對的!謝謝! – Micard