Blogus Maximus

Rubbing people the wrong way since 1970...

  Home  |   Contact  |   Syndication    |   Login
  1366 Posts | 10 Stories | 2225 Comments | 1336 Trackbacks

News


Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Tag Cloud


Archives

Post Categories

Image Galleries

Blogs

Code Camps

CTown Geeks

Geeky Webcomics

High Geek

Magenic Blogs

Microsoft Blogs

My Articles

My Sites

PodCasts

UG

XNA

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

Feedback

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

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

# re: Rotating your arrays (aka When the hell would I ever use this?) 6/17/2008 11:17 AM George Clingerman
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.

# re: Rotating your arrays (aka When the hell would I ever use this?) 6/17/2008 11:23 AM Blogus Maximus
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.

# re: Rotating your arrays (aka When the hell would I ever use this?) 9/3/2008 5:57 AM Mark T
If you wanted to rotate an image in code, you could use the same method I guess.

# re: Rotating your arrays (aka When the hell would I ever use this?) 9/11/2008 8:55 AM Blogus Maximus
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

# re: Rotating your arrays (aka When the hell would I ever use this?) 9/17/2009 6:12 PM Guy
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

Post A Comment
Title:
Name:
Email:
Comment:
Verification: