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.

Coding > sprite rotation problem

#17247 - hakanyuksel - Fri Mar 05, 2004 12:22 am

I am trying to rotate a 64x64 sprite in mode 0. When I enable sprite rotation the sprite become a solid one color 64x64(I tried 16x16 it is still happening) box. I dont know why does it happen. I have read all the documents on the web,
I am using HAM and my code is here:

#include <math.h>
#include "memory.h"
#include "graphics.h"
#include "data.h"
#include "game.h"
#include "keyboard.h"

volatile const int __gba_multiboot = 1;

int main(void)
{
unsigned short *sprite_attribute_memory = ( unsigned short* ) 0x7000000
*( unsigned short* ) 0x4000000 = (1<<12)|(1<<6);
copy_data_to_vram(); // I copy the sprite data and palet to vram here
wait_vblank();
sprite_attribute_memory[ 0 ] = SPR_COLOR_256 |(1<<8);
sprite_attribute_memory[ 1 ] = SPR_SIZE_64;
sprite_attribute_memory[ 2 ] = 0;
sprite_attribute_memory[ 6 ] = cos_table[100]>>8;
sprite_attribute_memory[ 7 ] = sin_table[100]>>8;
sprite_attribute_memory[ 14] = -sin_table[100]>>8;
sprite_attribute_memory[ 15] = cos_table[100]>>8;
while(1) {}
return 0;
}
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#17249 - yaustar - Fri Mar 05, 2004 12:48 am

What does the array bit here mean?
Code:
 sprite_attribute_memory[ 0 ] = SPR_COLOR_256 |(1<<8);
sprite_attribute_memory[ 1 ] = SPR_SIZE_64;
sprite_attribute_memory[ 2 ] = 0;
sprite_attribute_memory[ 6 ] = cos_table[100]>>8;
sprite_attribute_memory[ 7 ] = sin_table[100]>>8;
sprite_attribute_memory[ 14] = -sin_table[100]>>8;
sprite_attribute_memory[ 15] = cos_table[100]>>8;

_________________
[Blog] [Portfolio]

#17250 - tepples - Fri Mar 05, 2004 1:19 am

If a rot/scale sprite becomes one solid color the same as that of the pixel in the center, that means that you either aren't copying the rot/scale matrix into OAM or your sprite isn't referencing it. I think you meant to put the values in indices 3, 7, 11, and 15 rather than 6, 7, 14, and 15.

In addition, what fixed-point precision are your cos_table[] and sin_table[] stored at? The computations you gave (with the change in the destination indices) are correct for a 16.16 table.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17251 - hakanyuksel - Fri Mar 05, 2004 1:27 am

it is the memory of the attributes. I didnt make a structure.
So

unsigned short *sprite_attribute_memory =( unsigned short* ) 0x7000000;

1st sprite atributes
atribute 0 =sprite_attribute_memory[0];
atribute 1 =sprite_attribute_memory[1];
atribute 2 =sprite_attribute_memory[2];

2nd sprite attributes
atribute 0 =sprite_attribute_memory[3];
atribute 1 =sprite_attribute_memory[4];
atribute 2 =sprite_attribute_memory[5];

1st rotation and scale infomation
pa1 = sprite_attribute_memory[6];
pb1 = sprite_attribute_memory[7];

3rd sprite atributes
atribute 0 =sprite_attribute_memory[8];
atribute 1 =sprite_attribute_memory[9];
atribute 2 =sprite_attribute_memory[10];

4th sprite attributes
atribute 0 =sprite_attribute_memory[11];
atribute 1 =sprite_attribute_memory[12];
atribute 2 =sprite_attribute_memory[13];

1st rotation and scale infomation
pc1 = sprite_attribute_memory[14];
pd1 = sprite_attribute_memory[15];

.....
and so on

As I read from the documents
1. Attributes are 16 bit
2. After every 6 atributes there is a rotation 2 rotation/scale information
3. You tell in attribute1 what rotation/scale information will be used
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#17252 - hakanyuksel - Fri Mar 05, 2004 1:50 am

tepples wrote:
If a rot/scale sprite becomes one solid color the same as that of the pixel in the center, that means that you either aren't copying the rot/scale matrix into OAM or your sprite isn't referencing it. I think you meant to put the values in indices 3, 7, 11, and 15 rather than 6, 7, 14, and 15.

I am a little confused about this rotation and scale things.
I am not using an external array of sprites I work on directly OAM memory;is it bad?

tepples wrote:

In addition, what fixed-point precision are your cos_table[] and sin_table[] stored at? The computations you gave (with the change in the destination indices) are correct for a 16.16 table.



heres my code for the tables

#define PI 3.14159265
#define RADIAN(angle) ((float)angle / (float)180 * PI)

signed int cos_table[360];
signed int sin_table[360];

void calculate_tables_1()
{
unsigned int i;
for(i=0;1<360;i++)
{
cos_table[i]=(signed int)sin(RADIAN(i))*256;
sin_table[i]=(signed int)sin(RADIAN(i))*256;
}
}
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com

#17256 - tepples - Fri Mar 05, 2004 3:03 am

hakanyuksel wrote:
I work on directly OAM memory;is it bad?

As long as you fit your changes into vblank time, it's not bad at all.

Quote:
heres my code for the tables

Your trig table is in fact .8 as opposed to .16.

Five things to point out:
  1. A table of 512 values will be faster in practice than a table of 360 values because platforms without a hardware divide instruction (such as ARM7TDMI) handle the % operation (which reflects angles into the domain of your table) much faster when the divisor is a power of 2.
  2. Pre-calculate your table on a PC instead of doing floating-point trig computations on the GBA.
  3. With a .8 fixed-point format, you could make the table of type 'signed short' to save RAM.
  4. You could make 'cos' an inline function that calls 'sin' to save more RAM.
  5. Most importantly: Lose the ">>8" in your code.

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#17306 - hakanyuksel - Sat Mar 06, 2004 1:32 am

Your information helped very much. It is now working. I learned all the technical information about sprites now. Theere were 2 errors in my code
1. my trigonometry tables were wrong (When I copy and paste another trigonometry code from another file everything worked)
2. I misunderstood somethings about rotation/scale indices now I know what to do.

Thank you very much tepples
_________________
--------------------
Hakan Yuksel
3TE GAmes
www.3tegames.com