Custom trig functions needed.
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!!
Quote:Original post by jyk
What 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!
Well, I know nothing about PSP programing, but can't you include some standar libs like math.h or smething like that?
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.
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.
I will do some research into this, and hopefully reply soon with a more helpful answer.
:)
-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.
I will do some research into this, and hopefully reply soon with a more helpful answer.
:)
-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.
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)).
If you have rotations, you could also rotate (1,0) by a certain angle alpha, and the resulting vector is (cos(alpha),sin(alpha)).
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.
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.
You don't need trig to do that:
Of course, which is faster depends on whether cos and sin beat sqrt and sqr. But the chances are they don't.
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.
Quote:Original post by Bob Janova
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement