Search code examples
c++objective-cclassquaternions

Implement Quaternion calculation in Cocoa properly?


In my program I am dealing with tracking device movement with CMMotionManager and use quaternion representation of device attitude. For positioning the device in global coordinate system I need to do some basic calculations involving quaternion. For that I want to write some Quaternion class with all needed functions implemented. Does somebody know what is the proper way or maybe some general guidelines how it should be done. I've found a sample project from Apple which has Quaternion, Vector2 and Vector3 classes written in cpp, but I think that it's not very easy to use them in Cocoa, since I can't define properties of the object in Obj-C header file using these c++ classes. Or am I wrong? Thank you.


Solution

  • You're quite possibly not interested in OpenGL at all but as part of GLKit, Apple supplies GLKQuaternion. It has a C interface but should be easy to extrapolate into a class. Using it is recommended over expressing the mathematics directly since it likely uses the maths vector unit as fully as possible.

    Alternatively you can declare C++ and Objective-C uses of GLKQuaternion directly since both superset C.