tangstad/helds
tangstad
/
helds
Archived
1
0
Fork 0

Added sin/cos lookup tables and used fixed points to get nice rotational steering on the car (gas/brake with A/B).

All the chasers rotate in the same direction as the car, but still stay in the same spot.
This commit is contained in:
Truls Alexander Tangstad 2005-11-24 21:44:49 +00:00
parent 3861c2b69c
commit bee6419eb0
3 changed files with 184 additions and 60 deletions

View File

@ -19,6 +19,19 @@
#include "car_pal_bin.h"
#include "car_bin.h"
#include "chaser_bin.h"
#include "sinlut.h"
typedef s32 FIXED; // 32bit FIXED in 24.8 format
#define FIX_SHIFT 8
#define INT2FIX(n) ((n)<<FIX_SHIFT)
#define FIX2INT(n) ((n)>>FIX_SHIFT)
#define SIN_SIZE 512
#define SIN_MASK (SIN_SIZE-1)
#define lut_sin(x) _sinLUT[x]
#define lut_cos(x) _sinLUT[(x + (SIN_SIZE>>2)) & SIN_MASK]
//---------------------------------------------------------------------------------
// storage space for palette data
@ -33,6 +46,7 @@ OBJAFFINE *const oa_buffer = (OBJAFFINE*)oe_buffer;
unsigned int frame;
//---------------------------------------------------------------------------------
void VblankInterrupt()
//---------------------------------------------------------------------------------
@ -82,79 +96,81 @@ int main(void)
s32 base = (s32)(0.707106781 * 256);
s32 one = 1 << 8;
car_aff->pa = 1 << 8;
car_aff->pb = 0;
car_aff->pc = 0;;
car_aff->pd = 1 << 8;
u8 i;
for (i=0; i<NUMCHASERS; i++) {
chasers[i].attr2 = OBJ_CHAR(520);
chasers[i].attr1 = OBJ_X(20+(i*10));
chasers[i].attr0 = OBJ_256_COLOR | OBJ_Y(40);
chasers[i].attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(40);
}
int x = 112;
int y = 72;
FIXED x = INT2FIX(112);
FIXED y = INT2FIX(72);
int speed = 0;
int angle = 0;
while (1) {
// This isn't very tidy, but a cleanup is always
// imminent...
if ((KeysHeld() & KEY_RIGHT) && (KeysHeld() & KEY_UP)) {
car_aff->pa = base;
car_aff->pb = base;
car_aff->pc = -base;
car_aff->pd = base;
} else if ((KeysHeld() & KEY_RIGHT) && (KeysHeld() & KEY_DOWN)) {
car_aff->pa = -base;
car_aff->pb = base;
car_aff->pc = -base;
car_aff->pd = -base;
} else if ((KeysHeld() & KEY_LEFT) && (KeysHeld() & KEY_DOWN)) {
car_aff->pa = -base;
car_aff->pb = -base;
car_aff->pc = base;
car_aff->pd = -base;
} else if ((KeysHeld() & KEY_LEFT) && (KeysHeld() & KEY_UP)) {
car_aff->pa = base;
car_aff->pb = -base;
car_aff->pc = base;
car_aff->pd = base;
} else if (KeysHeld() & KEY_UP) {
car_aff->pa = 1 << 8;
car_aff->pb = 0;
car_aff->pc = 0;;
car_aff->pd = 1 << 8;
} else if (KeysHeld() & KEY_RIGHT) {
car_aff->pa = 0;
car_aff->pb = one;
car_aff->pc = -one;
car_aff->pd = 0;
} else if (KeysHeld() & KEY_DOWN) {
car_aff->pa = -one;
car_aff->pb = 0;
car_aff->pc = 0;
car_aff->pd = -one;
if (KeysHeld() & KEY_RIGHT) {
angle -= 8;
} else if (KeysHeld() & KEY_LEFT) {
car_aff->pa = 0;
car_aff->pb = -one;
car_aff->pc = one;
car_aff->pd = 0;
angle += 8;
}
if (angle < 0) {
angle += 512;
} else if (angle >= 512) {
angle -= 512;
}
if (KeysHeld() & KEY_A) {
if ((speed<30) && (speed>=0)) {
// slow speedup
speed += 1;
} else if (speed<0) {
// break if we're going backward
speed += 2;
}
}
if (KeysHeld() & KEY_B) {
if ((speed>=-5) && (speed<=0)) {
// slow speedup
speed -= 1;
} else if (speed>0) {
// break if we're going forward
speed -= 2;
}
}
// slow down if we're not doing anything
if (!(KeysHeld() & KEY_A) && !(KeysHeld() & KEY_B)) {
if (speed>0) {
speed -= 1;
} else if (speed<0) {
speed += 1;
}
}
if (KeysHeld() & KEY_RIGHT)
x += 1;
else if (KeysHeld() & KEY_LEFT)
x -= 1;
int fspeed = speed >> 3;
x -= fspeed*lut_sin(angle);
y -= fspeed*lut_cos(angle);
if (KeysHeld() & KEY_UP)
y -= 1;
else if (KeysHeld() & KEY_DOWN)
y += 1;
if (x>=INT2FIX(232)) {
x -= INT2FIX(256);
} else if (x<INT2FIX(-24)) {
x += INT2FIX(256);
}
car->attr1 = OBJ_SIZE(1) | OBJ_X(x);
car->attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(y);
if (y>=INT2FIX(152)) {
y -= INT2FIX(176);
} else if (y<INT2FIX(-24)) {
y += INT2FIX(176);
}
car_aff->pa = lut_cos(angle);
car_aff->pb = -lut_sin(angle);
car_aff->pc = lut_sin(angle);
car_aff->pd = lut_cos(angle);
car->attr1 = OBJ_SIZE(1) | OBJ_X(FIX2INT(x));
car->attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(FIX2INT(y));
VBlankIntrWait();
CpuFastSet(oe_buffer, OAM, COPY32 | sizeof(oe_buffer)/4);

89
gba/cartest/src/sinlut.c Normal file
View File

@ -0,0 +1,89 @@
// =====================================================================
// Look-Up Tables
// SIN: sin(x*pi/256)
//
// Exported by Cearn's excellut v0.9
// (comments, kudos, flames to daytshen@hotmail.com)
//
// =====================================================================
#include "sinlut.h"
// -----------------------------------------------------------------------
// SIN: a 512 long LUT of 16bit values in 8.8 format
// sin(x*pi/256)
const signed short _sinLUT[512]=
{
0x0000,0x0003,0x0006,0x0009,0x000D,0x0010,0x0013,0x0016,
0x0019,0x001C,0x001F,0x0022,0x0026,0x0029,0x002C,0x002F,
0x0032,0x0035,0x0038,0x003B,0x003E,0x0041,0x0044,0x0047,
0x004A,0x004D,0x0050,0x0053,0x0056,0x0059,0x005C,0x005F,
0x0062,0x0065,0x0068,0x006B,0x006D,0x0070,0x0073,0x0076,
0x0079,0x007B,0x007E,0x0081,0x0084,0x0086,0x0089,0x008C,
0x008E,0x0091,0x0093,0x0096,0x0098,0x009B,0x009D,0x00A0,
0x00A2,0x00A5,0x00A7,0x00AA,0x00AC,0x00AE,0x00B1,0x00B3,
0x00B5,0x00B7,0x00B9,0x00BC,0x00BE,0x00C0,0x00C2,0x00C4,
0x00C6,0x00C8,0x00CA,0x00CC,0x00CE,0x00CF,0x00D1,0x00D3,
0x00D5,0x00D7,0x00D8,0x00DA,0x00DC,0x00DD,0x00DF,0x00E0,
0x00E2,0x00E3,0x00E5,0x00E6,0x00E7,0x00E9,0x00EA,0x00EB,
0x00ED,0x00EE,0x00EF,0x00F0,0x00F1,0x00F2,0x00F3,0x00F4,
0x00F5,0x00F6,0x00F7,0x00F8,0x00F8,0x00F9,0x00FA,0x00FA,
0x00FB,0x00FC,0x00FC,0x00FD,0x00FD,0x00FE,0x00FE,0x00FE,
0x00FF,0x00FF,0x00FF,0x0100,0x0100,0x0100,0x0100,0x0100,
0x0100,0x0100,0x0100,0x0100,0x0100,0x0100,0x00FF,0x00FF,
0x00FF,0x00FE,0x00FE,0x00FE,0x00FD,0x00FD,0x00FC,0x00FC,
0x00FB,0x00FA,0x00FA,0x00F9,0x00F8,0x00F8,0x00F7,0x00F6,
0x00F5,0x00F4,0x00F3,0x00F2,0x00F1,0x00F0,0x00EF,0x00EE,
0x00ED,0x00EB,0x00EA,0x00E9,0x00E7,0x00E6,0x00E5,0x00E3,
0x00E2,0x00E0,0x00DF,0x00DD,0x00DC,0x00DA,0x00D8,0x00D7,
0x00D5,0x00D3,0x00D1,0x00CF,0x00CE,0x00CC,0x00CA,0x00C8,
0x00C6,0x00C4,0x00C2,0x00C0,0x00BE,0x00BC,0x00B9,0x00B7,
0x00B5,0x00B3,0x00B1,0x00AE,0x00AC,0x00AA,0x00A7,0x00A5,
0x00A2,0x00A0,0x009D,0x009B,0x0098,0x0096,0x0093,0x0091,
0x008E,0x008C,0x0089,0x0086,0x0084,0x0081,0x007E,0x007B,
0x0079,0x0076,0x0073,0x0070,0x006D,0x006B,0x0068,0x0065,
0x0062,0x005F,0x005C,0x0059,0x0056,0x0053,0x0050,0x004D,
0x004A,0x0047,0x0044,0x0041,0x003E,0x003B,0x0038,0x0035,
0x0032,0x002F,0x002C,0x0029,0x0026,0x0022,0x001F,0x001C,
0x0019,0x0016,0x0013,0x0010,0x000D,0x0009,0x0006,0x0003,
0x0000,0xFFFD,0xFFFA,0xFFF7,0xFFF3,0xFFF0,0xFFED,0xFFEA,
0xFFE7,0xFFE4,0xFFE1,0xFFDE,0xFFDA,0xFFD7,0xFFD4,0xFFD1,
0xFFCE,0xFFCB,0xFFC8,0xFFC5,0xFFC2,0xFFBF,0xFFBC,0xFFB9,
0xFFB6,0xFFB3,0xFFB0,0xFFAD,0xFFAA,0xFFA7,0xFFA4,0xFFA1,
0xFF9E,0xFF9B,0xFF98,0xFF95,0xFF93,0xFF90,0xFF8D,0xFF8A,
0xFF87,0xFF85,0xFF82,0xFF7F,0xFF7C,0xFF7A,0xFF77,0xFF74,
0xFF72,0xFF6F,0xFF6D,0xFF6A,0xFF68,0xFF65,0xFF63,0xFF60,
0xFF5E,0xFF5B,0xFF59,0xFF56,0xFF54,0xFF52,0xFF4F,0xFF4D,
0xFF4B,0xFF49,0xFF47,0xFF44,0xFF42,0xFF40,0xFF3E,0xFF3C,
0xFF3A,0xFF38,0xFF36,0xFF34,0xFF32,0xFF31,0xFF2F,0xFF2D,
0xFF2B,0xFF29,0xFF28,0xFF26,0xFF24,0xFF23,0xFF21,0xFF20,
0xFF1E,0xFF1D,0xFF1B,0xFF1A,0xFF19,0xFF17,0xFF16,0xFF15,
0xFF13,0xFF12,0xFF11,0xFF10,0xFF0F,0xFF0E,0xFF0D,0xFF0C,
0xFF0B,0xFF0A,0xFF09,0xFF08,0xFF08,0xFF07,0xFF06,0xFF06,
0xFF05,0xFF04,0xFF04,0xFF03,0xFF03,0xFF02,0xFF02,0xFF02,
0xFF01,0xFF01,0xFF01,0xFF00,0xFF00,0xFF00,0xFF00,0xFF00,
0xFF00,0xFF00,0xFF00,0xFF00,0xFF00,0xFF00,0xFF01,0xFF01,
0xFF01,0xFF02,0xFF02,0xFF02,0xFF03,0xFF03,0xFF04,0xFF04,
0xFF05,0xFF06,0xFF06,0xFF07,0xFF08,0xFF08,0xFF09,0xFF0A,
0xFF0B,0xFF0C,0xFF0D,0xFF0E,0xFF0F,0xFF10,0xFF11,0xFF12,
0xFF13,0xFF15,0xFF16,0xFF17,0xFF19,0xFF1A,0xFF1B,0xFF1D,
0xFF1E,0xFF20,0xFF21,0xFF23,0xFF24,0xFF26,0xFF28,0xFF29,
0xFF2B,0xFF2D,0xFF2F,0xFF31,0xFF32,0xFF34,0xFF36,0xFF38,
0xFF3A,0xFF3C,0xFF3E,0xFF40,0xFF42,0xFF44,0xFF47,0xFF49,
0xFF4B,0xFF4D,0xFF4F,0xFF52,0xFF54,0xFF56,0xFF59,0xFF5B,
0xFF5E,0xFF60,0xFF63,0xFF65,0xFF68,0xFF6A,0xFF6D,0xFF6F,
0xFF72,0xFF74,0xFF77,0xFF7A,0xFF7C,0xFF7F,0xFF82,0xFF85,
0xFF87,0xFF8A,0xFF8D,0xFF90,0xFF93,0xFF95,0xFF98,0xFF9B,
0xFF9E,0xFFA1,0xFFA4,0xFFA7,0xFFAA,0xFFAD,0xFFB0,0xFFB3,
0xFFB6,0xFFB9,0xFFBC,0xFFBF,0xFFC2,0xFFC5,0xFFC8,0xFFCB,
0xFFCE,0xFFD1,0xFFD4,0xFFD7,0xFFDA,0xFFDE,0xFFE1,0xFFE4,
0xFFE7,0xFFEA,0xFFED,0xFFF0,0xFFF3,0xFFF7,0xFFFA,0xFFFD,
};

19
gba/cartest/src/sinlut.h Normal file
View File

@ -0,0 +1,19 @@
// =====================================================================
//
// Exported by Cearn's excellut v0.9
// (comments, kudos, flames to daytshen@hotmail.com)
//
// =====================================================================
#ifndef SINLUT_H
#define SINLUT_H
// === LUT SIZES ===
#define SIN_SIZE 512
// === LUT DECLARATIONS ===
extern const signed short _sinLUT[];
#endif // SINLUT_H