x y z or float p[3]

Started by
24 comments, last by Promit 18 years, 2 months ago
I guess its not a problem cuz the variable is valid inside the function and cannot be used in the main program cuz its not stored anywhere.

Advertisement
Well I have something like this now.
I will use it for colors too so I added the r,g,b,a too.

//Vector 4 Classclass Vector_4{public:	union	{		struct		{			float x,y,z,w;		};		struct		{			float r,g,b,a;		};		float v[4];	};	Vector_4(){x = 0.0f; y = 0.0f; z = 0.0f; w = 1.0f;};	Vector_4(float ix,float iy,float iz,float iw = 1.0f){x = ix; y = iy; z = iz; w = iw;};	Vector_4(tVertex v) {x = v.x; y = v.y; z = v.z; w = 1.0f;};		Vector_3 vec3() const	{		Vector_3 v(x,y,z);		return v;	}	float magnitude() const 	{		return (float)sqrt((x*x) + (y*y) + (z*z));	}	float &operator [](int index)	{		return v[index];	}  	Vector_4 operator=(const Vector_4& vec)	{		return Vector_4(x = vec.x, y = vec.y, z = vec.z,w = vec.w);	}	Vector_4 operator=(const Vector_3& vec)	{		return Vector_4(x = vec.x, y = vec.y, z = vec.z,w = 1.0f);	}	Vector_4 operator+(Vector_4 vector)const	{		return Vector_4(vector.x + x,vector.y + y,vector.z + z,vector.w + w);	}	Vector_4 operator-(Vector_4 vector)const	{			return Vector_4(x - vector.x, y - vector.y, z - vector.z);	}		Vector_4 operator+(float num)	{		return Vector_4(x + num,y + num,z + num);	}	Vector_4 operator*(float num)	{		return Vector_4(x * num,y * num,z * num);	}	Vector_4 operator/(float num) const	{		return Vector_4(x / num,y / num,z / num);	}	const Vector_4& operator+=(const Vector_4& vec)    {    		x += vec.x;        y += vec.y;        z += vec.z;		w += vec.w;        return *this;	}	const Vector_4& operator+=(const Vector_3& vec)    {    		x += vec.x;        y += vec.y;        z += vec.z;        return *this;	}	const Vector_4&	operator-=(const Vector_4& vec)	{		x -= vec.x;        y -= vec.y;        z -= vec.z;        return *this;	}	const Vector_4 &operator*=(const float &s)    {        x *= s;        y *= s;        z *= s;                  return *this;    }		const Vector_4 &operator*=(const Vector_4& vec)	{		x *= vec.x;		y *= vec.y;		z *= vec.z;		return *this;	}        const Vector_4 &operator/=(const float &s)    {         const float recip = 1/s;         x *= recip;         y *= recip;         z *= recip;         return *this;    }	const Vector_4 &operator+=(const float s)	{		x += s;		y += s;		z += s;		return *this;	}	const bool operator!=(const Vector_4& vec)	{		if( x != vec.x&&			y != vec.y&&			z != vec.z)			return true;		else return false;	}	const Vector_4 operator*(const float &s) const    {         return Vector_4(x*s, y*s, z*s);    }        friend const Vector_4 operator*(const float &s, const Vector_4 &vec)    {         return vec*s;    }};
Quote:Original post by Black Knight
Doh yea unions.
Well unions share the same memory as far as i know.So Im not wasting any memory for it its the same as 3 floats?
And whats the purpose of the struct inside the class cant i just have :
class Vec3{public:	union{		float x,y,z;		float p[3];	};};

This seems to work.


No. This means x, y, z and p[0] will all share the same memory, while only p[1] and p[2] will work as intended. This is why the struct is needed.


class Vector3
{
public:

union
{
struct { float x, y, z; };
float p[3];
};
};
Hey its me again :D
After changing the my base object class' position from Vector_3 to Vector_4 I had lots of errors cuz lots of functions were taking the position as a parameter with type Vector_3.
So I added this to my Vector_4 class.
class Vector_4{public:	union	{		struct		{			Vector_3 vec3;			float w;		};		struct		{			float x,y,z,w;		};				struct		{			float x,y,z,iBoneID;		};		struct		{			float r,g,b,a;		};				float v[4];	};};

And lets say I have a base object like this:
class STObject{protected:    Vector_4 m_vPosition;public:     const Vector_4 &getPosition(){return m_vPosition;};}


Now I call functions like this:

findIntersection(pTerrain,pObject->getPosition().vec3);

And lots of other functions are doing the same.What else can I do?
I dont want to overload each function like
float dot(Vector_3 v1,Vector_3 v2)
float dot(Vector_4 v1,Vector_4 v2)

I thought I may use templates but sometimes I need to take the dot of a Vector_3 and a Vector_4 O_O.
Ok enought for now :=)


Quote:Original post by Black Knight
but sometimes I need to take the dot of a Vector_3 and a Vector_4 O_O.


Um? A dot product is normally only meaningful between vectors of the same length. What kind of math are *you* doing o_O
Quote:Original post by TheOddMan
Then again it does break the law of encapsulation, so Object-Oriented purists would say the data members *have* to be private.
There is no such "law". The so-called purists are merely morons who like to speak authoritatively.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement