Is there a quick ATAN out there?

Previous topic - Next topic

r0ber7

Hello.

To determine the angle of a trajectory so I can rotate a sprite accordingly, I'm using ATAN.

Goes a little something like this:

Code (glbasic) Select

angle = ATAN(start_y - dest_y, start_x - dest_x)


I've noticed that it takes up some time to complete the calculation. I'm using tSIN and tCOS from amos' z-project code for quick calculations, and I was wondering whether someone has code for a quick ATAN lying around. ;)

If not, I might write one myself, but I don't like reinventing the wheel, plus I'd rather keep busy creating game content than to delve into math to figure this out.

Anyone?  =D


backslider

Maybe you could try thsi atan2-function, but I don't know if it's faster:

http://www.raspberryginger.com/jbailey/minix/html/atan2_8c-source.html

kanonet

r0ber7 have a look at my libQMATH, it has approximations for many math commands, all less accurate than GLB ones, but faster. Maybe that helps you.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

r0ber7

Nice,right now I'm at work but I'll have a look tomorrow. Thank you. :-)

kanonet

I just added qATAN2 for you, its the same like standard GLBs ATAN. So now i offer ATAN with one and 2 parameters.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

Thanks Kanonet... I think this codes are absolutely very usefulls... in my game I use your code a lot...

Only a question... How do it´s this libraries... you make by yourself the maths?¿ :booze:

kanonet

Thank you mentalthink, im very happy if my code is useful for someone. :happy:

About your question, its a bit different for each command:
The qSIN math is based on the qSIN that was delivered by Gernot in /samples/common/qmath.gbas. Together with Qedo i just started optimizing Gernots Code without changing the formula in its core (dont know where he got it, tried creating my own ones based on Taylor series, but they could not compete with Gernots).
The qSQR/qInvSQR was taken from /samples/common/qmath.gbas too. It is based on some old code from Quake3, see Wikipedia. So i did nothing with this Functions... till today! Today i posted an update with more accuracy and maybe more speed, which is based on the work of Rrrola. I just 'ported' this to GLB.
So most times its just look how others did it and than do some heavy math and GLB optimisation, not doing everything right from the beginning alone.
The only work that was completely done by myself are qASIN (qACOS) and qATAN. As you can see they are half between and approximation and a hardcoded lookup table. I wrote a program that did the work for me, it calculated the steps (the CASE sentences) and calculated the linear approximations between these steps. Biggest part of this functions is machine generated code. And finally atan2 is a clear and easy definition, see Wikipedia, that i just needed to port to GLB.

Of cause its not too easy everything, but im a big fan of heavly optimising everything... maybe thats why i never finished a single game. :S

New Update of my Lib is online.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

@kanonet, sorry for the late reply, I don´t rebember this post...

Thanks a lot for your explanations... I´m sure I can do this for my maths skills, but it´s very interesting know this little things explained in the forum... here learn more than if you go by yourself...

I take a look in my free time to this links, I think it´s very interesting...

Thanks a lot kanonet, and excuseme for the huge late reply.

Iván J

r0ber7

Works perfectly. Not using it in all situations, since I want some graphics to be rotated with more precision, but it does save time at a few points. Thank you. :)