# Custom trig functions needed.

This topic is 4406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi. im working on a 2D game, and im using a very good library, thich supports rotation, distortion, etc. BUT, it has one major flaw: it has no trig functions. :( (which is kinda weird) in my game, i will need the following functions: sin cosin atan2 so, my question is, is there ANY way to handwrite these functions and have them return a value?? cause im stumped.. if there is, could someone please share a way to do it? thanks!!

##### Share on other sites
What language and library are you using?

##### Share on other sites
Quote:
 Original post by jykWhat language and library are you using?

im using C++, programming for the PSP. im using a library called OSLIB (old-school library) google it for more info. its actually a very nice library. can hanlde a lot, (i actually had 400 circles bouncing around the screen, rotation, changing size, all at around a 50 -60 FPS. (psp only allows 60 max, dunno why not any more.))

thanks!

##### Share on other sites
Well, I know nothing about PSP programing, but can't you include some standar libs like math.h or smething like that?

##### Share on other sites
If its on a PSP then just make a precomputed trig table. When you call sin(.3595), or whatever number, have it look up the closest return value to that number. You can make the trig table for like every 5 degress and come off fine.

##### Share on other sites
I don't know anything about PSP programming either, but my best guess is the PSP does not have the C Standard Library, so using math.h is not an option.

:)

-edit: beat by dpadam450. yes, i believe a precomputed table works well, and if I'm not mistaken, that would work much faster than even the cslib trig functions.

##### Share on other sites
Or, if you want full precission, you can use a CORDIC algorithm.

If you have rotations, you could also rotate (1,0) by a certain angle alpha, and the resulting vector is (cos(alpha),sin(alpha)).

##### Share on other sites
ok, thanks for all the realies! the reason why ic ant use math.h (yes, PSP does have one) is because its part of the standard library, and i cant use it without using the PSP SDK's standard files (basic I/O, and all the operating files) when i use those, i cant use OSLIB, (they both have theyre own systems, which interfere with another, resuling in 50 pages of error.) about the trig tables, i dont really understand what youre saying, can anyone explain more?

ALSO, if anyone want more deatil, here is what im trying to get it:

ok, i have a bullet, and i want to shoot it based on a cursor. (cross hair) im using this technique:

find the angle between the player and cursor:

angle = atan2(cursor_y - player_y, cursor_x - player_x)

then make a trajectory based on that angle:

traX = cos(angle) * speed
traY = sin(angle) * speed

and this work fine and dandy. but in my game, i need a player which rotates, and ive tried numerous rotation althogrims, which kill my FPS. OSLib is the only lib which does it using hardware and GU accleration. so, now i have the problem of using the trig functions.

##### Share on other sites
You don't need trig to do that:
scalefactor = speed / sqrt(sqr(cursor_x - player_x) + sqr(cursor_y - player_y));traX = (cursor_x - player_x) * scalefactor;traX = (cursor_y - player_y) * scalefactor;

Of course, which is faster depends on whether cos and sin beat sqrt and sqr. But the chances are they don't.

##### Share on other sites
Quote:
 Original post by Bob JanovaYou don't need trig to do that:scalefactor = speed / sqrt(sqr(cursor_x - player_x) + sqr(cursor_y - player_y));traX = (cursor_x - player_x) * scalefactor;traX = (cursor_y - player_y) * scalefactor;Of course, which is faster depends on whether cos and sin beat sqrt and sqr. But the chances are they don't.

hmm, thats a good concept, never really thought about that. linear interpolation right? square root is a very expensive operation, but square is nothing. cos and sin are equal. ill try that. run a debug test.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!