# Rotation around a focal point

This topic is 4901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Before I get into my problem id like to say hello to everyone, as this is my first post. I am writing a simple platformer game, but there is a catch - you can rotate the level around your character. The center of your character is always at (300,300) as the screen size is (600,600). The level scrolls around depending if you is walking left, right, jumping, or falling. I am trying to make the entire level rotate 90 degrees when the player chooses to do so. Currently, this is what I have:
Quote:
 int varA = (int)Math.cos(Math.toRadians(ROTATION)) * (getX() - 300); int varB = (int)Math.sin(Math.toRadians(ROTATION)) * (getY() - 300); int newX = varA - varB; int varC = (int)Math.sin(Math.toRadians(ROTATION)) * (getX() - 300); int varD = (int)Math.cos(Math.toRadians(ROTATION)) * (getY() - 300); int newY = varC + varD; if(getWidth() == origWidth)assignWidth(origHeight); else assignWidth(origWidth); if(getHeight() == origHeight)assignHeight(origWidth); else assignHeight(origHeight); assignX(newX); assignY(newY);
Where ROTATION is -90.00. I tried searching these forums and found pretty much the same solution - so what am I doing wrong?

##### Share on other sites
Quote:
 Before I get into my problem id like to say hello to everyone, as this is my first post.
Welcome :-)

Just looking at the algorithm, I'd say you're forgetting to add the 300's back in after rotation.

I guess you're using ints to clamp the rotation to multiples of 90? If so, there are probably better ways to do that. But, maybe you're using ints for some other reason that's not apparent from the code sample...

##### Share on other sites
The output from Math.cos and Math.sin is being cast before it's multiplied by (GetX() - 300) and (GetY() - 300). Enclose the whole computation in brackets, e.g. (int)(Math.cos(Math.toRadians(ROTATION)) * (getX() - 300));

Also, perhaps you should create a variable like rotationRadians and make the call to Math.toRadians(ROTATION) just once since it doesn't change between calls. And, as jyk pointed out, it doesn't look like you're adding the center to the results.

-Josh

##### Share on other sites
Thanks for the help, you guys were correct, I just forgot to add 300.

I was constantly changing the code not seeing the obvious, and thats the only reason why I hadnt changed Math.toRadians() to make it more efficient.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×