This site requires JavaScript, please enable it in your browser!
Greenfoot back
RUMMAKER
RUMMAKER wrote ...

2013/9/29

need help with move(double)

1
2
danpost danpost

2013/9/29

#
What didn't work like you wanted is line 3 (like you thought). It is because after converting to radians, there are only four possible 'int' values that 'angle' can be set to (zero, and the three between zero and PI). You will need to change 'angle' to a 'double' to make it work.
RUMMAKER RUMMAKER

2013/9/29

#
nvm apparently im terrible at calculous xD i forgot 1 radians != PI
davmac davmac

2013/9/30

#
what didnt work :
I think the problem was that you used an 'int' variable for angle. That only gives you 6 possible directions (there are 2 x PI radians in a full circle, i.e. just over 6). Try:
    public void move()    
        {    
            double angle = Math.toRadians(getRotation());    
            double vectorX = Math.cos(angle) * speed;    
            double vectorY = Math.sin(angle) * speed;   
        
            if (!isInsideWorld(currentX+vectorX, currentY+vectorY)) return;    
        
            currentX += vectorX;    
            currentY += vectorY;    
            setLocation((int)currentX, (int)currentY);    
        }    
I also got rid of some of the casts, which you don't need.
You need to login to post a reply.
1
2