Jump to content
  • Advertisement
Sign in to follow this  
DRPhil

Rotation around a focal point

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!