gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Problems with sprite rotation

#136349 - Phantaseur - Tue Jul 31, 2007 8:31 pm

[Images not permitted - Click here to view it]
I have problems with sprite rotation. Example is on picture. I'm making simple snake game, there is example of rotation of snake's head on picture, I have correct image just when there is no rotation, in other cases I have some elements of sprite missed.
When rotating 90 degrees there is last column of pixels in sprite missed and sprite moved one pixel right; in case of 180 & 270 degrees there are same problems.
For initialization of rotation I used next code:
Code:
   for(int i=0;i<4;i++){   
      snrot[i].hdx = COS[i*128 & 0x1FF] >> 4;
      snrot[i].hdy = SIN[i*128 & 0x1FF] >> 4;
      snrot[i].vdx = -snrot[i].hdy;
      snrot[i].vdy = snrot[i].hdx;
0 - no rotation, 1 - 90 degees, 2 - 180 degrees, 3 - 270 degrees.
For displaying sprite:
Code:
   snspr[0].attribute[0] = ATTR0_COLOR_256 | ATTR0_ROTSCALE | (hy*8&0x0ff);
   snspr[0].attribute[1] = ATTR1_ROTDATA(rd) | ATTR1_SIZE_8 | (hx*8&0x01ff);
   snspr[0].attribute[2] = 0;

   for(int j=0;j<8*4;j++)
      SPRITE_GFX[j+0]=(snakehead[2*j+1]<<8)|snakehead[2*j];
rd depends of orientation of head (can be 0,1,2 or 3), hy & hx are coordinates of snakehead in cell units, not in pixels.

What I have to do to get correctly rotated images?

#136359 - masscat - Tue Jul 31, 2007 9:20 pm

The centre of rotation for a 8x8 sprite is the 5th pixel along and down rather than the centre of the sprite (the interval between the 4th and 5th pixel).
This leads to you loosing the left and bottom edges of your sprite when rotating.
To avoid this you can restrict you sprites to 7x7 centred about the 5th pixel.

See TONC for more affine info.

#136362 - tepples - Tue Jul 31, 2007 9:59 pm

Sprites on the GBA and DS rotate about the center pixel, which is (4, 4) in an 8x8 pixel sprite.

You can use the same workaround that developers used on NES and Super NES (which had flipping but not rotation): Hand-draw the cels that point north and east, and use only 180 degree sprite flipping (no rotation matrix). Or rotate the sprite cels in software, storing the east cels and creating the north cels from that.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#136440 - Phantaseur - Wed Aug 01, 2007 9:04 pm

But why they did it such way?
Didn't they think it's unapplicable and useless?

#136444 - masscat - Wed Aug 01, 2007 9:28 pm

Because they did - there may be some hardware design reason for it. The 2D engine of the NDS is a functionally nearly the same as the GBA (so the design reason may be historic).

You can rotate and scale sprite like objects (textured polygons) using the 3D hardware on the NDS.

#136479 - knight0fdragon - Thu Aug 02, 2007 5:44 am

couldnt he also double the size of the canvas, allowing that extra row to show
_________________
http://www.myspace.com/knight0fdragonds

MK DS FC: Dragon 330772 075464
AC WW FC: Anthony SamsClub 1933-3433-9458
MPFH: Dragon 0215 4231 1206

#136480 - LiraNuna - Thu Aug 02, 2007 5:53 am

knight0fdragon wrote:
couldnt he also double the size of the canvas, allowing that extra row to show


The graphics would still be off by 1px, though.
_________________
Private property.
Violators will be shot, survivors will be shot again.