basic fixed-point math trigonometric functions

This commit is contained in:
Sylvain PILLOT 2023-02-05 13:53:42 +01:00
parent b4203610a0
commit 907639ae1a
8 changed files with 162 additions and 23 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,14 @@
{ "columns":32,
"image":"tileset.png",
"imageheight":1024,
"imagewidth":512,
"margin":0,
"name":"Tileset_Space",
"spacing":0,
"tilecount":2048,
"tiledversion":"1.8.0",
"tileheight":16,
"tilewidth":16,
"type":"tileset",
"version":"1.8"
}

Binary file not shown.

View File

@ -4,7 +4,7 @@
static libnum::num cosTable[360];
static libnum::num sinTable[360];
static bool is_fast_trig_initialised = false;
void Fast_Trig_Init( void )
{
@ -13,11 +13,14 @@ void Fast_Trig_Init( void )
cosTable[u] = libnum::num( cos( u * PI / 180 ) );
sinTable[u] = libnum::num( sin( u * PI / 180 ) );
}
is_fast_trig_initialised = true;
}
libnum::num FastCos( int16_t angle )
libnum::num FastCosInt( int16_t angle )
{
if (!is_fast_trig_initialised) Fast_Trig_Init();
if (angle>=0 and angle<360) return cosTable[ angle ];
else
{
@ -25,18 +28,20 @@ libnum::num FastCos( int16_t angle )
if (input<0)
{
while (input<0) input+=360;
return cosTable[ angle ];
return cosTable[ input ];
}
else
{
while (input>=360) input-=360;
return cosTable[ angle ];
return cosTable[ input ];
}
}
}
libnum::num FastSin( int16_t angle )
libnum::num FastSinInt( int16_t angle )
{
if (!is_fast_trig_initialised) Fast_Trig_Init();
if (angle>=0 and angle<360) return sinTable[ angle ];
else
{
@ -44,18 +49,46 @@ libnum::num FastSin( int16_t angle )
if (input<0)
{
while (input<0) input+=360;
return sinTable[ angle ];
return sinTable[ input ];
}
else
{
while (input>=360) input-=360;
return sinTable[ angle ];
return sinTable[ input ];
}
}
}
libnum::num FastTan( int16_t angle )
libnum::num FastTanInt( int16_t angle )
{
//TODO : work on representation of infinite number for angle = 90 degrees or angle = 270 degrees
return libnum::num(0);
if (!is_fast_trig_initialised) Fast_Trig_Init();
int16_t input = angle;
if (input<0)
{
while (input<0) input+=360;
}
else if (input>=360)
{
while (input>=360) input-=360;
}
libnum::num value;
if (input==90)
{
value.v = INT32_MAX;
return value;
}
else if (input==270)
{
value.v = INT32_MIN;
return value;
}
else
{
value = FastSinInt(input) / FastCosInt(input);
return value;
}
}

View File

@ -11,9 +11,9 @@
void Fast_Trig_Init( void );
libnum::num FastCos( int16_t angle );
libnum::num FastSin( int16_t angle );
libnum::num FastTan( int16_t angle );
libnum::num FastCosInt( int16_t angle );
libnum::num FastSinInt( int16_t angle );
libnum::num FastTanInt( int16_t angle );
#endif

View File

@ -280,10 +280,10 @@ static void get_inputs( float dt )
#if(DEBUG_MODE)
if(MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open() ) {screenshot = true;};
if(MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) {record = true; };
if(MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) {record = false; };
if(MyKeyboard.IsKeyPressedEvent(MYKEY_DEL) && usb_is_open()) {textoutput = true;};
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open() ) {screenshot = true;};
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) {record = true; };
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) {record = false; };
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_DEL) && usb_is_open()) {textoutput = true;};
#endif
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_F1)) {texttodraw=0;}
@ -382,10 +382,6 @@ int main(void)
{
exitToOS = false;
Fast_Trig_Init();
_uram = kmalloc_get_arena("_uram");
bool canWeAllocate3Mb = AddMoreRAM();
@ -464,6 +460,14 @@ int main(void)
// to add here what must be sent to USB for Text mode debugging
char texttosend[1024];
for(int i=-720; i<=720; i++)
{
sprintf( texttosend, "i=%d - Sin(i)=%f - Cos(i)=%f - Tan(i)=%f\n", i, (float) FastSinInt(i), (float) FastCosInt(i), (float) FastTanInt(i) );
usb_fxlink_text(texttosend, 0);
}
textoutput = false;
}
#endif

View File

@ -80,8 +80,8 @@ void Player::Render( void )
{
int angle = (int) satAngle + u*incangle;
angle = angle % 360;
int xsat = (int) (x + FastCos( angle ) * libnum::num( satRadius) );
int ysat = (int) (y + FastSin( angle ) * libnum::num( satRadius) );
int xsat = (int) (x + FastCosInt( angle ) * libnum::num( satRadius) );
int ysat = (int) (y + FastSinInt( angle ) * libnum::num( satRadius) );
azrp_image_p8_effect(xsat-w, ysat-h, &img_Satellite_Lvl1, DIMAGE_NONE);
}
}