My 3D Vector class

This is a discussion on My 3D Vector class within the C++ Programming forums, part of the General Programming Boards category; I'm working on a test 3D engine, and have started on a 3D vector class. I'm looking for opinions, feel ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    My 3D Vector class

    I'm working on a test 3D engine, and have started on a 3D vector class. I'm looking for opinions, feel free to use it in your code if you wish (probably not enough there to use anyway :p ) . *NOTE: If this is in the wrong place, please tell me where to move it :D .* Also, this is not completed, so please don't yell at me for that :) . If something is wrong, PLEASE correct me. Anyway, here it is:

    Code:
    /*
    The Vector3D class: represents a 3 dimensional vector. Only has 3 variables: x, y, and z for displacement.
    */
    
    class Vector3D {
     public:
    
    /*Variables:
     I didn't make x, y, and z private, as the programmer should already know the inner workings of a vector,
    and would figure that there would be an x, y, and z variables. Plus, I hate the look of setX(int x) and getX() :D .
    */
      double x, y, z;
    
    /*Constructors:
     Vector3D v: x, y, and z are all set to 0.
     Vector3D v2(1, 2, 3): x, y, and z are set to 1, 2, and 3, respectivly.
     Vector3D v3(Vector3D v4): v3.x, v3.y, and v3.z are all set to v4.x, v4.y, and v4.z, respectivly.
    */
      Vector3D() {
       x = y = z = 0;
      }
    
      Vector3D(double a, double b, double c) {
       x = a;
       y = b;
       z = c;
      }
    
      Vector3D(const Vector3D &v) {
       x = v.x;
       y = v.y;
       z = v.z;
      }
    
    /*Operator overloadings:
     Vector3D operator+(Vector v): x, y, and z are added to v.x, v.y, and v.z, respectivly.
     Vector3D operator-(Vector3D v): x, y, and z are subtracted from v.x, v.y, and v.z, respectivly.
     Vector3D operator*(double w): x, y, and z are all multiplied by w.
     Vector3D operator/(double w): x, y, and z are all divided by w.
     Vector3D operator=(Vector3D v): this vector is set to v.
    */
      Vector3D operator+(Vector3D v);
      Vector3D operator-(Vector3D v);
      Vector3D operator*(double w);
      Vector3D operator/(double w);
      Vector3D operator=(Vector3D v);
    
    /*Other functions:
     void negate(): negate the vector - x, y, and z are set to -x, -y, and -z, respectivly
     double length(): returns the length of the vector.
     void normalize(): normalize the vector, vector / length of vector
    */
      void negate();
      double length();
      void normalize();
    };
    
    Vector3D Vector3D::operator+(Vector3D v) {
     return Vector3D((x + v.x), (y + v.y), (z + v.z));
    }
    
    Vector3D Vector3D::operator-(Vector3D v) {
     return Vector3D((x - v.x), (y - v.y), (z - v.z));
    }
    
    Vector3D Vector3D::operator*(double w) {
     return Vector3D((x * w), (y * w), (z * w));
    }
    
    Vector3D Vector3D::operator/(double w) {
     return Vector3D((x / w), (y / w), (z / w));
    }
    
    Vector3D Vector3D::operator=(Vector3D v) {
     x = v.x;
     y = v.y;
     z = v.z;
     return *this;
    }
    
    void Vector3D::negate() {
     x = -x;
     y = -y;
     z = -z;
    }
    
    double Vector3D::length() {
     return sqrt((x * x) + (y * y) + (z * z));
    }
    
    void Vector3D::normalize() {
     x /= length();
     y /= length();
     z /= length();
    }
    Last edited by Lurker; 11-09-2003 at 08:47 PM.
    Do not make direct eye contact with me.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What about cross-products or dot products?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    void Vector3D::crossWith( Vector3D a )
    {
    
    	double x1, y1, z1;
    
    	x1 = ( a.y * b.z ) - ( a.z * b.y );
    	y1 = ( a.z * b.x ) - ( a.x * b.z );
    	z1 = ( a.x * b.y ) - ( a.y * b.x );
    
    	x = x1;
    	y = y1;
    	z = z1;
    
    }
    
    double Vector3D::dotProduct( Vector3D a )
    {
    
    	return ( x * b.x ) + ( y * b.y ) + ( z * b.z );
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Also, this is not completed, so please don't yell at me for that .
    I'm not looking at your other post, I'm making my own code .
    Do not make direct eye contact with me.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Oops. My attention span is shot today, so I missed that part. Ooh.... shinyy.....
    Last edited by XSquared; 11-09-2003 at 09:26 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by XSquared
    attemtion
    Hahah - I see .
    Do not make direct eye contact with me.

  7. #7
    ___
    Join Date
    Jun 2003
    Posts
    806
    Whats a 3d Vector do?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    zakk, sorry but go google.

    Lurker, it looks good just keep implementing stuff. I didn't make X,Y and Z private either. In fact I think the private keyword should be removed from C++ because it is one of the most pointless and least useful keywords in existence...if anyone is dumb enough to need their own data hidden from you then you shouldn't be allowed to program or operate a motor vehicle
    Last edited by Silvercord; 11-10-2003 at 06:10 PM.

  9. #9
    ___
    Join Date
    Jun 2003
    Posts
    806
    Damn Silver give me more credit than that! I askready did a search on it. The best I can grasp is something about creating 3d graphics for you and the bit. Is that right?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    A vector is a quantity with multiple components. In the case of Lurker's code it is a quantity that either represents
    a) position
    b) direction

    but a vector can have any number of components, in my engine I use four component (four dimensional) vectors for some of the calculations (if it has 1 component it is called a scalar). In my assembly instruction set books a byte is sometimes referred to as a vector with eight components (x86 instruction set).


    EDIT: also lurker, don't implement DotProduct and Crossproduct without really understanding what they do. DotProduct is more important and is used more in professional 3D game engines than any other mathematical entity. DotProduct is used for computing basic distances, but can also be used for finding the angle between vectors (the dotproduct between two normalized vectors is the cosine between the vectors in radians). You'll find the dotproduct most pertinent when using the plane equation, because the dotproduct between a position in 3D space and the normal to the plane, minus the plane's distance from the origin, gives you the distance from the plane.

    Normal to plane is normalized (has a length of one)
    DotProduct(Position,Plane.Normal) - Plane.DistanceFromOrigin = Position's distance from plane
    The crossproduct takes the 'hand' as the first parameter, the 'palm' as the second parameter, and returns the 'thumb' vector (called the 'right hand rule', so you always know what direction the new vector will point in). The length isn't preserved unless the angle between the two vectors is 90 degrees and they are both unit vectors, otherwise the magnitude of the new vector is :
    ||A|| * ||B|| * sin(anglebetweenvectors)

    EDIT1: I should've just told you both to go googling, but i have low self esteem.
    Last edited by Silvercord; 11-10-2003 at 06:29 PM.

  11. #11
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I know what they do and how they are used - I spent 10 minutes on that code, and didn't have them ready when I posted it .
    Do not make direct eye contact with me.

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    okay, just make sure you actually understand *everything* you put into code. Meaningless copying equations and stuff will put you in the hole if you end up trying to develop anything moderately complex.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    if it has 1 component it is called a scalar
    Technically not true. A vector is a certain type of element of a vector space. A vector space ofver some field K is an ordered triple (G, +, *). G is an additive group (known as the vector group, so its elements are the 'vectors'). + is the addition operation over the group G. * is a function from KxG -> G... that is, a scalar product. Elements of the field (techincally, it need only be a ring). There are also a few conditions which must be met by the relations.

    Both scalars and vectors can also be considered to be specializations of the tensor concept. An m dimensional tensor of order n can be uniquely identified by m^n numbers. A scalar is of order 0 and a vector of order 1. Though a scalar and a 1 dimensional vector can both be identified by one number, they are still of a different order.

    Anyways, not that any of that matters... I was just a bit bored.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    ...all kinds of crap nobody cares about (and is unimportant).

    I'll say again, if it has one component, it is a scalar. Ask anybody who actually does 3d programming. And Zach next time you try to make someone seem wrong for no good reason at least type stuff people will actually read.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> ...all kinds of crap nobody cares about (and is unimportant).

    People working in CS fields such as cryptography care. Algebra is quite important to that field, and vectors are quite important, yet in a different way than for graphics programming.

    >> I'll say again, if it has one component, it is a scalar.

    Certainly not if they come from different sets.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 1 of 8 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Network Analysis Tool
    By durban in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 11-08-2005, 06:33 PM
  2. A 3D Program for my 3D models...?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 08-19-2004, 11:04 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 01:40 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 08:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21