# My 3D Vector class

Show 80 post(s) from this thread on one page
Page 1 of 8 12345678 Last
• 11-09-2003
Lurker
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(); }```
• 11-09-2003
XSquared
What about cross-products or dot products?
• 11-09-2003
XSquared
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 ); }```
• 11-09-2003
Lurker
Quote:

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 :D :p .
• 11-09-2003
XSquared
Oops. My attention span is shot today, so I missed that part. Ooh.... shinyy.....
• 11-09-2003
Lurker
Quote:

Originally posted by XSquared
attemtion
Hahah - I see :D .
• 11-10-2003
ZakkWylde969
Whats a 3d Vector do?
• 11-10-2003
Silvercord

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
• 11-10-2003
ZakkWylde969
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?
• 11-10-2003
Silvercord
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.
• 11-10-2003
Lurker
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 :( .
• 11-10-2003
Silvercord
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.
• 11-10-2003
Zach L.
Quote:

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.
• 11-10-2003
Silvercord
...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.
• 11-10-2003
Zach L.
>> ...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.
Show 80 post(s) from this thread on one page
Page 1 of 8 12345678 Last