-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.cpp
57 lines (52 loc) · 947 Bytes
/
math.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "math3.h"
#include "quaternion.h"
#include "vector3d.h"
float Dot3(Vector3 v1, Vector3 v2){
float res=v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
return res;
}
Vector3 Cross3(Vector3 v1, Vector3 v2){
Vector3 res;
res.x=v1.x*v2.z-v1.z*v2.y;
res.y=v1.z*v2.x-v1.x*v2.z;
res.z=v1.x*v2.y-v1.y*v2.x;
return res;
}
float magn3(Vector3 v1){
float magn=sqrt(v1.x*v1.x+v1.y*v1.y+v1.z*v1.z);
return magn;
}
Vector3 Normalize3(Vector3 v){
float magn=magn3(v);
Vector3 v1;
v1.x=v.x/magn;
v1.y=v.y/magn;
v1.z=v.z/magn;
return v1;
}
namespace irr{
namespace core{
vector3df rotatevector(quaternion q,vector3df v){
quaternion q1;
q.normalize();
q1.X=v.X;
q1.Y=v.Y;
q1.Z=v.Z;
q1.W=0;
q1=q*q1*q.makeInverse();
v.X=q1.X;
v.Y=q1.Y;
v.Z=q1.Z;
return v;
}
vector3df rotatevector2(quaternion q,vector3df v){
vector3df q1;
q1.X=q.X;
q1.Y=q.Y;
q1.Z=q.Z;
vector3df t= (v.crossProduct(q1))*2;
v=v+t*(q.W)+q1.crossProduct(t);
return v;
}
}
}