Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411517 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 09:02:54 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)rotating non-square 2d- maps
Pages: [1]
Print
Author Topic: rotating non-square 2d- maps  (Read 1611 times)
nikki
Level 10
*****


View Profile
« on: December 13, 2009, 04:49:38 AM »


Hello,

I am working on a level-editor and i ran into a problem:
I'm trying to rotate a map, an easy way of doing this is switching the variables in the draw loops, for example this is the normal way

for x= 0 to map_width
 for y= o to map_height
  drawtile(x,y)
 next
next

then this is the same map 90 degrees turned

for x= 0 to map_width
 for y= o to map_height
  drawtile(y,x-map_width)
 next
next

this all works, but offcourse this won't work on non square maps.


Today I've planned to fix this, maybe someone here has got some tricks for me ?






Logged
zamp
Level 1
*



View Profile
« Reply #1 on: December 13, 2009, 05:12:56 AM »

It would be better to change the values in the map data than changing the draw method.
If you change the map data then you don't have to reprogram the draw method of the game.
Logged
nikki
Level 10
*****


View Profile
« Reply #2 on: December 13, 2009, 05:23:25 AM »

that was more a pseudoway of writing it down, assume the rotations are being done on data, and the draw method is nice and the same all over,

the actual problem is still untouched however.

I'm trying it out atm, I think i'll need an extra map with opposite sizes of arrays.
example

map[32,16]
_90map[16,32]

wait !
I go drink coffee and program more
Logged
kiwi
Level 0
***


View Profile WWW
« Reply #3 on: December 13, 2009, 05:28:05 AM »

Easiest way I can think of is by using Givens rotation matrices
Code:
[x] * [  cos a  sin a ] = [x'] 
[y]   [ -sin a  cos a ]   [y']

Where a is the angle you wish to rotate your map by (counterclockwise) and x' and y' are the coordinates of the rotated object

It's either that or using polar coordinates, which in turn means that instead of using x and y to describe a position, you use a distance measured from the origin and the angle.
You then compute x and y like this

x=R*cos a
y=R*sin a

Or you could just make all your maps square
Logged

nikki
Level 10
*****


View Profile
« Reply #4 on: December 13, 2009, 06:14:05 AM »

I have it working now by dividing up the rectangle in squares....
it looks like it willl become messy though so i tried to look up this Givens rotation

Thos wiki-math computer science pages sure look like a magic-spell book to me..
could you enlighten on this line of your example kiwi

  • * [  cos a  sin a ] = [x'] 

please a simple example

what does this mean [  cos a  sin a ]
multiply cos(a) with sin(a) ?, i cant just put two number after each other?

Logged
nikki
Level 10
*****


View Profile
« Reply #5 on: December 13, 2009, 06:30:24 AM »

ok i have found some more matrix wiki pages...
could someone explane this to me:

assume you have a simple 2d array thats not square
i'll run through the array and fill a 90 degrees array
lets say:

Code:
map_width=32
map_height=16

original[map_width,map_height]

this map is filled with values
because the map is non-square i'll create another container
Code:
rotated[map_height,map_width]

lets say i want to fill that rotated array with data from the original using matrices
how do i do this?

i assume something like this:
Code:
for local x0 = 0 to map_width
  for local y0 = 0 to map_height
   
    x1= some calculation i don't get yet
    y1=  ''
   
    rotated[x1,y1]=original[x0,y0]
   next
next

am i on the right track ?
Logged
zamp
Level 1
*



View Profile
« Reply #6 on: December 13, 2009, 06:36:21 AM »

this might work

Code:
original[width][height]
new[height][width]

// 90 degree turn to right
for y=0 to width
  for x=0 to height
    new[x][y] = original[y][x]

// 90 degree turn to right (no invert)
for y=0 to width
  for x=height to 0
    new[x][y] = original[y][height-x]
« Last Edit: December 23, 2009, 12:22:22 AM by zamp » Logged
kiwi
Level 0
***


View Profile WWW
« Reply #7 on: December 13, 2009, 06:49:46 AM »

please a simple example

what does this mean [  cos a  sin a ]
multiply cos(a) with sin(a) ?, i cant just put two number after each other?



Hmmm, I guess the way I wrote that is a bit incorrect (even from a mathematical point of view now that I look at it) and seeing as I can't really make a matrix in this text editor, I'm gonna rephrase that in code (C++).

The givens rotator is a matrix, in our case it's 2x2 because we're working with 2-d space
Therefore we have

Code:
float angle = PI;
float givens[2][2];
float coordinates[2];
float transcoords[2];

coordinates[0]=0;
coordinates[1]=1;

givens[0][0]=cos(angle);
givens[0][1]=sin(angle);
givens[1][0]=-sin(angle);//or you could use sin(-angle) here because sin(-x)=-sin(x)
givens[1][1]=cos(angle);

transcoords[0]=givens[0][0]*coordinates[0]+givens[0][1]*coordinates[1];
transcoords[1]=givens[1][0]*coordinates[0]+givens[1][1]*coordinates[1];

So in this example x=0 and y=1 and we want to rotate them by PI radians which would mean that x=0 and y=-1

So we have x'=cos(PI)*0 + sin(PI)*1 = 0 and y'=-sin(PI)*0 + cos(PI)*1 = -1 because sin(PI)=0 and cos(PI)=-1

Let's take another easy example and rotate x and y just by PI/2 radians
This time we have x'=cos(PI/2)*0 + sin(PI/2)*1 = 1 and y'=-sin(PI/2)*0 + cos(PI/2)*1 = 0

Also,it looks like the rotation is clockwise not counterclockwise, my bad.
Logged

nikki
Level 10
*****


View Profile
« Reply #8 on: December 13, 2009, 07:01:50 AM »

that was good! thanks kiwi

I already found out that since the rotations will be 0,90,180 or 270 there is actually no need for cos,sin or pi because its always 0,-1 or 1 as outcome.

the code posted by zamp almost works !
the newmap is filled with data , the rotating is not 100% though, its rotated and flipped either horizontally of vertical.
one calculation away i guess !

Logged
nikki
Level 10
*****


View Profile
« Reply #9 on: December 13, 2009, 07:04:34 AM »

edit above

both zamp's loops have the same outcome..
Logged
nikki
Level 10
*****


View Profile
« Reply #10 on: December 13, 2009, 07:15:29 AM »



got it !!!

Code:

original[width][height]
new[height][width]

// 90
for x=0 to width
  for y=0 to height
    new[y-width][x] = original[x][y]


Logged
zamp
Level 1
*



View Profile
« Reply #11 on: December 23, 2009, 12:24:04 AM »

edit above

both zamp's loops have the same outcome..

Hmm.. programming without testing aint working out Smiley
Edited the post
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic