johannes-fetz / joengine

Jo Engine is an open source 2D and 3D game engine for the Sega Saturn written in C under MIT license
http://jo-engine.org/
MIT License
205 stars 32 forks source link

Add new jo_fixed_cos func and correct tan division #67

Closed alfanhui closed 1 year ago

alfanhui commented 2 years ago

Changelog:

Description: Here is a few math calculations to prove new cos(45degree) function accuracy: image

You can compare sin/cos/tan values with this table at the 45 degrees mark: https://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=1011&context=mathclass

I noticed this cos issue as I am creating a right-angle turn movement along a grid. In this gif, you can see when I turn right (EAST), the grid starts trailing off the screen, and then the other gif is with the fix:

Original cos(): old_cos

Fixed cos(): new_cos

Interesting, sglCos is showing an inaccuracy as well.. i'll leave that for someone else to look into!

ponut64 commented 2 years ago

I think this is a good change, if only for that it is directly using fixed-point maths, and is an alternative method to SGL sine and cosine.

That said, it is using a lot of divisions. Sinus and co-sinus are not used often, so that is okay. All the same, having a fast approximate version would be a good idea.

Also, a VERY important note: REMOVE the use of the toFIXED macro. This multiplies by a floating point. That is VERY bad for performance. When converting an integer to a fixed-point, shift left by 16 (<<16). To convert a fixed to an integer, shift right by 16 (>>16).