   Moving in a 2D world (TopDown)
  I'm currently developing a top-down RPG game (written in Java) and I have run into a bit of a snag. I know that I want the controls to be WADS (W-S = Forwards+Backwards, A-D = Turning Left and Right). The character will be stationary and when your character "turns" it really just rotates all of the other objects on the screen to the left. I will also have to change the Rotation Direction of all the other objects to compensate for me turning.How I have the player class is it has the Game X and Y coordinates and has a facing direction variable, Which will be an int ranging from 0-350 in increments of 10 (0, 10, 20... etc.) So when the player "turns" left. it subtracts 10 degrees. This will greatly affect my move function though. If your facing Up in the game world, and you move forward you will move down 5 pixels on the Y (or whatever I plan on making it) But, if you are facing Right in the world (Which would be 90 in the facing direction variable) and you moved forward you would have to move Up 5 Pixels on the X axis. The only way I can think of turning/moving this way is to define how much you need to move on each axis for each degree of rotation. I thinking of how to programatically do that and all I can think of is one huge Switch. My question is, am I doing this the wrong way? If I am, what would be better? Thanks!~Gordo~   
Quote:The only way I can think of turning/moving this way is to define how much you need to move on each axis for each degree of rotation. I thinking of how to programatically do that and all I can think of is one huge Switch.Hint: study the sin and cos functions and see what those can do for you.Then you will also discover that your approach of working with degrees is not very efficient. Start working with radians (or indices, when using sine/cosine lookup tables which of course should contain 2^N entries).;Quote:Original post by Jan-LieuweQuote:The only way I can think of turning/moving this way is to define how much you need to move on each axis for each degree of rotation. I thinking of how to programatically do that and all I can think of is one huge Switch.Hint: study the sin and cos functions and see what those can do for you.Then you will also discover that your approach of working with degrees is not very efficient. Start working with radians (or indices, when using sine/cosine lookup tables which of course should contain 2^N entries).Gah! Sin and cos... I feel so stupid for not thinking of those... And to think I swore to my math teacher that I would NEVER use that crap again...;  sin() and cos() need their parameters in radians, so if you use degrees, you must convert them.to convert from degrees to radians: radians = degrees * Π / 180so 60 degrees = Π/3. In sin, you don't pass Π, you would call it like this:sin(1/3); or sin(60/180);   ;Quote:Original post by sheep19sin() and cos() need their parameters in radians, so if you use degrees, you must convert them.to convert from degrees to radians: radians = degrees * Π / 180so 60 degrees = Π/3. In sin, you don't pass Π, you would call it like this:sin(1/3); or sin(60/180);Umm... Sorry, it's been a few years since I took Trig in Highschool and I didn't pay attention because I honestly never thought I would see this again (Big mistake)Why would it be sin(1/3)?   ;Quote:Why would it be sin(1/3)?I'm not sure what sheep19 is trying to get at there, but 60 degrees is about 1.05 radians, not 1/3.   ;  60 degrees = (1/3)pi ~= 1.047. sheep19 just forgot the pi part.   