0

I am trying to write a code that will show an accelerating wheel. As long as the user presses 'a' the wheel should accelerate counterclockwise. The thing is that it turns in the right direction but it doesn't accelerate. This is the code I am using (in PTB-3 and Windows XP):

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   

If anyone has an idea why it doesn't accelerate, I'd appreciate it very much.

skaffman
  • 398,947
  • 96
  • 818
  • 769
ariel
  • 2,637
  • 4
  • 20
  • 12

1 Answers1

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 

This code appears to influence th for exactly one "cycle". Instead, you should modify the rotation speed, and have the rotation speed influence the angle.

Try this:

if(keycode(larrow))  
    rot_spd-=1; % accelerate counterclockwise  
    th  
end
if(keycode(rarrow))  
    rot_spd+=1; % accelerate clockwise  
    th  
end
the+=rot_speed;
sarnold
  • 102,305
  • 22
  • 181
  • 238