-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cpp
79 lines (62 loc) · 1.7 KB
/
Entity.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/***************************************************
Entity.cpp
Source file for Entity Class
CPSC8170 - Proj 1 GBG 8/2013
****************************************************/
#include "Entity.h"
using namespace std;
//
// Constructor
//
Entity::Entity(){
Start = true;
Stop = true;
Step = false;
Rest = false;
}
//
// Setters
//
void Entity::SetRest(int rest) { Rest = rest; }
void Entity::SetStart(int start) { Start = start; }
void Entity::SetStop(int stop) { Stop = stop; }
void Entity::SetStep(int step) { Step = step; }
void Entity::SetRadius(double r) { Radius = r; }
//
// Getters
//
int Entity::IsRest() { return Rest; }
int Entity::IsStart() { return Start; }
int Entity::IsStop() { return Stop; }
int Entity::IsStep() { return Step; }
double Entity::GetRadius() { return Radius; }
//
// Functions
//
// fast triangle intersection test
// should be called by collidable objects
// uh...returns the index of the triangle that's closeset
// pass in as &fhit
int Entity::CheckCollision(Vector3d pcen, Vector3d pvel, Vector3d pnewcen) {
int i, rtni;
float f, fhit;
double u, v, a;
Vector3d p0, p1, p2;
rtni = -1;
fhit = 100;
for (i = 0; i < ntriangles/2; i++) {
p0.set(vertices[triangles[i][0]]);
p1.set(vertices[triangles[i][1]]);
p2.set(vertices[triangles[i][2]]);
a = ((p2 - p1) % (p1 - p0)).norm();
u = ((p2 - p1) % (pcen - p1)) * normals[i] / a;
v = ((p0 - p2) % (pcen - p2)) * normals[i] / a;
if(u >= 0.001 && v >= 0.001 && u + v <= 1 && u + v > -0.001) { // we hit...so now to calculate...
f = ((pcen - p1) * normals[i] / ((pcen - pnewcen) * normals[i]));
if(f >= 0 && f < 1 && f < fhit) {
rtni = i;
}
}
}
return rtni;
}