Original Post
EDIT: I hope this is in the right section. I wasn't sure whether to post this here or at the Maths and Physics forum, but I realized how much this actually matters to game programming and how little mathematically it gets, so I thought it might be better off here. Feel free to correct this if needed though! I recently wrote this vector class (it's a structure actually) and I wonder what could be improved for speed and simplicity to use and what kind of functionality needs to be added in order to give the user everything he needs to do all kinds of vector calculations (to create averaged normals for lighting or for phyisical simulations etc). Currently it supports simple and direct access to all three elements of the vector (x, y and z), a function to add two vectors together (by using the plus-operator for simplicity), a function to calculate both the dot- and cross-product of two given vectors and a function that simply normalizes the vector. Here's the complete file: So, this gives me the possibility all of this in my projects: // define a vector first cvector vector; // now give it some values vector.x = 3.0f; vector.y = 5.0f; vector.z = 2.0f; // we could now add another vector to it like this vector + another_vector; // or we can calculate both the dot- or cross-product vector.dot_product ( another_vector ); vector.cross_product ( another_vector ); // and last but not least we can normalize our vector vector.normalize ( ); I wrote my own function to return absolute values, because I only found one for integer values and not for float values. I am sure there are several tricks to avoid taking the square roots in my whole structure, but this is the first time I am writing a vector class, so please cope with my possible newbieness. ;)
#include <math.h>
float absolute ( float value )
// returns the absolute value for a given value
// and returns it
{
// make the value positive by taking the
// square root of the square and return it
return ( ( float ) sqrt ( value * value ) );
}
struct cvector
// structure for three-dimensional vectors
{
// define the elements for the vector
float x, y, z;
cvector operator + ( const cvector &other )
// performs vector addition
{
cvector result;
// add all elements to each other
result.x = x + other.x;
result.y = y + other.y;
result.z = z + other.z;
// and return the result
return result;
}
cvector cross_product ( const cvector &other )
// calculates the cross-product for two
// given vectors
{
cvector result;
// calculate all results
result.x = ( y * other.z ) - ( z * other.y );
result.y = ( z * other.x ) - ( x * other.z );
result.z = ( x * other.y ) - ( y * other.x );
// and return the resulting vector
return result;
}
float dot_product ( const cvector &other )
// calculates the dot-product for two given
// vectors
{
// return the resulting value
return ( x * other.x ) + ( y * other.y ) + ( z * other.z );
}
void normalize ( void )
// normalizes the vector - that makes the vector
// length equal one
{
float length;
// calculate the length of the vector first
length = absolute ( ( float ) sqrt ( ( x * x ) + ( y * y ) + ( z * z ) ) );
// now divide each element of the vector
// by the length of the vector
x = x / length;
y = y / length;
z = z / length;
}
};