-
Notifications
You must be signed in to change notification settings - Fork 2
/
RayPhysics.cs
388 lines (330 loc) · 9.62 KB
/
RayPhysics.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.Collections.Generic;
using WarriorsSnuggery.Graphics;
using WarriorsSnuggery.Objects;
namespace WarriorsSnuggery.Physics
{
public class RayPhysics
{
static readonly CPos invalid = new CPos(int.MaxValue, int.MaxValue, 0);
public CPos Start;
public int StartHeight;
public CPos End { get; private set; }
public int EndHeight;
public CPos Target;
public int TargetHeight;
readonly CPos[][] mapBounds;
readonly List<MPos> positions = new List<MPos>();
readonly World world;
public RayPhysics(World world)
{
this.world = world;
var map = world.Map;
mapBounds = new[]
{
new [] { map.TopLeftCorner, map.TopRightCorner },
new [] { map.TopLeftCorner, map.BottomLeftCorner },
new [] { map.TopRightCorner, map.BottomRightCorner },
new [] { map.BottomLeftCorner, map.BottomRightCorner }
};
}
public void CalculateEnd(Actor[] ignore = null, bool ignoreActors = false)
{
var closestIntersect = new CPos(0, 0, int.MaxValue);
var closestT1 = double.MaxValue;
var sectors = new List<MPos>();
var diff = Target - Start;
var bounds = world.Map.Bounds;
positions.Clear();
var x0 = (int)Math.Round(Start.X / 1024.0);
var y0 = (int)Math.Round(Start.Y / 1024.0);
if (x0 >= 0 && y0 >= 0 && x0 < bounds.X && y0 < bounds.Y)
{
var sx = Math.Sign(diff.X);
var sy = Math.Sign(diff.Y);
var tMaxX = Math.Abs((x0 * 1024.0 - Start.X + 512 * sx) / diff.X);
var tMaxY = Math.Abs((y0 * 1024.0 - Start.Y + 512 * sy) / diff.Y);
var tDeltaX = Math.Abs(1024.0 / diff.X);
var tDeltaY = Math.Abs(1024.0 / diff.Y);
while (true)
{
var walls = new Wall[2];
walls[0] = world.WallLayer.Walls[x0 * 2, y0];
walls[1] = world.WallLayer.Walls[x0 * 2 + 1, y0];
var hit = false;
foreach (var wall in walls)
{
if (wall == null)
continue;
var lines = wall.Physics.GetLines();
foreach (var line in lines)
{
var end = getIntersection(line.Start, line.End, out var t1);
if (end != invalid && t1 < closestT1)
{
var height = calculateHeight(end);
if (height <= wall.Physics.Height + wall.Physics.HeightRadius || height >= wall.Physics.Height - wall.Physics.HeightRadius)
{
closestIntersect = end;
closestT1 = t1;
EndHeight = height;
hit = true;
}
}
}
}
// Add sectors that need to be checked for collision
var sector = new MPos(x0 / 2, y0 / 2);
if (!sectors.Contains(sector))
sectors.Add(sector);
// HACK: Break to prevent crash
if (positions.Contains(new MPos(x0, y0)))
break;
positions.Add(new MPos(x0, y0));
// We hit something, therefore we can ignore the rest which is further away
if (hit)
break;
if (tMaxX < tMaxY)
{
tMaxX += tDeltaX;
x0 += sx;
}
else
{
tMaxY += tDeltaY;
y0 += sy;
}
// Map edges as exit conditions
if (x0 < 0)
{
if (sx < 0) break;
continue;
}
if (x0 >= bounds.X)
{
if (sx > 0) break;
continue;
}
if (y0 < 0)
{
if (sy < 0) break;
continue;
}
if (y0 >= bounds.Y)
{
if (sy > 0) break;
continue;
}
}
}
// Collision at actors
if (!ignoreActors)
{
foreach (var sectorPos in sectors)
{
if (sectorPos.X < 0 || sectorPos.Y < 0 || sectorPos.X >= world.Map.Bounds.X / PhysicsLayer.SectorSize || sectorPos.Y >= world.Map.Bounds.Y / PhysicsLayer.SectorSize)
continue;
var sector = world.PhysicsLayer.Sectors[sectorPos.X, sectorPos.Y];
var objs = sector.GetObjects(ignore);
var hit = false;
foreach (var obj in objs)
{
if (obj.Physics.Shape == Shape.CIRCLE)
{
var end = getIntersection(obj.Physics.Position, obj.Physics.RadiusX, out var t1, out var t2, out var end2);
if (end != invalid && t1 < closestT1)
{
var height = calculateHeight(end);
if (height <= obj.Physics.Height + obj.Physics.HeightRadius && height >= obj.Physics.Height - obj.Physics.HeightRadius)
{
closestIntersect = end;
closestT1 = t1;
EndHeight = height;
hit = true;
}
else if (t2 < closestT1)
{
height = calculateHeight(end2);
if (height <= obj.Physics.Height + obj.Physics.HeightRadius && height >= obj.Physics.Height - obj.Physics.HeightRadius)
{
closestIntersect = end2;
closestT1 = t2;
EndHeight = height;
hit = true;
}
}
}
}
else
{
foreach (var line in obj.Physics.GetLines())
{
var end = getIntersection(line.Start, line.End, out var t1);
if (end != invalid && t1 < closestT1)
{
var height = calculateHeight(end);
if (height <= obj.Physics.Height + obj.Physics.HeightRadius && height >= obj.Physics.Height - obj.Physics.HeightRadius)
{
closestIntersect = end;
closestT1 = t1;
EndHeight = height;
hit = true;
}
}
}
}
}
// If we hit something, we won't need to check any sectors behind
if (hit)
break;
}
}
// Collision at map bounds, if nothing was hit
if (closestIntersect == new CPos(0, 0, int.MaxValue))
{
foreach (var line in mapBounds)
{
var end = getIntersection(line[0], line[1], out var t1);
if (end != invalid && t1 < closestT1)
{
closestIntersect = end;
closestT1 = t1;
}
}
}
End = closestIntersect;
}
public float GetWallPenetrationValue()
{
var walls = new List<Wall>();
var diff = Target - Start;
var bounds = world.Map.Bounds;
var x0 = (int)Math.Round(Start.X / 1024.0);
var y0 = (int)Math.Round(Start.Y / 1024.0);
if (x0 < 0 || y0 < 0 || x0 > world.Map.Bounds.X || y0 > world.Map.Bounds.Y)
return 1f;
var sx = Math.Sign(diff.X);
var sy = Math.Sign(diff.Y);
var tMaxX = Math.Abs((x0 * 1024.0 - Start.X + 512 * sx) / diff.X);
var tMaxY = Math.Abs((y0 * 1024.0 - Start.Y + 512 * sy) / diff.Y);
var tDeltaX = Math.Abs(1024.0 / diff.X);
var tDeltaY = Math.Abs(1024.0 / diff.Y);
bool run = true;
while (run)
{
var walls2 = new Wall[2];
walls2[0] = world.WallLayer.Walls[x0 * 2, y0];
walls2[1] = world.WallLayer.Walls[x0 * 2 + 1, y0];
foreach (var wall in walls2)
{
if (!run)
break;
if (wall == null)
continue;
var lines = wall.Physics.GetLines();
foreach (var line in lines)
{
var end = getIntersection(line.Start, line.End, out var _);
if (end != invalid && (Start - end).Dist <= (Start - Target).Dist)
{
walls.Add(wall);
// Stop if damage does not get penetrated anyway
if (wall.Type.DamagePenetration == 0f)
{
run = false;
break;
}
}
}
}
if (tMaxX < tMaxY)
{
tMaxX += tDeltaX;
x0 += sx;
}
else
{
tMaxY += tDeltaY;
y0 += sy;
}
// Map edges as exit conditions
if (x0 < 0)
{
if (sx < 0) break;
continue;
}
if (x0 >= bounds.X)
{
if (sx > 0) break;
continue;
}
if (y0 < 0)
{
if (sy < 0) break;
continue;
}
if (y0 >= bounds.Y)
{
if (sy > 0) break;
continue;
}
}
var output = 1f;
foreach (var wall in walls)
output *= wall.Type.DamagePenetration;
return output;
}
CPos getIntersection(CPos a1, CPos a2, out double T1)
{
T1 = double.MaxValue;
var pos1 = Start;
var delta1 = Target - Start;
var pos2 = a1;
var delta2 = a2 - a1;
var combined_dist = Math.Sqrt(delta1.X * (double)delta1.X + delta1.Y * (double)delta1.Y) * Math.Sqrt(delta2.X * (double)delta2.X + delta2.Y * (double)delta2.Y);
var skalar_product = delta1.X * delta2.X + delta1.Y * delta2.Y;
if (skalar_product / combined_dist == 1)
return invalid;
var T2 = (delta1.X * (pos2.Y - pos1.Y) + delta1.Y * (pos1.X - pos2.X)) / (double)(delta2.X * delta1.Y - delta2.Y * delta1.X);
T1 = (pos2.X + delta2.X * T2 - pos1.X) / (delta1.X == 0 ? 1 : delta1.X);
// Must be within parametic whatevers for RAY/SEGMENT
if (T1 < 0 || double.IsNaN(T1))
return invalid;
if (T2 < 0 || T2 > 1)
return invalid;
// Return the POINT OF INTERSECTION
return new CPos(pos1.X + (int)(delta1.X * T1), pos1.Y + (int)(delta1.Y * T1), 0);
}
CPos getIntersection(CPos center, int radius, out double T1, out double T2, out CPos intersect2)
{
T1 = double.MaxValue;
T2 = double.MaxValue;
intersect2 = invalid;
var delta = Target - Start;
var dist = center - Start;
double a = (delta.X * delta.X) + (delta.Y * delta.Y);
double b = 2 * ((delta.X * dist.X) + (delta.Y * dist.Y));
double c = (dist.X * dist.X) + (dist.Y * dist.Y) - radius * radius;
var discriminant = b * b - 4 * a * c;
if (discriminant < 0)
return invalid;
// smallest one is the nearest
T1 = Math.Abs(-b + Math.Sqrt(discriminant)) / (2 * a);
T2 = Math.Abs(-b - Math.Sqrt(discriminant)) / (2 * a);
// Return the POINT OF INTERSECTION
intersect2 = new CPos(Start.X + (int)(delta.X * T2), Start.Y + (int)(delta.Y * T2), 0);
return new CPos(Start.X + (int)(delta.X * T1), Start.Y + (int)(delta.Y * T1), 0);
}
int calculateHeight(CPos pos)
{
var diff = (Start - pos).FlatDist/(Start - Target).FlatDist;
var hDiff = StartHeight - TargetHeight;
return StartHeight - (int)(diff * hDiff);
}
public void RenderDebug()
{
foreach (var pos in positions)
ColorManager.DrawQuad(pos.ToCPos(), 1012, new Color(0, 255, 0, 64));
}
}
}