#include "dxstdafx.h" #include ".\vector.h" Vector::Vector(void) : D3DXVECTOR4(0, 0, 0, 1) { } Vector::Vector(float _x, float _y, float _z) : D3DXVECTOR4(_x, _y, _z, 1) { } Vector::Vector(NxVec3 vec) : D3DXVECTOR4(vec.x, vec.y, vec.z,1) { } void Vector::normalize() { float thelen = 1/sqrt (x*x + y*y + z*z); if (thelen != 0) { x *= thelen; y *= thelen; z *= thelen; } } float Vector::length() { return sqrt(x*x + y*y + z*z); } NxVec3 Vector::getNxVector() { this->nxVector.x = this->x; this->nxVector.y = this->y; this->nxVector.z = this->z; return this->nxVector; } void Vector::setNxVector(NxVec3 &nxv) { //D3DVector aktualisieren this->x = nxv.x; this->y = nxv.y; this->z = nxv.z; this->w = 1; } D3DXVECTOR3 Vector::getD3DXVector3() { D3DXVECTOR3 temp(this->x, this->y, this->z); return temp; } NxQuat Vector::getNxQuatRotation() { NxQuat q; float _x, _y, _z,cr, cp, cy, sr, sp, sy, cpcy, spsy; _x = this->x/2; _y = this->y/2; _z = this->z/2; cr = cos(_x); cp = cos(_y); cy = cos(_z); sr = sin(_x); sp = sin(_y); sy = sin(_z); cpcy = cp * cy; spsy = sp * sy; q.setx(sr * cpcy - cr * spsy); q.sety(cr * sp * cy + sr * cp * sy); q.setz(cr * cp * sy - sr * sp * cy); q.setw(cr * cpcy + sr * spsy); return q; } Vector Vector::crossProd(Vector &v2) { Vector v(y*v2.z-z*v2.y, z*v2.x-x*v2.z, x*v2.y-y*v2.x); return v; } float Vector::dotProd(Vector &v2) { float ret = x*v2.x + y*v2.y + z*v2.z; return ret; } // assignment operators Vector Vector::operator += ( CONST Vector& v) { D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)+v; Vector retv(tmpv.x, tmpv.y, tmpv.z); return retv; } Vector Vector::operator -= ( CONST Vector& v) { D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)-v; Vector retv(tmpv.x, tmpv.y, tmpv.z); return retv; } Vector Vector::operator *= ( float a) { D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)*a; Vector retv(tmpv.x, tmpv.y, tmpv.z); return retv; } Vector Vector::operator /= ( float a) { D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)/a; Vector retv(tmpv.x, tmpv.y, tmpv.z); return retv; } // binary operators Vector Vector::operator + ( CONST Vector& v) const { Vector retv(this->x+v.x, this->y+v.y, this->z+v.z); return retv; } Vector Vector::operator - ( CONST Vector& v) const { Vector retv(this->x-v.x, this->y-v.y, this->z-v.z); return retv; } Vector Vector::operator * ( float a) const { Vector retv(this->x*a, this->y*a, this->z*a); return retv; } Vector Vector::operator / ( float a) const { Vector retv(this->x/a, this->y/a, this->z/a); return retv; } void Vector::applyD3DXVector(D3DXVECTOR4 *v) { this->x = v->x; this->y = v->y; this->z = v->z; this->w = v->w; } void Vector::setXYZ(float _x, float _y, float _z) { this->x = _x; this->y = _y; this->z = _z; this->w = 1; } void Vector::addXYZ(float _x, float _y, float _z) { this->x += _x; this->y += _y; this->z += _z; }