Geeks With Blogs

News

Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer


Archives
Blogus Maximus Rubbing people the wrong way since 1970...

I have a nephew who is wicked smart. Actually, I have 4 nephews and 4 nieces in total, but this is about one nephew in particular.

Anyway... he's fairly young, but he's always been interested in software development. By software development, I mean games of course.  He doesn't just play them though, he's taught himself Java. Then I showed him C#. (Yes, I suppose I could have showed him VB... but he already knew Java, so C# was the logical choice.) Along with C#, was XNA. He really grok'ed XNA.

One of his first XNA projects was the standard breakout/Arkanoid type of game, with power-ups, etc... and particle effects. For a first (or even 3rd or 4th) game, it was pretty darn cool.

Before long, he was taking XNA in new directions. Asking questions I couldn't answer. Eventually, I hear from him and he's built something new. Not a game so much as a tool. Apparently he's big into Second Life. He needed a way to work with his Second Life models outside of the game, so he wrote a viewer in XNA and WinForms. Pretty sweet.

This weekend, I got an email from him. He's working on a Tetris clone and had everything done except the shape rotation. He's storing all the shapes in a 4x4 array and couldn't figure out how to rotate the array left and right.

It's funny how sometimes the hard stuff comes easy to us, while the simple stuff proves vexing. I run into this a lot at work. I think a large part of it comes from not having a CS background. Being largely self taught can be a disadvantage sometimes. Many folks will tell you that most of what you learn in a CS degree program is obsolete, and while that may apply to specific technology, there's a lot of theory and otherwise "common" knowledge that us self-taught folks don't always have when entering the work force.  (Stay in School Kids!!)

So, I gave his question some thought and came up with (I hope) a pretty good answer. Assuming this is what he's trying to achieve, how would we go about making it happen?

image

 

Part of the problem to solve depends on which direction you are trying to rotate.

In the illustration, you can see the shape array is rotating one step to the right. This means that the first row of the Y axis becomes the farthest column of the X axis. Therefore, the next row is the second farthest column, and so on.

 

It should be a simple matter of looping through the source array and writing into the destination array.

So, if you're rotating to the right, you have a nested loop (X inside of Y) reading the source array row by row and you want to write to the destination array column by column, where the target column is the inverse of the source row.

On the other hand, if you are rotating to the left, it's actually even easier. The top row becomes the first column, the second row becomes the second column and so on.


The code should look something like this:

' For LEFT turns
For Y = 0 to 3
    For X = 0 to 3
        Destination(Y,3-X) = Source(X,Y)
    Next
Next
 
' For RIGHT turns
For Y = 0 to 3
    For X = 0 to 3
        Destination(3-Y,X) = Source(X,Y)
    Next
Next

 

This code makes some assumptions, like knowing you have 4x4 arrays and knowing which way you're turning, and of course actually drawing the resulting shape on the screen, in the proper color. I'll leave all that as an exercise to you, the reader.

The real question is: now that I've figured out how, when the hell would I ever use this other than in a Tetris game?

Posted on Monday, June 16, 2008 10:23 PM | Back to top


Comments on this post: Rotating your arrays (aka When the hell would I ever use this?)

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
Not sure if it is a big concern in a Tetris-style game, but you will get better performance using a jagged array.
Left by Jeff Brand on Jun 17, 2008 8:25 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
so you mean, storing each shape configuration in the array and pulling out the right one based on rotation state?
Left by Blogus Maximus on Jun 17, 2008 8:39 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
Yeah, that's how I did mine was with all the rotations pre-defined in an array for each shape. Then I just access that rotation from that array and I know the position the blocks should be in.

Pretty cool that he's catching on so fast. It will be fun to say you knew him when he was just starting out.
Left by George Clingerman on Jun 17, 2008 11:17 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
yeah, that would work too. I got an email back from him this morning, that he had it working. But I'll suggest pre-storing all the rotations and he can go with either one.
Left by Blogus Maximus on Jun 17, 2008 11:23 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
If you wanted to rotate an image in code, you could use the same method I guess.
Left by Mark T on Sep 03, 2008 5:57 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
Apparently I made a typo in the original post... A couple of folks (included one somewhat abrasive individual that I deleted) have contacted me to point out that left rotations should be like this:

For Y = 0 to 3
For X = 0 to 3
Destination(Y,3-X) = Source(X,Y)
Next
Next
Left by Blogus Maximus on Sep 11, 2008 8:55 AM

# re: Rotating your arrays (aka When the hell would I ever use this?)
Requesting Gravatar...
Hi,

I have a Cam profile plotted as an oddly shaped circle in excel. I need to transpose the data 1 degree at a time to animate it rotating.

- This is a good use of this method

- Any idea how to transpose, just X & Y columns about a centre (0,0) point in Excel VBA?

Cheers

Guy
Left by Guy on Sep 17, 2009 6:12 PM

Your comment:
 (will show your gravatar)


Copyright © Chris G. Williams | Powered by: GeeksWithBlogs.net | Join free