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

2014/8/18

Help?!

GreenGoo GreenGoo

2014/8/18

#
Can anyone figure out what this code does? I'm think someone gave me the code many moons ago but I've only just re-discovered it and cannot for the life of me figure out how it works.
x = x - (Math.cos(Math.toRadians(rotation))*speed) + Endeavour.xVelocity;  
        y = y - (Math.sin(Math.toRadians(rotation))*speed) + Endeavour.yVelocity;  
danpost danpost

2014/8/18

#
It looks like it may have been applied to a class that represent an object that can be picked up and moved (or, at least, somehow influenced) by another object and the code is negating its own movement and following the movements of 'Endeavour'. I could be totally wrong, as that is just a guess.
GreenGoo GreenGoo

2014/8/18

#
Yes. that's totally right, but I was more concerned with the maths. I don't understand the maths.
danpost danpost

2014/8/18

#
The math codes in parenthesis just get the horizontal (line 1) and vertical (line 2) distances when moving 'speed' in the given 'rotation'. Since 'rotation' is saved in degrees, 'Math.toRadians' is used to convert degrees into radians, which are the units that 'Math.cos' and 'Math.sin' use. A more telling conversion would be 'rotation*Math.PI/180', which is basically what 'toRadians' does.
davmac davmac

2014/8/18

#
GreenGoo, check Neil's blog post for a really comprehensive tutorial on sine and cosine.
GreenGoo GreenGoo

2014/8/18

#
The blog concerns exactly what I was trying to do on other question... Thanks, it will be a real help.
You need to login to post a reply.