2010-04-10 82 views
0

我想寫一個代碼,將顯示加速輪。只要用戶按下'a',車輪應該逆時針加速。事情是它轉向正確的方向,但不會加速。這是我使用的代碼(PTB-3和Windows XP):在MATLAB中加速車輪 - psychtoolbox

img=imread('c:\images.jpg'); 
[yimg,ximg,z]=size(img); 
rot_spd = 1; 
larrow = KbName('a'); % modify this for Windows 
rarrow = KbName('b'); 
[w,rect]=Screen('OpenWindow',0,[0 0 0]); 
sx = 400; % desired x-size of image (pixels) 
sy = yimg*sx/ximg; % desired y-size--keep proportional 
t = Screen('MakeTexture',w,img); 
bdown=0; 
th = 0; % initial rotation angle (degrees) 
HideCursor 
while(~any(bdown)) % exit loop if mouse button is pressed 
    [x,y,bdown]=GetMouse; 
    [keyisdown,secs,keycode] = KbCheck;  
    if(keycode(larrow)) 
     th = th - rot_spd-1; % accelerate counterclockwise 
     th 
    end 
    if(keycode(rarrow)) 
     th = th + rot_spd+1; % accelerate clockwise 
     th 
    end 
    destrect=[x-sx/2,y-sy/2,x+sx/2,y+sy/2]; 
    Screen('DrawTexture',w,t,[],destrect,th); 
    Screen('Flip',w); 
end 
Screen('Close',w) 
ShowCursor 

如果任何人有一個想法,爲什麼它沒有加速,我會非常感激。

回答

1
if(keycode(larrow)) 
    th = th - rot_spd-1; % accelerate counterclockwise 
    th 
end 
if(keycode(rarrow)) 
    th = th + rot_spd+1; % accelerate clockwise 
    th 
end 

此代碼似乎影響th只有一個「週期」。相反,您應該修改轉速,並使轉速影響角度。

試試這個:

if(keycode(larrow)) 
    rot_spd-=1; % accelerate counterclockwise 
    th 
end 
if(keycode(rarrow)) 
    rot_spd+=1; % accelerate clockwise 
    th 
end 
the+=rot_speed;