Rotation around a focal point

Started by
2 comments, last by DRPhil 18 years, 8 months ago
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?
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...
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

--www.physicaluncertainty.com
--linkedin
--irc.freenode.net#gdnet

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.

This topic is closed to new replies.

Advertisement