This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rotate about origin" Watch "Rotate about origin" New topic
Author

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

Gabe.

Jim Yingst
Wanderer
Sheriff

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

(0,0),(3,0),(3,1),(0,1)

its center is at

(1.5,0.5)

and rotating it 90 degrees clockwise would put it at

(1,2),(1,-1),(2,-1),(2,2)

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