1 | #include "dxstdafx.h"
|
---|
2 | #include ".\vector.h"
|
---|
3 |
|
---|
4 | Vector::Vector(void) : D3DXVECTOR4(0, 0, 0, 1)
|
---|
5 | {
|
---|
6 | }
|
---|
7 |
|
---|
8 | Vector::Vector(float _x, float _y, float _z) : D3DXVECTOR4(_x, _y, _z, 1)
|
---|
9 | {
|
---|
10 | }
|
---|
11 |
|
---|
12 | Vector::Vector(NxVec3 vec) : D3DXVECTOR4(vec.x, vec.y, vec.z,1)
|
---|
13 | {
|
---|
14 | }
|
---|
15 |
|
---|
16 | void Vector::normalize()
|
---|
17 | {
|
---|
18 | float thelen = 1/sqrt (x*x + y*y + z*z);
|
---|
19 |
|
---|
20 | if (thelen != 0)
|
---|
21 | {
|
---|
22 | x *= thelen;
|
---|
23 | y *= thelen;
|
---|
24 | z *= thelen;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | float Vector::length()
|
---|
29 | {
|
---|
30 | return sqrt(x*x + y*y + z*z);
|
---|
31 | }
|
---|
32 |
|
---|
33 | NxVec3 Vector::getNxVector()
|
---|
34 | {
|
---|
35 | this->nxVector.x = this->x;
|
---|
36 | this->nxVector.y = this->y;
|
---|
37 | this->nxVector.z = this->z;
|
---|
38 | return this->nxVector;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void Vector::setNxVector(NxVec3 &nxv)
|
---|
42 | {
|
---|
43 | //D3DVector aktualisieren
|
---|
44 | this->x = nxv.x;
|
---|
45 | this->y = nxv.y;
|
---|
46 | this->z = nxv.z;
|
---|
47 | this->w = 1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | D3DXVECTOR3 Vector::getD3DXVector3()
|
---|
51 | {
|
---|
52 | D3DXVECTOR3 temp(this->x, this->y, this->z);
|
---|
53 | return temp;
|
---|
54 | }
|
---|
55 |
|
---|
56 | NxQuat Vector::getNxQuatRotation()
|
---|
57 | {
|
---|
58 | NxQuat q;
|
---|
59 |
|
---|
60 | float _x, _y, _z,cr, cp, cy, sr, sp, sy, cpcy, spsy;
|
---|
61 |
|
---|
62 | _x = this->x/2;
|
---|
63 | _y = this->y/2;
|
---|
64 | _z = this->z/2;
|
---|
65 |
|
---|
66 | cr = cos(_x);
|
---|
67 | cp = cos(_y);
|
---|
68 | cy = cos(_z);
|
---|
69 |
|
---|
70 | sr = sin(_x);
|
---|
71 | sp = sin(_y);
|
---|
72 | sy = sin(_z);
|
---|
73 |
|
---|
74 | cpcy = cp * cy;
|
---|
75 | spsy = sp * sy;
|
---|
76 |
|
---|
77 | q.setx(sr * cpcy - cr * spsy);
|
---|
78 | q.sety(cr * sp * cy + sr * cp * sy);
|
---|
79 | q.setz(cr * cp * sy - sr * sp * cy);
|
---|
80 | q.setw(cr * cpcy + sr * spsy);
|
---|
81 |
|
---|
82 | return q;
|
---|
83 | }
|
---|
84 |
|
---|
85 | Vector Vector::crossProd(Vector &v2)
|
---|
86 | {
|
---|
87 | Vector v(y*v2.z-z*v2.y, z*v2.x-x*v2.z, x*v2.y-y*v2.x);
|
---|
88 | return v;
|
---|
89 | }
|
---|
90 |
|
---|
91 | float Vector::dotProd(Vector &v2) {
|
---|
92 | float ret = x*v2.x + y*v2.y + z*v2.z;
|
---|
93 | return ret;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // assignment operators
|
---|
97 | Vector Vector::operator += ( CONST Vector& v)
|
---|
98 | {
|
---|
99 | D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)+v;
|
---|
100 | Vector retv(tmpv.x, tmpv.y, tmpv.z);
|
---|
101 | return retv;
|
---|
102 | }
|
---|
103 |
|
---|
104 | Vector Vector::operator -= ( CONST Vector& v)
|
---|
105 | {
|
---|
106 | D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)-v;
|
---|
107 | Vector retv(tmpv.x, tmpv.y, tmpv.z);
|
---|
108 | return retv;
|
---|
109 | }
|
---|
110 |
|
---|
111 | Vector Vector::operator *= ( float a)
|
---|
112 | {
|
---|
113 | D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)*a;
|
---|
114 | Vector retv(tmpv.x, tmpv.y, tmpv.z);
|
---|
115 | return retv;
|
---|
116 | }
|
---|
117 |
|
---|
118 | Vector Vector::operator /= ( float a)
|
---|
119 | {
|
---|
120 | D3DXVECTOR4 tmpv = (D3DXVECTOR4) (*this)/a;
|
---|
121 | Vector retv(tmpv.x, tmpv.y, tmpv.z);
|
---|
122 | return retv;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // binary operators
|
---|
126 | Vector Vector::operator + ( CONST Vector& v) const
|
---|
127 | {
|
---|
128 | Vector retv(this->x+v.x, this->y+v.y, this->z+v.z);
|
---|
129 | return retv;
|
---|
130 | }
|
---|
131 |
|
---|
132 | Vector Vector::operator - ( CONST Vector& v) const
|
---|
133 | {
|
---|
134 | Vector retv(this->x-v.x, this->y-v.y, this->z-v.z);
|
---|
135 | return retv;
|
---|
136 | }
|
---|
137 |
|
---|
138 | Vector Vector::operator * ( float a) const
|
---|
139 | {
|
---|
140 | Vector retv(this->x*a, this->y*a, this->z*a);
|
---|
141 | return retv;
|
---|
142 | }
|
---|
143 |
|
---|
144 | Vector Vector::operator / ( float a) const
|
---|
145 | {
|
---|
146 | Vector retv(this->x/a, this->y/a, this->z/a);
|
---|
147 | return retv;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void Vector::applyD3DXVector(D3DXVECTOR4 *v) {
|
---|
151 | this->x = v->x;
|
---|
152 | this->y = v->y;
|
---|
153 | this->z = v->z;
|
---|
154 | this->w = v->w;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void Vector::setXYZ(float _x, float _y, float _z)
|
---|
158 | {
|
---|
159 | this->x = _x;
|
---|
160 | this->y = _y;
|
---|
161 | this->z = _z;
|
---|
162 | this->w = 1;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void Vector::addXYZ(float _x, float _y, float _z)
|
---|
166 | {
|
---|
167 | this->x += _x;
|
---|
168 | this->y += _y;
|
---|
169 | this->z += _z;
|
---|
170 | }
|
---|