Path: chuka.playstation.co.uk!news From: James Chow Newsgroups: scee.yaroze.beginners Subject: Re: sin and cos Date: Thu, 22 Jul 1999 06:59:28 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 66 Message-ID: <3796B340.82D5CDCC@chowfam.demon.co.uk> References: <37968F09.108DFABB@netscape.net> NNTP-Posting-Host: chowfam.demon.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.5 [en] (Win98; I) X-Accept-Language: en Look up tables are much faster (I've not done any proper timings - but I reckon in the region of 100 times faster...) Sin/cos functions in the libraries uses radians and not degrees. 360 degrees = 2 * PI radians 1 degree = (2 * PI / 360) radians Sorry, don't know about the >> 12 tho, haven't looked at the code. I know using >> bit shift operator is a trick to do quick division on integers by numbers to the powers of 2. so >> 12 does x / (2^12) ie x/4096. The rotate member variable in GsSPRITE uses 4096 units for 1 degree which is probably the reason for >> 12. #define PI (3.14159265) double SIN[360]; double COS[360]; //initialise look up tables before using for (i=0; i<360; i++) { SIN[i] = sin(2 * PI * i / 360); COS[i] = cos(2* PI * i / 360); } //to get sin/cos values int degree = ?; degree = degree % 360; //restrict to +/- 360 if (degree < 0) degree = degree + 360; //if negative, make positive int sindegree = SIN[degree]; int cosdegree = COS[degree]; Sorry if this isn't very clean, but I'm writing this on the fly. Cheers. James. Eddie Harrison wrote: > Does any 1 now how to use sin or cos in a game at the moment i am using > a bit of code i have borrowed which involves look up tables and i don't > understand it ( it from amatuer wars and uses a >>12 thing in it ) . i > have also looked at the section in the libary reference but like the > rest of the book it means nothing to me . if anyone know's which is > faster look up tables or the sin function please tell me . > here is my attempt so far at using lookup tables > > #include > #include "sincos.h" > > int main() > { > > int adjacent,oppisate,angle; > > angle = 32; > > adjacent = (SIN[angle]*10); > oppisate = (COS[angle]*10); > > printf("A triangle has a angle of %d\n",angle); > printf("and a hypotonuse of 10 so!!\n"); > printf("so it's adjacent side is %d long\n",adjacent); > printf("and its oppisate side is %d long\n\n",oppisate); > > return 0; > }