Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

executor_2k2

Should I make my vector into a class?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can either use structs or classes for objects such as vectors or planes. But I noticed that classes are slower. At least they are in my code. Should I still go with the classes? Is there any way to optimize? The current project im working on where i notice the speed difference is a particle system project. It has many particles and lots of objects are being created obviously so class overhead would come into play a lot. What is your guy's experience? Classes or sturcts? What is most commonly used? What do major games like q3 use? Thx Heres my current vector class: #ifndef __VECTOR_H #define __VECTOR_H #include <math.h> /* VECTOR.H scalar_t CVector class OpenGL Game Programming by Kevin Hawkins and Dave Astle Some operators of the CVector class based on operators of the CVector class by Bas Kuenen. Copyright (c) 2000 Bas Kuenen. All Rights Reserved. homepage: baskuenen.cfxweb.net */ typedef float scalar_t; class CVector { public: scalar_t x; scalar_t y; scalar_t z; // x,y,z coordinates public: CVector(scalar_t a = 0, scalar_t b = 0, scalar_t c = 0) : x(a), y(b), z(c) {} CVector(const CVector &vec) : x(vec.x), y(vec.y), z(vec.z) {} // vector assignment const CVector &operator=(const CVector &vec) { x = vec.x; y = vec.y; z = vec.z; return *this; } // vecector equality const bool operator==(const CVector &vec) const { return ((x == vec.x) && (y == vec.y) && (z == vec.z)); } // vecector inequality const bool operator!=(const CVector &vec) const { return !(*this == vec); } // vector add const CVector operator+(const CVector &vec) const { return CVector(x + vec.x, y + vec.y, z + vec.z); } // vector add (opposite of negation) const CVector operator+() const { return CVector(*this); } // vector increment const CVector& operator+=(const CVector& vec) { x += vec.x; y += vec.y; z += vec.z; return *this; } // vector subtraction const CVector operator-(const CVector& vec) const { return CVector(x - vec.x, y - vec.y, z - vec.z); } // vector negation const CVector operator-() const { return CVector(-x, -y, -z); } // vector decrement const CVector &operator-=(const CVector& vec) { x -= vec.x; y -= vec.y; z -= vec.z; return *this; } // scalar self-multiply const CVector &operator*=(const scalar_t &s) { x *= s; y *= s; z *= s; return *this; } // scalar self-divecide const CVector &operator/=(const scalar_t &s) { const float recip = 1/s; // for speed, one divecision x *= recip; y *= recip; z *= recip; return *this; } // post multiply by scalar const CVector operator*(const scalar_t &s) const { return CVector(x*s, y*s, z*s); } // pre multiply by scalar friend inline const CVector operator*(const scalar_t &s, const CVector &vec) { return vec*s; } /* friend inline const CVector operator*(const CVector &vec, const scalar_t &s) { return CVector(vec.x*s, vec.y*s, vec.z*s); } */ // divecide by scalar const CVector operator/(scalar_t s) const { s = 1/s; return CVector(s*x, s*y, s*z); } // cross product const CVector CrossProduct(const CVector &vec) const { return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x); } // cross product const CVector operator^(const CVector &vec) const { return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x); } // dot product const scalar_t DotProduct(const CVector &vec) const { return x*vec.x + y*vec.x + z*vec.z; } // dot product const scalar_t operator%(const CVector &vec) const { return x*vec.x + y*vec.x + z*vec.z; } // length of vector const scalar_t Length() const { return (scalar_t)sqrt((double)(x*x + y*y + z*z)); } // return the unit vector const CVector UnitVector() const { return (*this) / Length(); } // normalize this vector void Normalize() { (*this) /= Length(); } const scalar_t operator!() const { return sqrtf(x*x + y*y + z*z); } // return vector with specified length const CVector operator | (const scalar_t length) const { return *this * (length / !(*this)); } // set length of vector equal to length const CVector& operator |= (const float length) { return *this = *this | length; } // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); } // reflect this vector off surface with normal vector const CVector inline Reflection(const CVector& normal) const { const CVector vec(*this | 1); // normalize this vector return (vec - normal * 2.0 * (vec % normal)) * !*this; } }; #endif Edited by - executor_2k2 on February 2, 2002 7:28:32 PM

Share this post


Link to post
Share on other sites
Advertisement
Quake III is written in C, there are no classes. Guess what they use?

The only difference between classes and structs is that structs are public by default (in C++, there''s no private/public/protected in C) and classes are private by default. There''s no performance difference, no magic additional overheads, no resource usage difference; there''s nothing else that isn''t the same between them.

Share this post


Link to post
Share on other sites
Please don''t habitually post that much code. People don''t read it and it makes it more difficult to understand what your problem is. For example, we only needed to see your struct/class definition (not that it''s even your class).

If classes are slower in your code, then you don''t really know how to use them yet. As "Null" pointed out, structs are a particular instance of classes in C++ anyway. (So are unions).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Ok sorry. The reason why is used that code and not my own is because i wanted to see what the best method was before i make my own implimentation. Anyways, thx for the clarification

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!