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 > Problem using math functions

#1581 - DennisBor - Mon Jan 20, 2003 2:26 pm

I'm trying to rotate my sprites and the tutorial from the PERN project sais I have to use the math functions.

I'm using the code below for the functions:

Code:

#include "math.h"

// maak een macro voor FIXED getallen (zonder komma)
#define FIXED unsigned long

//maak een macro voor de waarde van PI
#define PI 3.14159

// maak een macro om de
#define RADIAAL(myWaarde) (((float)myWaarde)/(float)180*PI)

// [CUT] A LOT MORE CODE... [/CUT]

// loop to save all 360 cos and sin values
   for (myLoop = 0; myLoop < 360; myLoop ++)
   {
      mySinus[myLoop] = (FIXED)(sin(RADIAAL(myLoop)) * 256);
      myCosinus[myLoop] = (FIXED)(cos(RADIAAL(myLoop)) * 256);
   }


The problem is that the compiler complains:
undefined reference to 'sin'
undefined reference to 'cos'


does anyone know how to fix this compiler error?

Dennis
_________________
I rather have a compiler than a girl... A compiler isn't complaining all the time!

#1591 - joet - Mon Jan 20, 2003 4:32 pm

Make sure that you're linking the math library in the GCC path (-lm) ...

#1593 - DennisBor - Mon Jan 20, 2003 4:44 pm

Thanks :) that works!
_________________
I rather have a compiler than a girl... A compiler isn't complaining all the time!

#1603 - tepples - Mon Jan 20, 2003 6:15 pm

Using sin() and cos() to build lookup tables is SLOW and can cause several seconds of delay in starting the program. I'd suggest putting a 256-entry lookup table in 2.14 fixed-point format in your program and then using linear interpolation for in-between values. In all, it shouldn't take more than 1 KB; the sin() and cos() code from libm is probably bigger than that already.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#1604 - DennisBor - Mon Jan 20, 2003 6:19 pm

Ive just started programming C++ and GBA, can you explain such a table a little more for me?
_________________
I rather have a compiler than a girl... A compiler isn't complaining all the time!

#1806 - Mr. Sanity - Thu Jan 23, 2003 11:55 am

As an aside, why are you calculating both cos and sin? After all,
Code:

sin(degrees) = cos((degrees-90)%360)

So, if you only calculate the table for one set, you can use assignment for the other set, saving you almost 50% of the calculation time.

Even better yet, make a one-shot C/C++ program for your PC/Mac that outputs the following to stdout or a file:
Code:

const long mySinus[360] = {...values...};
const long myCosinus[360] = {...values...};

Then, copy and paste this output into your header file, and ditch that loop you have altogether. This way, you don't waste any processor time calculating this at runtime, which is a waste considering that sin/cos tables are never going to change.

#1807 - DennisBor - Thu Jan 23, 2003 12:03 pm

Yes I've done that and it works great! Thanks for all your help!
_________________
I rather have a compiler than a girl... A compiler isn't complaining all the time!