A fast vector library for purebasic.

Posted by on January 9, 2009

I recently required some vector functions to prototype a game idea in PureBasic, however there is no such thing as a built-in vector library and any of the existent community code was buggy to say the least (no offense but it’s old, ugly code that you’ll find anywhere else).

The reason for this library is quite simple, and so is the library itself. you can either include it or install the resource file and start using it right away. After taking a quick look at the source, you’ll soon familiarize with it’s naming convention; there is no need to write any documentation for this type of code, since it’s basic vector math. However a test source is included for your delight.

You can find the source in here: http://gushh.net/dev/?file=pb/vec3_macro.pbi
And some ugly test code in here: http://gushh.net/dev/?file=pb/vec3_macro_test.pb

Or, you can get “the whole package” from here: http://gushh.net/dls/vec3_macro.zip

Using this library is as easy as…

Define.VEC3 a, b, c
VEC3_SET( a, 0.1, 0.2, 0.3 )
VEC3_SET( b, 0.4, 0.5, 0.6 )
VEC3_ADD( a, b, c )
VEC3_DEBUG3( a, b, c ) ; display the vectors in the debug console.

 

I mention “so-called fast” because it’s relatively fast, however not as fast as a pure sse2+ implementation would be – although we remain “as fast as we can” without breaking code readability and maintainability at all. This is also 100% cross-platform and as fast as it gets with the current compiler. “Macrofobics” could disagree but it’s their loss.

Some might argue that using a “fast sqrt” would be a good thing, but I disagree. You don’t really need this at all now a days. Also, any implementation of the fast sqrt in PB would not benefit from much speed increase at all, if any you’ll ruin code readability over a little bit of speed while giving away precision and portability.

That said, feel free to modify as you see fit – But please share your findings with the community.

If there is a need for a VEC2 lib, I’ll gladly release it. Remember, you don’t need a point library / class at all, a vector library can indeed be used for points and it is recommended; cuts unecesarry redundant code and makes your life easier. Perhaps a purist OOP coder would disagree, but I don’t care.

Have fun with vectors!

2 Comments on A fast vector library for purebasic.

Closed

  1. […] this would be an ideal solution. For everything else, I suggest a specific library, such as my vec3 macro […]