Public Group

# x y z or float p[3]

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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;    }};

##### Share on other sites
Quote:
 Original post by Black KnightDoh 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]; };}; 
 0 
 Share this post Link to post Share on other sites 
 
 
 Black Knight    769 Black Knight Member 769 Posted January 31, 2006 Hey its me again :DAfter 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 likefloat 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 :=) 0 Share this post Link to post Share on other sites Zahlman    1682 Zahlman GDNet Emeritus 1682 Posted January 31, 2006 Quote:Original post by Black Knightbut 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 0 Share this post Link to post Share on other sites Promit    13374 Promit Senior Moderator 13374 Posted January 31, 2006 Quote:Original post by TheOddManThen 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. 0 Share this post Link to post Share on other sites 
 Prev 1 2 3 Next Page 3 of 3   Sign in to follow this   Followers 0 
 Go To Topic Listing Forum 
 Advertisement 
 Advertisement What is your GameDev Story? In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us. (You must login to your GameDev.net account.) Share My Story Popular Tags 2D 3D Advice C# C++ Character Concept Console Custom Design DX11 Feedback Gameplay General Graphics Learning Mobile Music OpenGL PC Publishing Unity Unreal VR Vulkan Popular Now 10 C++ Modeless dialogbox Win32 question By DividedByZeroStarted Thursday at 11:38 PM 25 3D float or double for my vertices / normals? By codingJoeStarted Thursday at 02:35 AM 13 C++ Creating a global thread By DividedByZeroStarted Thursday at 12:31 AM 10 Unreal Need Advice Important (advanced questions) By CloooouuuudStarted Wednesday at 07:30 PM 9 Multiplayer networking for modded open source game? By mysteriousmonkey29Started Monday at 07:44 PM Advertisement Forum Statistics Total Topics 634484 Total Posts 3017743 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Groups For Beginners Forum x y z or float p[3] 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry. Sign me up! $(document).ready(function() { setInterval(function() { window.googletag.pubads().refresh(); }, 30000); });$(document).ready(function() { if (ipsSettings.memberID > 0) { ga('send','event','User','View','Member'); } else { ga('send','event','User','View','Guest'); } });