-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcollision.cpp
156 lines (129 loc) · 3.19 KB
/
collision.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
** collision.cpp
**
** Some nice vector functions and stuff... for PixelMachine
** by SuperJer
**
** Email = [email protected]
** Web = http://www.superjer.com/
**
** You can do whatever you like with this code, I don't care,
** just please recognize me as the original author. :)
**
*/
#include "collision.h"
V &add( V &out, const V &a, const V &b )
{
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
return out;
}
V &subtract( V &out, const V &a, const V &b )
{
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
return out;
}
V &cross( V &out, const V &a, const V &b )
{
out.x = a.y*b.z - a.z*b.y;
out.y = a.z*b.x - a.x*b.z;
out.z = a.x*b.y - a.y*b.x;
return out;
}
V &scale( V &out, double n )
{
out.x *= n;
out.y *= n;
out.z *= n;
return out;
}
V &normalize( V &out, double n )
{
double l = sqrt(out.x*out.x + out.y*out.y + out.z*out.z);
out.x *= n/l;
out.y *= n/l;
out.z *= n/l;
return out;
}
V &mirror( V &out, const V &in, const V &mirror )
{
V perp1, perp2;
cross( perp1, in, mirror );
cross( perp2, perp1, mirror );
const double &a = mirror.x;
const double &b = perp2.x;
const double &c = in.x;
const double &x = mirror.y;
const double &y = perp2.y;
const double &z = in.y;
const double &i = mirror.z;
const double &j = perp2.z;
const double &k = in.z;
double n, m;
if( a > -0.0001 && a < 0.0001 )
{
if( i > -0.0001 && i < 0.0001 )
{
out.x = -in.x;
out.y = in.y;
out.z = -in.z;
return out;
}
else
{
n = (z - (x*k)/i)/(y - (x*j)/i);
m = (k - (j*n))/i;
}
}
else
{
n = (z - (x*c)/a)/(y - (x*b)/a);
m = (c - (b*n))/a;
}
V v1, v2;
v1 = mirror;
v2 = perp2;
scale( v1, m );
scale( v2, n );
subtract( out, v1, v2 );
return out;
}
// This function is part of the SPARToR game engine by SuperJer.com
void CollideRaySphere( double &t, const V &o, const V &v, const V &p, const double &r )
{
double a, b, c, d, ox, oy, oz;
// check that v is even headed toward the sphere first
{
if( (v.x>0.0 && o.x>p.x+r) || (v.x<0.0 && o.x<p.x-r) ||
(v.y>0.0 && o.y>p.y+r) || (v.y<0.0 && o.y<p.y-r) ||
(v.z>0.0 && o.z>p.z+r) || (v.z<0.0 && o.z<p.z-r) )
{
t = NO_COLLISION;
return;
}
}
// quadratic formula
ox = o.x - p.x;
oy = o.y - p.y;
oz = o.z - p.z;
a = 2.0*(v.x*v.x + v.y*v.y + v.z*v.z);
b = 2.0*(v.x*ox + v.y*oy + v.z*oz);
c = ox*ox + oy*oy + oz*oz - r*r;
d = b*b - 2.0*a*c; // determinant
if( d < 0.0 ) // no intersection at all
{
t = NO_COLLISION;
return;
}
a = 1.0 / a;
d = sqrt( d );
t = (-d - b) * a;
if( t >= 0.0 ) return; // smaller root wins
t = ( d - b) * a;
if( t >= 0.0 ) return; // bigger root wins
t = NO_COLLISION;
return; // no root in range
}