We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to write code that will allow a preprogrammed sprite to move according to input from the WSAD keys. Currently, I am defining a sprite as a custom class with an x, y position, a width, and a height. I have the ability to program the code so each key moves the sprite in a corresponding direction.
W - Up, S - Down, A - Left, D - Right
What I want to do is edit this code slightly. I want to build it so the keys function like this:
W - Up, S - Down, A - Rotates the sprite left around the center point, D - Rotates the sprite right around the center point
I am looking at the rotate() and translate() functions now, just a little unclear on how to properly use them. I can make the sprite rotate around a point, but when I move the sprite off of that point (using either W or S) the sprite continues to rotate around the original position. How do I write the code to adjust the position with the new x, y coordinates?
Thanks for the help.
Answers
You need to move the origin of your coordinate system to be at the center of the sprite. Right now you are probably drawing your sprite at a position (x,y):
Convince yourself this does the same thing:
Next try this:
Notice the sprite does rotate - but it rotates around the top left corner. We need to move the origin a little more!
See the reference to learn about what these functions do.
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Nl$898UQxW3Q
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9NBw8x3YGExwu
Thank you TfGuy44 and GoToLoop. I'll look over these examples. You guys are awesome!!!