Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-member conversion functions; Casting different types, e.g. DirectX vector to OpenGL vector

Tags:

c++

casting

I am currently working on a game "engine" that needs to move values between a 3D engine, a physics engine and a scripting language. Since I need to apply vectors from the physics engine to 3D objects very often and want to be able to control both the 3D, as well as the physics objects through the scripting system, I need a mechanism to convert a vector of one type (e.g. vector3d<float>) to a vector of the other type (e.g. btVector3). Unfortunately I can make no assumptions on how the classes/structs are laid out, so a simple reinterpret_cast probably won't do.

So the question is: Is there some sort of 'static'/non-member casting method to achieve basically this:

vector3d<float> operator vector3d<float>(btVector3 vector) {
    // convert and return
}

btVector3 operator btVector3(vector3d<float> vector) {
    // convert and return
}

Right now this won't compile since casting operators need to be member methods. (error C2801: 'operator foo' must be a non-static member)

like image 739
sunside Avatar asked Jun 08 '10 16:06

sunside


1 Answers

I would suggest writing them as a pair of free functions (i.e. don't worry about making them 'operators'):

vector3d<float> vector3dFromBt(const btVector3& src) {
    // convert and return
}

btVector3 btVectorFrom3d(const vector3d<float>& src) {
    // convert and return
}

void f(void)
{
  vector3d<float> one;   
// ...populate...   
  btVector3 two(btVectorFrom3d(one));    
// ...
  vector3d<float> three(vector3dFromBt(two));
}
like image 169
Adam Holmberg Avatar answered Oct 11 '22 22:10

Adam Holmberg