jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Rotate about origin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rotate about origin" Watch "Rotate about origin" New topic

Rotate about origin

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi all, just wondered if yall could help me with this rotate method for my tetris game. This method works fine, but I want the points of the body of the pieces to stay positive, for example, if I rotate the "J" Piece once (90 degrees) it will rotate out of the standard 0,0 origin and actually have one of the blocks (points) sticking out of this quadrant.

Is there an easy way to implement the rotation around a central origin for EVERY piece, or a way to reset the origin after the piece has been moved?

Thanks in advance


Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I don't think that rotating around an object's center will necessarily prevent its points from straying into negative territory. For example, if we have a rectangle with vertices


its center is at


and rotating it 90 degrees clockwise would put it at


which has crossed the x-axis. So I think your best bet is to just rotate the shape like you're doing, but then shift it afterwards. To do this, you'll need to figure out the minimum x coordinate and minimum y coordinate after rotation. Then loop through and subtract minX from each x coord, and subtract minY from each y coord.

To do this, you will need at least one more loop besides the one you've written. But if you're clever, you can do this with only one more loop, by combining some steps into the loop you've already written. However if you're having trouble, don't hesitate to write as many loops as you need; they're pretty fast and easy. You can always figure out how to combine them later.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: Rotate about origin
It's not a secret anymore!