🔒 Locked
Vector Class
Advertisement
Original Post
Greetings I plan on making a Vector class for future use with future projects. I've been told that they are useful, but I can't figure out what they are or why they would be used. Also, where could I get some formula's that I would need or a list of things that should be part of the class. Thanks.
You can think of a vector as a one dimensional array. A vector only goes in one direction (adding onto it) and can dynamically increase in size. It's basically an array that allocates memory and reallocates it when needed. This is great because you can dynamically add data to the vector and not have to worry about memory management. I've converted my VList (on my website) into a class CVectorList, and fixed some bugs I have. So far It works fantastic for what I need.
Or you could be talking about a Mathematical vector - x, y, z (coords) which I use for vertices of 3d models, along with physics modeling (velocity, acceleration, Forces, torques). I suggest make something *when* you need them instead of making stuff in hopes that you will use them someday. Start projects and see how it would be easier to implement something. Then research and figure out the best way around the problem before writing code. Just my 2 cents
Yeah there are two types of vectors, well they are technically the same thing, but if you want the array type use the STL implementation
#include
using std::vector;
The other vector is a 1x3 matrix and is used in physics, ie veloctiy is a vector, it is a magnitude and a direction.
#include
using std::vector;
The other vector is a 1x3 matrix and is used in physics, ie veloctiy is a vector, it is a magnitude and a direction.
Quote:
Original post by ratt1233
Or you could be talking about a Mathematical vector - x, y, z (coords) which I use for vertices of 3d models, along with physics modeling (velocity, acceleration, Forces, torques). I suggest make something *when* you need them instead of making stuff in hopes that you will use them someday. Start projects and see how it would be easier to implement something. Then research and figure out the best way around the problem before writing code. Just my 2 cents
Yup, thats the one I was thinking of. I'm going to begin 3D programming with OGL rther than 2D, and I've been told that they come in handy for moving the player, bullets etc. I was just looking for a tut/article on making a vector class that woudl be suffitient for my purposes.
if you know vector maths making a vector class is a cake walk, if you dont know vector maths, well, its time to learn [wink]
I just made myself one because I needed to do my own terrain normals in opengl. I understood cross and dot product stuff from physics but I never knew how to use it in programming. So I gave it a shot. I just wish I knew why my terrain looks like a chess board now, lots of black and white quads. Maybe I need to average the normals?
This I can help you with.
1)The dot product mutliplys the components of each vector that are parallel to each other. It finds the angle between two vectors and gives a scalar answer.
2)The dot product multiplys the components of the vectors that are perpendicular to each other. The answer that results is another vector and its direction is perpedicular to the plane the first two vectors lie on.
3)A normalizing function takes the answer from your dot product and divdes it by the length of that vector. This effecitivly makes it equal one. This vector can then be used to do lighting.
Example:
Dot Product-(x*x2,y*y2,z*z2)
Cross Product-(y*z2 - z*y2, z*x2 - x*z2, x*y2 - y*x2)
Normal- First you find the length
length=sqr(x^2+y^2+z^2)
then you divide each component of your new vector by the length
x/=length
y/=length
z/=length
this will normalize the vector so that is can be used for lighting applications
1)The dot product mutliplys the components of each vector that are parallel to each other. It finds the angle between two vectors and gives a scalar answer.
2)The dot product multiplys the components of the vectors that are perpendicular to each other. The answer that results is another vector and its direction is perpedicular to the plane the first two vectors lie on.
3)A normalizing function takes the answer from your dot product and divdes it by the length of that vector. This effecitivly makes it equal one. This vector can then be used to do lighting.
Example:
Dot Product-(x*x2,y*y2,z*z2)
Cross Product-(y*z2 - z*y2, z*x2 - x*z2, x*y2 - y*x2)
Normal- First you find the length
length=sqr(x^2+y^2+z^2)
then you divide each component of your new vector by the length
x/=length
y/=length
z/=length
this will normalize the vector so that is can be used for lighting applications
Answer to qn (1):
Basically, a dot product (also called a scalar product) is a projection of one vector onto another. In physics, its also known as resolving.
let * = dot product operator, a = any non-zero vector, b = any non-zero vector
c = angle between vector a & vector b
let a = (xa)i + (ya)j + (za)k
b = (xb)i + (yb)j + (zb)k
definition: a*b = |a||b|cos c
if you follow through the definition and draw two vectors on a piece of paper, you should be able to quickly see that what you are actually doing is getting the projection of the vector a onto the vector b, that is resolving the vector a in the direction of vector b.
the above can be simplified to
a*b = (xa*xb) + (ya*yb) + (za*zb)
Answer to qn (2):
A cross product (also called a vector product) is a bit more difficult to explain. Basically, it produces a vector that is perpendicular to both the original vectors (i.e. a normal).
let x = cross product operator
definition: a x b = |a||b|sin c . n
where n = a unit vector perpendicular to both a & b
there's another way to evaluate the cross product, but that involves using a 3x3 matrix and finding its determinant.
a x b =
| i j k |
| xa ya za |
| xb yb zb |
Answer to qn (3):
Well, that depends on whether you mean 'normalizing' or normal. As explained above, to find a normal to a 'surface', u find the cross product of 2 vectors on that surface. To normalise a vector however, you just divide that vector by its magnitude (hence giving it a magnitude of 1)
p = q/|q|, where p & q are any non-zero vectors.
This is just the basic run-down of 3 dimensional vectors. I would suggest googling both dot and cross product. There are probably many sites that can explain it much better than I can with this limited formating. However, if you plan on doing much 3D programming, I would heavily suggest that you learn this as it can help a lot.
Just my 2 cents!
Basically, a dot product (also called a scalar product) is a projection of one vector onto another. In physics, its also known as resolving.
let * = dot product operator, a = any non-zero vector, b = any non-zero vector
c = angle between vector a & vector b
let a = (xa)i + (ya)j + (za)k
b = (xb)i + (yb)j + (zb)k
definition: a*b = |a||b|cos c
if you follow through the definition and draw two vectors on a piece of paper, you should be able to quickly see that what you are actually doing is getting the projection of the vector a onto the vector b, that is resolving the vector a in the direction of vector b.
the above can be simplified to
a*b = (xa*xb) + (ya*yb) + (za*zb)
Answer to qn (2):
A cross product (also called a vector product) is a bit more difficult to explain. Basically, it produces a vector that is perpendicular to both the original vectors (i.e. a normal).
let x = cross product operator
definition: a x b = |a||b|sin c . n
where n = a unit vector perpendicular to both a & b
there's another way to evaluate the cross product, but that involves using a 3x3 matrix and finding its determinant.
a x b =
| i j k |
| xa ya za |
| xb yb zb |
Answer to qn (3):
Well, that depends on whether you mean 'normalizing' or normal. As explained above, to find a normal to a 'surface', u find the cross product of 2 vectors on that surface. To normalise a vector however, you just divide that vector by its magnitude (hence giving it a magnitude of 1)
p = q/|q|, where p & q are any non-zero vectors.
This is just the basic run-down of 3 dimensional vectors. I would suggest googling both dot and cross product. There are probably many sites that can explain it much better than I can with this limited formating. However, if you plan on doing much 3D programming, I would heavily suggest that you learn this as it can help a lot.
Just my 2 cents!
Quote:
Original post by PlutoWarrior
I just made myself one because I needed to do my own terrain normals in opengl. I understood cross and dot product stuff from physics but I never knew how to use it in programming. So I gave it a shot. I just wish I knew why my terrain looks like a chess board now, lots of black and white quads. Maybe I need to average the normals?
Ah, i had a problem like this when i was quickly crowbaring lighting into a terrain system I wrote, and yes, you have to add all the normals for each face a vertex is in and then, if the face normals are already normalised you can just normalise the result, if they arent.. well i cant remember, I produced normlised face normals [wink], but its in a post around here somewhere.
Great, at least I know that I'm headed in the right direction. Thanks, I'll take a look around for the info.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement