conversion between float[3] and vector3d

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

conversion between float[3] and vector3d

Post by JonLT »

This is no big thing but alot of times i need to convert between float[3] and vector3d, mostly when using newton. So i look in some of the files in the newton samples and found some stuff that i added to the vector3d class:

Code: Select all

//an additional constructor
vector3d(const T* ptr) : X(ptr[0]), Y(ptr[1]), Z(ptr[2]) {};
//additional operators
T& operator[] (int i) { return (&X)[i]; }
const T& operator[] (int i) const { return (&X)[i]; }
With this included in the vector3d class conversion is very easy:
float[3] to vector3d:

Code: Select all

float[3] f = {1,2,3};
vector3df v(f); //this makes a vector from the float
vector3d to float[3]:

Code: Select all

vector3df v(1,2,3);
float *f = &v[0]; //this makes a float[3] from the vector3d
I know this isn't magic, but i find it usefull so i thought i'd share it!
Post Reply