Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Gabriel White
Ranch Hand
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?

Gabe.

Jim Yingst
Wanderer
Sheriff
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.