🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

sin and cosine look ups

Started by
10 comments, last by bosco 23 years, 12 months ago
Hey, Ok, so here''s the question of the minute.. Lookup tables for sin, consine etc.. Piece of cake. But what''s the best way to go the other way.. say cos(theta) = 0.8 What''s the best way to get theta.. Thanks, bosco() p.s. at any point feel free to ignore my laziness.. :p
--leader of the free world .. or something ..
Advertisement
Umm, have you never heard of arccos and arcsin?
Lets see.....

if you are using a fixed point aritmetic (ie. 24 bits for the integer and 8 bits for the fraction)

use the following macros and typedef

                        typedef long fixed;			// Our new fixed point type.#define itofx(x) ((x) << 8)			// Integer to fixed point#define ftofx(x) ((x) * 256)			// Float to fixed point#define dtofx(x) ((x) * 256)			// Double to fixed point#define fxtoi(x) ((x) >> 8)			// Fixed point to integer#define fxtof(x) ((float) (x) / 256)		// Fixed point to float#define fxtod(x) ((double)(x) / 256)	        // Fixed point to double#define Mulfx(x,y) (((y) * (x)) >> 8)		// Multiply a fixed by a fixed#define Divfx(x,y) ((y << 8) / (x))    		// Divide a fixed by a fixed// Declare your look up tablesfixed MyASin[256];MakeTables(){ for (int n= 0 ; n< 256 ; n++) {  tblMyAsin[n] = asin(n/256.0); }}// to convert#define MyASin(a)  (tblMySin[(a & 0x000000ff)])// a - should by of type 'fixed'.                    



You will probably have to play around a bit, but that is the jist of it.



Edited by - DeltaVee on July 7, 2000 1:24:32 PM
D.V.Carpe Diem
Sorry Kyle Radue if this stuff is below you, just ignore me.. But I was referring to lookup table methods.. Can I refence x[0.8] and get the angle... hmm, don''t think so..

On the other hand, thanks DeltaVee for the post.. It''s defintely along the lines of what I''m looking for.. Not real sure with all of it though..

Why divide n by 256? I know it''s the size of the loop, but is that some arbitrary resolution you decided on?

Thanks again.. I''ll run it through some test and see what I can make of it..


bosco()

--leader of the free world .. or something ..
Why divide by 256?

The fractional part of the fixed type is 8 bits wide, which means the fractions are in 256ths.

There is a bug in the sample

#define MyASin(a) (tblMySin[(a & 0x000000ff)])// a - should by of type 'fixed'.

should read

#define MyASin(a) (tblMySin[fxtoi(a)])// a - should by of type 'fixed'.

--------------------------
Carpe Diem

Edited by - DeltaVee on July 7, 2000 3:13:15 PM
D.V.Carpe Diem
DeltaVee, you forgot to convert the floating point to a fixed point when you take the cos^-1 = arccos()

tblMyAsin[n] = (fixed) ftofx(asin((float) n/256.0));
Also, tblMySin should be MyASin...
Yeah sorry, like I said you may have to play with it a little. I just threw the code together, and got pulled away from my desk before I could go through and fix any mistakes.

I should put these posts together ''offline'' first, that way I wont look like an idiot

--------------------------
Carpe Diem
D.V.Carpe Diem
it''s all right.. we all make mistakes..
Look like an idiot.. nahh, I''m the idiot.. hehe.. You gave me a good starting place.. I just needed an idea ya know.. So thanks..

I''m about to give it a go here in a second.. I''ll let you know how it comes out.. thanks again.


bosco()

---
leader of the free world .. or something ..
--leader of the free world .. or something ..

This topic is closed to new replies.

Advertisement