This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotate about origin

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic