Sin Tables (and rotations)

Hullo again all,

Camera rotations are something that've been bothering me for a long time, and I've read suggestions about looking up in sin tables in the past, but only recently really started thinking about it. (I haven't really done any research into these, I just read the words "eventually you'll just look it up in a sin table", and this is how I interpretted it).

For those that are just reading this for fun, and don't understand the problem, it's that calling sin and other trig functions in C/C++ gets pretty slow if you do it all the time (very bad when trying to render lots of objects on the fly).

So, for my solution...

Since the problem lies in calling sin on the fly, being slow, I figure creating the table once at the beginning of my program is the way to go.

I'm thinking of making a class, oh, I dunno, cSinTable.

In it will be an array with 62832 float elements (0 to 62831).

Code:

`#define TABLE_ELEMENTS 62832 //Note: 6.28318 is roughly two Pi`

float sine[TABLE_ELEMENTS];

Generation will look something like this:

Code:

`for(int X = 0; X < TABLE_ELEMENTS; X++)`

{

sine[X] = sinf((float)X / 1000.0f);

}

Then, with the added complexity of having to multiply your float numbers by 1000, this table will have instant access time (hooray for array implementation) to all numbers from 0 to 2 Pi (keeping in mind 2 Pi is the same as 0, so that 62833th element isn't necessary) in 0.0001 increments.

I.e. if you want to find the sin of 0.5

**EDIT: I mentioned this would be a class...**

Code:

**Function would look like this...**

float cSinTable::MySin(float X)

{

return sine[(int)(X * 1000)];

}

//**Use this to call the function...**

SinTable.MySin(0.5)

Issues that could arise:

**-** Too much typecasting (values changed/too much data loss?)

**-** Writing code to avoid overflows (if the user wants the sin of 3 pi, there must be a way to reduce it until it is within less than 2 Pi and greater than 0). A while loop that keeps subtracting 2 Pi until this condition is met could get the job done though. Since the point of this is rotations though, the angle should never really be anywhere close to that high.

Anyways, that's my idea, I was wondering what you all thought about it (is that the usual way to do it?). Any other problems that might arise that you can spot? Anything obvious I missed?

Just to note, that code was very quickly put together for this post, but hopefully I didn't make any ridiculous errors there.

Thanks for any input