Is it possible to rotate an array?

For questions about using Classic.

Post » Sat Jul 30, 2011 4:32 am

In my level editor you can select any amount of tiles from a tileset by drawing over them. The data is saved to an array and loaded each time I place some tiles on the grid. I know how to set the angle of the individual tiles, but I want to be able to rotate groups of tiles - angle and position, just like rotating tetrominoes in tetris. To do this I'd need to rotate the array itself by 90 degree increments. Any idea how I'd do that?
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Sat Jul 30, 2011 4:57 am

Should be possible using two identical sized array objects and a nested for loop. Exact syntax would depend on the size of your arrays, and obviously these arrays could only rotate in 90 degree increments.

It would look something like this for a 90 degree rotation clockwise:

[code]
int CurrentRow = 0;
int CurrentColumn = HIGHY;

for ( int y = 0;y<=HIGHY;y++ )
{
    for ( int x = 0;x<=HIGHX;x++ )
    {
        CurrentRow++;
        DestinationArray[CurrentRow][CurrentColumn] = SourceArray[x][y];
    }
    CurrentRow = 0;
    CurrentColumn--;
}
[/code]

It's in C++, but it should be easily convertible to event structure.Arsonide2011-07-30 05:06:25
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Sat Jul 30, 2011 5:33 am

<- is retarded when it comes to code.

Any chance you can write that out in events or pseudo code for me? I'd really appreciate it.
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Sat Jul 30, 2011 6:30 am



That may not be exactly correct, as some things in Construct are base 0 and some things are base 1, but it's close. If it has any problems they will be along the outer borders of the array, along index 0 and along the maximums. Rotating other directions should be a similar process with some +'s and -'s changed around.Arsonide2011-07-30 06:32:38
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Sat Jul 30, 2011 10:58 am

I'm sorry, but this is not correct. Apart from the 1-based index issue, that code would mirror vertically on the horizontal axis. But Tokinsom wants to rotate in 90 degree steps.

Here is an example. Click on the sprites to color them then use the buttons to rotate clockwise or counterclockwise. Of course, you would need to integrate offsets if you want to rotate only a part of the array.

Feel free to ask, if it troubles you.


http://www.mediafire.com/file/mz0cv4sm5x7bt5j/rotatingarray.cap

EDIT: Ups, there was an redundant event, causing the array to be calculated two times. New link.tulamide2011-07-30 12:17:22
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Jul 30, 2011 2:19 pm

I realize you just need 90 degrees rotations, but for added effect, you can use any angle doing a matrix transformation using S.xoffrot(,,,) from "S". I'll try to do an example later.
Btw I think "S" has a built in function to rotate its arrays as well.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sat Jul 30, 2011 8:17 pm

[QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Sat Jul 30, 2011 8:29 pm

Agh it seems the 'S' Plugin is the answer to all my questions but I'm still hesitating to learn it ^^; Plus I'd basically have to start over on my level editor..and all the "snippets" in the S tutorials are obfuscated due to old quote boxes not working on the new forums!

Anyway, thanks for all the help guys. Tulamide, I'll try out your example in a bit!Tokinsom2011-07-30 20:31:20
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Sat Jul 30, 2011 9:12 pm

[QUOTE=Arsonide] [QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.[/QUOTE]

Again, I'm sorry, but this is not quite right. The function indeed is called rotate array, but it shifts a 1-dimensional array to the right or to the left (aka the equivalent to moving, not angular rotation), so

ABCD

rotated by 1 gets

DABC

or rotated by -1 gets

BCDA


Also, when using any free rotional functions, wether from 's' or just sin/cos, you can't make sure you will always keep all values on the grid (because of the rounding, that needs to be applied), leading to overwriting or doubling values.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Jul 30, 2011 10:39 pm

[QUOTE=tulamide] [QUOTE=Arsonide] [QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.[/QUOTE]

Again, I'm sorry, but this is not quite right. The function indeed is called rotate array, but it shifts a 1-dimensional array to the right or to the left (aka the equivalent to moving, not angular rotation), so

ABCD

rotated by 1 gets

DABC

or rotated by -1 gets

BCDA[/QUOTE]

Ahh well thats ok. Thats the only type of array I can do in S :P

[QUOTE=tulamide]Also, when using any free rotional functions, wether from 's' or just sin/cos, you can't make sure you will always keep all values on the grid (because of the rounding, that needs to be applied), leading to overwriting or doubling values.
[/QUOTE]
True, but once it gets to 90 degrees it should line back up, shouldn't it?
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests