2016-04-27 53 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