Is it possible to rotate an array?

This forum is currently in read-only mode.
From the Asset Store
Simple resize and rotate events for any sprite, quick and easy event sheet that you can use for your own projects.
  • 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?

  • 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:

    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--;
    }
    

    It's in C++, but it should be easily convertible to event structure.

  • <- 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.

  • <img src="http://content.screencast.com/users/Arsonide/folders/Jing/media/35ab3930-e4ed-4fb7-9a72-9bb3e320cda8/2011-07-30_0030.png" border="0" />

    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.

  • 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.

  • 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.

  • Btw I think "S" has a built in function to rotate its arrays as well.

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

  • 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!

  • > Btw I think "S" has a built in function to rotate its arrays as well.

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

    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.

  • >

    > > Btw I think "S" has a built in function to rotate its arrays as well.

    >

    > I thought something did, but I couldn't remember what. I think it was S.

    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

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

    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.

    True, but once it gets to 90 degrees it should line back up, shouldn't it?

  • True, but once it gets to 90 degrees it should line back up, shouldn't it?

    Yes, matrix rotation in 90 degree steps will work perfectly.

    (as well as 180, 270, 360, etc.)

  • Well the idea was for extra effect showing it rotate, but I get the feeling this is for an editor, so the extra steps would be pointless.

  • You mean something like that?

    http://www.mediafire.com/file/1lmfgk4sfqpuhj7/rotatingsprites.cap

    (Was too lazy to do ccw and omitted the array, which is a simple task to add now)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yep, pretty much, the center around instance version.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)