-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhysicsManager.hx
413 lines (365 loc) · 11.8 KB
/
PhysicsManager.hx
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package;
import flash.display.Sprite;
import flash.geom.Point;
/**
* ...
* @author Henry D. Fernández B.
*/
class PhysicsManager
{
//private var quadtree : Quadtree;
//private var gameObjects : Array<PhysicsObject>;
public function new()
{
//quadtree = new Quadtree(0, new flash.geom.Rectangle(0, 0, GraphicManager.GetWidth(), GraphicManager.GetHeight()),null);
//gameObjects = new Array<PhysicsObject>();
}
/*public function AddGameObject(gameObject : PhysicsObject) : Void
{
//gameObjects.push(gameObject);
quadtree.Insert(gameObject);
}
public function RemoveGameObject(gameObject : PhysicsObject) : Void
{
gameObjects.remove(gameObject);
}*/
public function Update(gameTime : Float) : Void
{
}
/*public function UpdateQuadElement(element : PhysicsObject) : Void
{
//TODO: check if it was moved from the previous quad
//If was moved: Remove and Insert again
//Else: do nothing
}*/
public function HandleCircleCircleCollision(c1 : Circle, c2 : Circle) : Float
{
var mod,p,rad,pX,pY,nX,nY : Float;
rad = c1.GetRadius() + c2.GetRadius();
mod = Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2));
p = mod - rad;
nX = (c1.x - c2.x) / mod;
nY = (c1.y - c2.y) / mod;
pX = nX * Math.abs(p);
pY = nY * Math.abs(p);
return p;
}
public function GetCollisionNormal(currentCircle : Circle,futureCircle : Circle, rectangle : Rectangle) : Normal
{
var cx, cy, cr, rx, ry, rw, rh : Float;
var value,cTransformed : Point;
var ccx, ccy, ccr : Float;
var transformedCurrentCircle : Point;
var normal : Normal;
var dir : Normal.NormalDirection;
//cTransformed = TransformCoordinates(futureCircle, rectangle);
cTransformed = TransformCoordinates(currentCircle, rectangle);
value = new Point();
dir = Normal.NormalDirection.Down;
cx = cTransformed.x;
cy = cTransformed.y;
cr = futureCircle.GetRadius();
rx = rectangle.GetPosition().x;
ry = rectangle.GetPosition().y;
rw = rectangle.GetNonRotatedWidth();
rh = rectangle.GetNonRotatedHeight();
transformedCurrentCircle = TransformCoordinates(currentCircle, rectangle);
ccx = transformedCurrentCircle.x;
ccy = transformedCurrentCircle.y;
ccr = currentCircle.GetRadius();
//inside the rectangle (x axis)
if (cx - cr <= rx + rw/2 && cx + cr >= rx - rw/2)
{
//Up
if (cy < ry)
{
value = rectangle.GetNormalUp();
dir = Normal.NormalDirection.Up;
}
//Down
else if (cy > ry)
{
value = rectangle.GetNormalDown();
dir = Normal.NormalDirection.Down;
}
}
//inside the rectangle (y axis)
else if (cy - cr <= ry + rh/2 && cy + cr >= ry - rh/2)
{
//Left
if (cx < rx)
{
value = rectangle.GetNormalLeft();
dir = Normal.NormalDirection.Left;
}
//Right
else if (cx > rx)
{
value = rectangle.GetNormalRight();
dir = Normal.NormalDirection.Right;
}
}
//Corners
else
{
//Up
if (cy < ry)
{
//Left
if (cx < rx)
{
value = rectangle.GetNormalUpLeft();
dir = Normal.NormalDirection.UpLeft;
}
//Right
else if (cx > rx)
{
value = rectangle.GetNormalUpRight();
dir = Normal.NormalDirection.UpRight;
}
}
//Down
else if (cy > ry)
{
//Left
if (cx < rx)
{
value = rectangle.GetNormalDownLeft();
dir = Normal.NormalDirection.DownLeft;
}
//Right
else if (cx > rx)
{
value = rectangle.GetNormalDownRight();
dir = Normal.NormalDirection.DownRight;
}
}
}
normal = new Normal(value, dir);
return normal;
}
public function HandleCircleOBBCollision(currentCircle : Circle,futureCircle : Circle,rectangle : Rectangle, normal : Normal) : CollisionData
{
var rectW, rectH, currCircR,futuCircR, distance : Float;
var currCircPos, futuCircPos, rectPos, newPos, futuCircDiffRect, futuCircRectClamp, futuCircCollision, currCircDiffRect, currCircRectClamp : Point;
var collisionData : CollisionData;
//Initial values
rectPos = rectangle.GetPosition();
futuCircR = futureCircle.GetRadius();
currCircR = futureCircle.GetRadius();
newPos = null;
collisionData = null;
//Rectangle real size: width and height
rectW = rectangle.GetNonRotatedWidth();
rectH = rectangle.GetNonRotatedHeight();
futuCircPos = TransformCoordinates(futureCircle, rectangle);
currCircPos = TransformCoordinates(currentCircle, rectangle);
//Position difference between circle and rectangle
currCircDiffRect = new Point(currCircPos.x - rectPos.x, currCircPos.y - rectPos.y);
futuCircDiffRect = new Point(futuCircPos.x - rectPos.x, futuCircPos.y - rectPos.y);
//Clamping vector to rectangle dimensions
currCircRectClamp = MathHelper.Clamp(currCircDiffRect, new Point(rectW/2, rectH/2));
futuCircRectClamp = MathHelper.Clamp(futuCircDiffRect, new Point(rectW/2, rectH/2));
//Collision vector
futuCircCollision = new Point(futuCircDiffRect.x - futuCircRectClamp.x, futuCircDiffRect.y - futuCircRectClamp.y);
//Collision distance
distance = futuCircCollision.length - futuCircR;
if (distance < 0)
{
/*//Inside X boundaries
if (futuCircPos.x + futuCircR >= rectPos.x - rectW / 2 && futuCircPos.x - futuCircR <= rectPos.x + rectW / 2)
{*/
//Up collision
if (currCircPos.y < rectPos.y)
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y - rectH / 2 - futuCircR;
}
//Down collision
else
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y + rectH / 2 + futuCircR;
}
//}
//Inside Y boundaries
/*else
if (futuCircPos.y + futuCircR >= rectPos.y - rectH / 2 && futuCircPos.y - futuCircR <= rectPos.y + rectH / 2)
{*/
//Left collision
if (futuCircPos.x < rectPos.x)
{
newPos = new Point();
newPos.x = rectPos.x - rectW/2 - futuCircR;
newPos.y = futuCircPos.y;
}
//Right collision
else
{
newPos = new Point();
newPos.x = rectPos.x + rectW/2 + futuCircR;
newPos.y = futuCircPos.y;
}
/*}
else
{
trace("fuera y");
}*/
newPos = MathHelper.TransformPoint(newPos, rectPos, MathHelper.ConvertDegToRad(rectangle.GetRotationAngle()));
collisionData = new CollisionData(Math.abs(distance),newPos,normal);
}
return collisionData;
}
public function HandleCircleOBBCollision2(currentCircle : Circle,futureCircle : Circle,rectangle : Rectangle, normal : Normal) : CollisionData
{
var rectW, rectH, currCircR,futuCircR, distance : Float;
var currCircPos, futuCircPos, rectPos, newPos, futuCircDiffRect, futuCircRectClamp, futuCircCollision, currCircDiffRect, currCircRectClamp : Point;
var collisionData : CollisionData;
//Initial values
rectPos = rectangle.GetPosition();
futuCircR = futureCircle.GetRadius();
currCircR = futureCircle.GetRadius();
newPos = null;
collisionData = null;
//Rectangle real size: width and height
rectW = rectangle.GetNonRotatedWidth();
rectH = rectangle.GetNonRotatedHeight();
futuCircPos = TransformCoordinates(futureCircle, rectangle);
currCircPos = TransformCoordinates(currentCircle, rectangle);
//Position difference between circle and rectangle
currCircDiffRect = new Point(currCircPos.x - rectPos.x, currCircPos.y - rectPos.y);
futuCircDiffRect = new Point(futuCircPos.x - rectPos.x, futuCircPos.y - rectPos.y);
//Clamping vector to rectangle dimensions
currCircRectClamp = MathHelper.Clamp(currCircDiffRect, new Point(rectW/2, rectH/2));
futuCircRectClamp = MathHelper.Clamp(futuCircDiffRect, new Point(rectW/2, rectH/2));
//Collision vector
futuCircCollision = new Point(futuCircDiffRect.x - futuCircRectClamp.x, futuCircDiffRect.y - futuCircRectClamp.y);
//Collision distance
distance = futuCircCollision.length - futuCircR;
var dx, dy : Float;
dx = Math.abs(rectPos.x - futuCircPos.x) - (rectW + futuCircR * 2) / 2;
dy = Math.abs(rectPos.y - futuCircPos.y) - (rectH + futuCircR * 2) / 2;
if (dx < 0 && dy < 0)
{
//Inside X boundaries
if (currCircPos.y + currCircR < rectPos.y - rectH/2 || currCircPos.y - currCircR > rectPos.y + rectH/2)
{
if (futuCircPos.x + futuCircR >= rectPos.x - rectW / 2 && futuCircPos.x - futuCircR <= rectPos.x + rectW / 2)
{
//Up collision
if (currCircPos.y < rectPos.y)
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y - (rectH / 2) - currCircR;
}
//Down collision
else
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y + (rectH / 2) + currCircR;
}
}
}
//Inside Y boundaries
else if (currCircPos.x + currCircR < rectPos.x - rectW/2 || currCircPos.x - currCircR > rectPos.x + rectW/2)
{
if (futuCircPos.y + futuCircR >= rectPos.y - rectH / 2 && futuCircPos.y - futuCircR <= rectPos.y + rectH / 2)
{
//Left collision
if (futuCircPos.x < rectPos.x)
{
newPos = new Point();
newPos.x = rectPos.x - rectW/2 - futuCircR;
newPos.y = futuCircPos.y;
}
//Right collision
else
{
newPos = new Point();
newPos.x = rectPos.x + rectW/2 + futuCircR;
newPos.y = futuCircPos.y;
}
}
}
else
{
if (futuCircPos.x + futuCircR >= rectPos.x - rectW / 2 && futuCircPos.x - futuCircR <= rectPos.x + rectW / 2)
{
//Up collision
if (currCircPos.y < rectPos.y)
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y - (rectH / 2) - currCircR;
}
//Down collision
else
{
newPos = new Point();
newPos.x = futuCircPos.x;
newPos.y = rectPos.y + (rectH / 2) + currCircR;
}
}
else if (futuCircPos.y + futuCircR >= rectPos.y - rectH / 2 && futuCircPos.y - futuCircR <= rectPos.y + rectH / 2)
{
//Left collision
if (futuCircPos.x < rectPos.x)
{
newPos = new Point();
newPos.x = rectPos.x - rectW/2 - futuCircR;
newPos.y = futuCircPos.y;
}
//Right collision
else
{
newPos = new Point();
newPos.x = rectPos.x + rectW/2 + futuCircR;
newPos.y = futuCircPos.y;
}
}
}
newPos = MathHelper.TransformPoint(newPos, rectPos, MathHelper.ConvertDegToRad(rectangle.GetRotationAngle()));
collisionData = new CollisionData(Math.abs(distance),newPos,normal);
}
return collisionData;
}
public function HandleAABBCollision(r1 : Rectangle, r2 : Rectangle) : Bool
{
var dx,dy : Float;
dx = Math.abs(r1.GetPosition().x - r2.GetPosition().x) - (r1.GetNonRotatedWidth() + r2.GetNonRotatedWidth()) / 2;
dy = Math.abs(r1.GetPosition().y - r2.GetPosition().y) - (r1.GetNonRotatedHeight() + r2.GetNonRotatedHeight()) / 2;
return (dx < 0 && dy < 0);
}
public function HandleAABBCircleCollision(r : Rectangle, c : Circle) : Bool
{
var dx, dy : Float;
dx = Math.abs(r.GetPosition().x - c.GetPosition().x) - (r.GetNonRotatedWidth() + c.GetRadius() * 2) / 2;
dy = Math.abs(r.GetPosition().y - c.GetPosition().y) - (r.GetNonRotatedHeight() + c.GetRadius() * 2) / 2;
return dx < 0 && dy < 0;
}
public function Draw(container : Sprite) : Void
{
//quadtree.Draw(container);
}
/*
* Transform the circle coordinates compared to the rotated rectangle.
* This function is used to perform physics collision between a rotated rectangle and a circle.
*
* */
public static function TransformCoordinates(circle : Circle,rectangle : Rectangle) : Point
{
var newX, newY, angle, cx,cy,rx,ry : Float;
cx = circle.GetPosition().x;
cy = circle.GetPosition().y;
rx = rectangle.GetPosition().x;
ry = rectangle.GetPosition().y;
angle = rectangle.GetRotationAngle();
newX = Math.cos(MathHelper.ConvertDegToRad(-angle)) * (cx - rx) - Math.sin(MathHelper.ConvertDegToRad(-angle)) * (cy - ry) + rx;
newY = Math.sin(MathHelper.ConvertDegToRad(-angle)) * (cx - rx) + Math.cos(MathHelper.ConvertDegToRad(-angle)) * (cy - ry) + ry;
return new Point(newX,newY);
}
}