Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Gjk.cs
Go to the documentation of this file.
1
using
System
;
2
3
namespace
Microsoft.Xna.Framework
;
4
5
[
Serializable
]
6
internal
class
Gjk
7
{
8
private
static
int
[]
BitsToIndices
=
new
int
[16]
9
{
10
0, 1, 2, 17, 3, 25, 26, 209, 4, 33,
11
34, 273, 35, 281, 282, 2257
12
};
13
14
private
Vector3
closestPoint
;
15
16
private
Vector3
[]
y
;
17
18
private
float
[]
yLengthSq
;
19
20
private
Vector3
[][]
edges
;
21
22
private
float
[][]
edgeLengthSq
;
23
24
private
float
[][]
det
;
25
26
private
int
simplexBits
;
27
28
private
float
maxLengthSq
;
29
30
public
bool
FullSimplex
=>
simplexBits
== 15;
31
32
public
float
MaxLengthSquared
=>
maxLengthSq
;
33
34
public
Vector3
ClosestPoint
=>
closestPoint
;
35
36
public
Gjk
()
37
{
38
y
=
new
Vector3
[4];
39
yLengthSq
=
new
float
[4];
40
edges
=
new
Vector3
[4][]
41
{
42
new
Vector3
[4],
43
new
Vector3
[4],
44
new
Vector3
[4],
45
new
Vector3
[4]
46
};
47
edgeLengthSq
=
new
float
[4][]
48
{
49
new
float
[4],
50
new
float
[4],
51
new
float
[4],
52
new
float
[4]
53
};
54
det
=
new
float
[16][];
55
for
(
int
i = 0; i < 16; i++)
56
{
57
det
[i] =
new
float
[4];
58
}
59
}
60
61
public
void
Reset
()
62
{
63
simplexBits
= 0;
64
maxLengthSq
= 0f;
65
}
66
67
public
bool
AddSupportPoint
(ref
Vector3
newPoint)
68
{
69
int
num = (
BitsToIndices
[
simplexBits
^ 0xF] & 7) - 1;
70
ref
Vector3
reference = ref
y
[num];
71
reference = newPoint;
72
yLengthSq
[num] = newPoint.LengthSquared();
73
for
(
int
num2 =
BitsToIndices
[
simplexBits
]; num2 != 0; num2 >>= 3)
74
{
75
int
num3 = (num2 & 7) - 1;
76
Vector3
vector =
y
[num3] - newPoint;
77
edges
[num3][num] = vector;
78
ref
Vector3
reference2 = ref
edges
[num][num3];
79
reference2 = -vector;
80
edgeLengthSq
[num][num3] = (
edgeLengthSq
[num3][num] = vector.
LengthSquared
());
81
}
82
UpdateDeterminant
(num);
83
return
UpdateSimplex
(num);
84
}
85
86
private
static
float
Dot
(ref
Vector3
a, ref
Vector3
b)
87
{
88
return
a.X * b.X + a.Y * b.Y + a.Z * b.Z;
89
}
90
91
private
void
UpdateDeterminant
(
int
xmIdx)
92
{
93
int
num = 1 << xmIdx;
94
det
[num][xmIdx] = 1f;
95
int
num2 =
BitsToIndices
[
simplexBits
];
96
int
num3 = num2;
97
int
num4 = 0;
98
while
(num3 != 0)
99
{
100
int
num5 = (num3 & 7) - 1;
101
int
num6 = 1 << num5;
102
int
num7 = num6 | num;
103
det
[num7][num5] =
Dot
(ref
edges
[xmIdx][num5], ref
y
[xmIdx]);
104
det
[num7][xmIdx] =
Dot
(ref
edges
[num5][xmIdx], ref
y
[num5]);
105
int
num8 = num2;
106
for
(
int
i = 0; i < num4; i++)
107
{
108
int
num9 = (num8 & 7) - 1;
109
int
num10 = 1 << num9;
110
int
num11 = num7 | num10;
111
int
num12 = ((
edgeLengthSq
[num5][num9] <
edgeLengthSq
[xmIdx][num9]) ? num5 : xmIdx);
112
det
[num11][num9] =
det
[num7][num5] *
Dot
(ref
edges
[num12][num9], ref
y
[num5]) +
det
[num7][xmIdx] *
Dot
(ref
edges
[num12][num9], ref
y
[xmIdx]);
113
num12 = ((
edgeLengthSq
[num9][num5] <
edgeLengthSq
[xmIdx][num5]) ? num9 : xmIdx);
114
det
[num11][num5] =
det
[num10 | num][num9] *
Dot
(ref
edges
[num12][num5], ref
y
[num9]) +
det
[num10 | num][xmIdx] *
Dot
(ref
edges
[num12][num5], ref
y
[xmIdx]);
115
num12 = ((
edgeLengthSq
[num5][xmIdx] <
edgeLengthSq
[num9][xmIdx]) ? num5 : num9);
116
det
[num11][xmIdx] =
det
[num6 | num10][num9] *
Dot
(ref
edges
[num12][xmIdx], ref
y
[num9]) +
det
[num6 | num10][num5] *
Dot
(ref
edges
[num12][xmIdx], ref
y
[num5]);
117
num8 >>= 3;
118
}
119
num3 >>= 3;
120
num4++;
121
}
122
if
((
simplexBits
| num) == 15)
123
{
124
int
num13 = ((!(
edgeLengthSq
[1][0] <
edgeLengthSq
[2][0])) ? ((
edgeLengthSq
[2][0] <
edgeLengthSq
[3][0]) ? 2 : 3) : ((
edgeLengthSq
[1][0] <
edgeLengthSq
[3][0]) ? 1 : 3));
125
det
[15][0] =
det
[14][1] *
Dot
(ref
edges
[num13][0], ref
y
[1]) +
det
[14][2] *
Dot
(ref
edges
[num13][0], ref
y
[2]) +
det
[14][3] *
Dot
(ref
edges
[num13][0], ref
y
[3]);
126
num13 = ((!(
edgeLengthSq
[0][1] <
edgeLengthSq
[2][1])) ? ((
edgeLengthSq
[2][1] <
edgeLengthSq
[3][1]) ? 2 : 3) : ((!(
edgeLengthSq
[0][1] <
edgeLengthSq
[3][1])) ? 3 : 0));
127
det
[15][1] =
det
[13][0] *
Dot
(ref
edges
[num13][1], ref
y
[0]) +
det
[13][2] *
Dot
(ref
edges
[num13][1], ref
y
[2]) +
det
[13][3] *
Dot
(ref
edges
[num13][1], ref
y
[3]);
128
num13 = ((!(
edgeLengthSq
[0][2] <
edgeLengthSq
[1][2])) ? ((
edgeLengthSq
[1][2] <
edgeLengthSq
[3][2]) ? 1 : 3) : ((!(
edgeLengthSq
[0][2] <
edgeLengthSq
[3][2])) ? 3 : 0));
129
det
[15][2] =
det
[11][0] *
Dot
(ref
edges
[num13][2], ref
y
[0]) +
det
[11][1] *
Dot
(ref
edges
[num13][2], ref
y
[1]) +
det
[11][3] *
Dot
(ref
edges
[num13][2], ref
y
[3]);
130
num13 = ((!(
edgeLengthSq
[0][3] <
edgeLengthSq
[1][3])) ? ((
edgeLengthSq
[1][3] <
edgeLengthSq
[2][3]) ? 1 : 2) : ((!(
edgeLengthSq
[0][3] <
edgeLengthSq
[2][3])) ? 2 : 0));
131
det
[15][3] =
det
[7][0] *
Dot
(ref
edges
[num13][3], ref
y
[0]) +
det
[7][1] *
Dot
(ref
edges
[num13][3], ref
y
[1]) +
det
[7][2] *
Dot
(ref
edges
[num13][3], ref
y
[2]);
132
}
133
}
134
135
private
bool
UpdateSimplex
(
int
newIndex)
136
{
137
int
num =
simplexBits
| (1 << newIndex);
138
int
num2 = 1 << newIndex;
139
for
(
int
num3 =
simplexBits
; num3 != 0; num3--)
140
{
141
if
((num3 & num) == num3 &&
IsSatisfiesRule
(num3 | num2, num))
142
{
143
simplexBits
= num3 | num2;
144
closestPoint
=
ComputeClosestPoint
();
145
return
true
;
146
}
147
}
148
bool
result =
false
;
149
if
(
IsSatisfiesRule
(num2, num))
150
{
151
simplexBits
= num2;
152
closestPoint
=
y
[newIndex];
153
maxLengthSq
=
yLengthSq
[newIndex];
154
result =
true
;
155
}
156
return
result;
157
}
158
159
private
Vector3
ComputeClosestPoint
()
160
{
161
float
num = 0f;
162
Vector3
zero =
Vector3
.
Zero
;
163
maxLengthSq
= 0f;
164
for
(
int
num2 =
BitsToIndices
[
simplexBits
]; num2 != 0; num2 >>= 3)
165
{
166
int
num3 = (num2 & 7) - 1;
167
float
num4 =
det
[
simplexBits
][num3];
168
num += num4;
169
zero +=
y
[num3] * num4;
170
maxLengthSq
=
MathHelper
.
Max
(
maxLengthSq
,
yLengthSq
[num3]);
171
}
172
return
zero / num;
173
}
174
175
private
bool
IsSatisfiesRule
(
int
xBits,
int
yBits)
176
{
177
bool
result =
true
;
178
for
(
int
num =
BitsToIndices
[yBits]; num != 0; num >>= 3)
179
{
180
int
num2 = (num & 7) - 1;
181
int
num3 = 1 << num2;
182
if
((num3 & xBits) != 0)
183
{
184
if
(
det
[xBits][num2] <= 0f)
185
{
186
result =
false
;
187
break
;
188
}
189
}
190
else
if
(
det
[xBits | num3][num2] > 0f)
191
{
192
result =
false
;
193
break
;
194
}
195
}
196
return
result;
197
}
198
}
Microsoft.Xna.Framework.Gjk.simplexBits
int simplexBits
Definition
Gjk.cs:26
Microsoft.Xna.Framework.Gjk.IsSatisfiesRule
bool IsSatisfiesRule(int xBits, int yBits)
Definition
Gjk.cs:175
Microsoft.Xna.Framework.Gjk.Dot
static float Dot(ref Vector3 a, ref Vector3 b)
Definition
Gjk.cs:86
Microsoft.Xna.Framework.Gjk.y
Vector3[] y
Definition
Gjk.cs:16
Microsoft.Xna.Framework.Gjk.edges
Vector3[][] edges
Definition
Gjk.cs:20
Microsoft.Xna.Framework.Gjk.FullSimplex
bool FullSimplex
Definition
Gjk.cs:30
Microsoft.Xna.Framework.Gjk.Reset
void Reset()
Definition
Gjk.cs:61
Microsoft.Xna.Framework.Gjk.MaxLengthSquared
float MaxLengthSquared
Definition
Gjk.cs:32
Microsoft.Xna.Framework.Gjk.maxLengthSq
float maxLengthSq
Definition
Gjk.cs:28
Microsoft.Xna.Framework.Gjk.yLengthSq
float[] yLengthSq
Definition
Gjk.cs:18
Microsoft.Xna.Framework.Gjk.edgeLengthSq
float[][] edgeLengthSq
Definition
Gjk.cs:22
Microsoft.Xna.Framework.Gjk.closestPoint
Vector3 closestPoint
Definition
Gjk.cs:14
Microsoft.Xna.Framework.Gjk.ClosestPoint
Vector3 ClosestPoint
Definition
Gjk.cs:34
Microsoft.Xna.Framework.Gjk.AddSupportPoint
bool AddSupportPoint(ref Vector3 newPoint)
Definition
Gjk.cs:67
Microsoft.Xna.Framework.Gjk.UpdateDeterminant
void UpdateDeterminant(int xmIdx)
Definition
Gjk.cs:91
Microsoft.Xna.Framework.Gjk.det
float[][] det
Definition
Gjk.cs:24
Microsoft.Xna.Framework.Gjk.BitsToIndices
static int[] BitsToIndices
Definition
Gjk.cs:8
Microsoft.Xna.Framework.Gjk.ComputeClosestPoint
Vector3 ComputeClosestPoint()
Definition
Gjk.cs:159
Microsoft.Xna.Framework.Gjk.UpdateSimplex
bool UpdateSimplex(int newIndex)
Definition
Gjk.cs:135
Microsoft.Xna.Framework.Gjk.Gjk
Gjk()
Definition
Gjk.cs:36
Microsoft.Xna.Framework.Gjk
Definition
Gjk.cs:7
Microsoft.Xna.Framework.MathHelper.Max
static float Max(float value1, float value2)
Definition
MathHelper.cs:41
Microsoft.Xna.Framework.MathHelper
Definition
MathHelper.cs:6
Microsoft.Xna.Framework
Definition
AlphaTestEffect.cs:1
System.Data.IsolationLevel.Serializable
@ Serializable
System
Definition
BlockingCollection.cs:8
Microsoft.Xna.Framework.Vector3.Zero
static Vector3 Zero
Definition
Vector3.cs:44
Microsoft.Xna.Framework.Vector3.LengthSquared
float LengthSquared()
Definition
Vector3.cs:126
Microsoft.Xna.Framework.Vector3
Definition
Vector3.cs:12
source
Microsoft.Xna.Framework
Gjk.cs
Generated by
1.10.0