Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

# Rotation

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

How would you rotate the contents of a 2d array. For example if I had a 2d array like: 1000 1000 1000 1000 and I wanted it to become 1111 0000 0000 0000 how would I go about doing this? Is there any maths involved and what branch of maths would this involve?

#### Share this post

##### Share on other sites
Advertisement
Is this for rotating a sprite or other 2d graphic?

#### Share this post

##### Share on other sites
In this case just integers i.e

int array[4][4]

I suppose it could be a sprite though. It would be similar solution no matter what it was would it not? You would just be assigning values to different memory locations I think.

I may have some ideas actually but I'm not sure and need some more opinions.

#### Share this post

##### Share on other sites
Would this work?

temp_array[4][4];for(int i = 0; i < 4; ++i)    for(int j = 0; j < 4; ++j)        temp_array[j] = array[j];

then if you want to you can copy them all back into the original array.

#### Share this post

##### Share on other sites
I don't think that would work Adamb because I want the situation to be that the 2d array is rotated 90 degrees each time. The code you have would only rotate it between these two states.

1000
1000
1000
1000

and

1111
0000
0000
0000

The next state should be

0001
0001
0001
0001

etc.

#### Share this post

##### Share on other sites
Hi, I don't know if this will help, but i'm currently doing the same with my tetris clone. (Storing those blocks in Array[4][4]). I use this for-loop:

bool TempArray[4][4] = {false};for (int x=0; x<4; ++x)    for (int y=0; y<4; ++y)        TempArray[3-y][x] = true;

I stole this loop from Rob Loach, look at his source (rBlocks) here: Click

Also, Aaron Cox tutorials explains a little of the math part of rotating, although he uses a slightly different approach: Look at part 2)

Now, I'm pretty sure that this works for me so far, but since this is the first time I do this myself I would recommend looking at the code / tutorial of more experienced people.

#### Share this post

##### Share on other sites
Sorry, that was me (AP). Forgot to login.

EDIT: bah, I did something wrong with the links too.
Rob Loach's rBlocks
Aaron's Tutorials

Also, the loop should have been:
for (int x=0; x<4; ++x){    for (int y=0; y<4; ++y)    {       if (TileArray[x][y])          TempArray[3-y][x] = true;    }}

Damn, I really f**k things up today.

[Edited by - DeadXorAlive on September 19, 2005 6:37:30 AM]

#### Share this post

##### Share on other sites
Thanks DeadXorAlive, that was exactly what I was looking for.

#### Share this post

##### Share on other sites
Looks like someone has been doing their research! [wink]

One problem I ran into and didn't end up fixing was on-wall rotations. If you play the original nintendo version of Tetris, you'll notice that if you try to rotate a block along the wall, it shifts the block over for you. If you play rBlocks and attempt to do the same thing, it will deny you from rotating it.

#### Share this post

##### Share on other sites
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(int argc, char *argv[]){  int array[4][4] = {                      0,0,0,0,                      0,1,1,0,                      1,1,0,0,                      0,0,0,0                    };    int i,j;    //print out the 2d array  for(i = 0; i < 4; i++)  {    for(j = 0; j < 4; j++)    {      printf("%d", array[j]);    }    printf("\n");  }                         int x, y, tempx, tempy;  //test the rotate equations  for(x = 0; x < 4; x++)  {    for(y = 0; y < 4; y++)    {      tempx = x*(cos(90)) - y*(sin(90));      tempy = x*sin((90)) + y*cos((90));      printf("x: %d y: %d\n" tempx, tempy);     }  }   system("PAUSE");	  return 0;}

I had a look at Aarons tutorials and found the equations which will handle the rotations. I wrote some code to try and get a better understanding of what those equations are supposed to do but I don't think I am able to grasp what the equations are going to give you. The numbers that you get for tempx and tempy are sometimes negative so how can this possibly be used to rotate elements in a 2d array?

I would be grateful if anyone could shed any light on this.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 10
• 11
• 13
• 9
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015433
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!