/
PhysicsWorld.hx
352 lines (295 loc) · 9.65 KB
/
PhysicsWorld.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
package armory.trait.physics.bullet;
#if arm_bullet
import iron.Trait;
import iron.system.Time;
import iron.math.Vec4;
import iron.math.RayCaster;
import iron.data.SceneFormat;
class ContactPair {
public var a:Int;
public var b:Int;
public var posA:Vec4;
public var posB:Vec4;
public var normOnB:Vec4;
public var impulse:Float;
public var distance:Float;
public function new(a:Int, b:Int) {
this.a = a;
this.b = b;
}
}
class PhysicsWorld extends Trait {
public static var active:PhysicsWorld = null;
static var sceneRemoved = false;
#if arm_physics_soft
public var world:bullet.Bt.SoftRigidDynamicsWorld;
#else
public var world:bullet.Bt.DiscreteDynamicsWorld;
#end
var dispatcher:bullet.Bt.CollisionDispatcher;
var gimpactRegistered = false;
var contacts:Array<ContactPair>;
var preUpdates:Array<Void->Void> = null;
public var rbMap:Map<Int, RigidBody>;
public var timeScale = 1.0;
var timeStep = 1 / 60;
var maxSteps = 1;
public var solverIterations = 10;
public var hitPointWorld = new Vec4();
public var hitNormalWorld = new Vec4();
var pairCache:Bool = false;
static var nullvec = true;
var vec1:bullet.Bt.Vector3 = null;
var vec2:bullet.Bt.Vector3 = null;
#if arm_debug
public static var physTime = 0.0;
#end
public function new(timeScale = 1.0, timeStep = 1 / 60, solverIterations = 10) {
super();
if (nullvec) {
nullvec = false;
vec1 = new bullet.Bt.Vector3(0, 0, 0);
vec2 = new bullet.Bt.Vector3(0, 0, 0);
}
// Scene spawn
if (active != null && !sceneRemoved) return;
sceneRemoved = false;
this.timeScale = timeScale;
this.timeStep = timeStep;
maxSteps = timeStep < 1 / 60 ? 10 : 1;
this.solverIterations = solverIterations;
// First scene
if (active == null) {
createPhysics();
}
// Scene switch
else {
world = active.world;
dispatcher = active.dispatcher;
gimpactRegistered = active.gimpactRegistered;
}
contacts = [];
rbMap = new Map();
active = this;
// Ensure physics are updated first in the lateUpdate list
_lateUpdate = [lateUpdate];
@:privateAccess iron.App.traitLateUpdates.insert(0, lateUpdate);
iron.Scene.active.notifyOnRemove(function() {
sceneRemoved = true;
});
}
public function reset() {
for (rb in active.rbMap) removeRigidBody(rb);
}
function createPhysics() {
var broadphase = new bullet.Bt.DbvtBroadphase();
#if arm_physics_soft
var collisionConfiguration = new bullet.Bt.SoftBodyRigidBodyCollisionConfiguration();
#else
var collisionConfiguration = new bullet.Bt.DefaultCollisionConfiguration();
#end
dispatcher = new bullet.Bt.CollisionDispatcher(collisionConfiguration);
var solver = new bullet.Bt.SequentialImpulseConstraintSolver();
var g = iron.Scene.active.raw.gravity;
var gravity = g == null ? new Vec4(0, 0, -9.81) : new Vec4(g[0], g[1], g[2]);
#if arm_physics_soft
var softSolver = new bullet.Bt.DefaultSoftBodySolver();
world = new bullet.Bt.SoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration, softSolver);
vec1.setX(gravity.x);
vec1.setY(gravity.y);
vec1.setZ(gravity.z);
#if js
world.getWorldInfo().set_m_gravity(vec1);
#else
world.getWorldInfo().m_gravity = vec1;
#end
#else
world = new bullet.Bt.DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
#end
setGravity(gravity);
}
public function setGravity(v:Vec4) {
vec1.setValue(v.x, v.y, v.z);
world.setGravity(vec1);
}
public function addRigidBody(body:RigidBody) {
#if js
world.addRigidBodyToGroup(body.body, body.group, body.group);
#else
world.addRigidBody(body.body, body.group, body.group);
#end
rbMap.set(body.id, body);
}
public function removeRigidBody(body:RigidBody) {
if (body.destroyed) return;
body.destroyed = true;
if (world != null) world.removeRigidBody(body.body);
rbMap.remove(body.id);
body.delete();
}
// public function addKinematicCharacterController(controller:KinematicCharacterController) {
// if (!pairCache){ // Only create PairCache if needed
// world.getPairCache().setInternalGhostPairCallback(BtGhostPairCallbackPointer.create());
// pairCache = true;
// }
// world.addAction(controller.character);
// world.addCollisionObjectToGroup(controller.body, controller.group, controller.group);
// }
// public function removeKinematicCharacterController(controller:KinematicCharacterController) {
// if (world != null) {
// world.removeCollisionObject(controller.body);
// world.removeAction(controller.character);
// }
// #if js
// bullet.Bt.Ammo.destroy(controller.body);
// #else
// var cbody = controller.body;
// untyped __cpp__("delete cbody");
// #end
// }
/**
* Used to get intersecting rigid bodies with the passed in RigidBody as reference. Often used when checking for object collisions.
*
* @param body The passed in RigidBody to be checked for intersecting rigid bodies.
* @return Array<RigidBody> or null.
*/
public function getContacts(body:RigidBody):Array<RigidBody> {
if (contacts.length == 0) return null;
var res:Array<RigidBody> = [];
for (i in 0...contacts.length) {
var c = contacts[i];
var rb = null;
#if js
if (c.a == untyped body.body.userIndex) rb = rbMap.get(c.b);
else if (c.b == untyped body.body.userIndex) rb = rbMap.get(c.a);
#else
var ob:bullet.Bt.CollisionObject = body.body;
if (c.a == ob.getUserIndex()) rb = rbMap.get(c.b);
else if (c.b == ob.getUserIndex()) rb = rbMap.get(c.a);
#end
if (rb != null && res.indexOf(rb) == -1) res.push(rb);
}
return res;
}
public function getContactPairs(body:RigidBody):Array<ContactPair> {
if (contacts.length == 0) return null;
var res:Array<ContactPair> = [];
for (i in 0...contacts.length) {
var c = contacts[i];
#if js
if (c.a == untyped body.body.userIndex) res.push(c);
else if (c.b == untyped body.body.userIndex) res.push(c);
#else
var ob:bullet.Bt.CollisionObject = body.body;
if (c.a == ob.getUserIndex()) res.push(c);
else if (c.b == ob.getUserIndex()) res.push(c);
#end
}
return res;
}
function lateUpdate() {
var t = Time.delta * timeScale;
if (t == 0.0) return; // Simulation paused
#if arm_debug
var startTime = kha.Scheduler.realTime();
#end
if (preUpdates != null) for (f in preUpdates) f();
world.stepSimulation(timeStep, maxSteps, t);
updateContacts();
for (rb in rbMap) @:privateAccess rb.physicsUpdate();
#if arm_debug
physTime = kha.Scheduler.realTime() - startTime;
#end
}
function updateContacts() {
contacts = [];
var disp:bullet.Bt.Dispatcher = dispatcher;
var numManifolds = disp.getNumManifolds();
for (i in 0...numManifolds) {
var contactManifold = disp.getManifoldByIndexInternal(i);
#if js
var body0 = untyped bullet.Bt.Ammo.btRigidBody.prototype.upcast(contactManifold.getBody0());
var body1 = untyped bullet.Bt.Ammo.btRigidBody.prototype.upcast(contactManifold.getBody1());
#else
var body0:bullet.Bt.CollisionObject = contactManifold.getBody0();
var body1:bullet.Bt.CollisionObject = contactManifold.getBody1();
#end
var numContacts = contactManifold.getNumContacts();
var pt:bullet.Bt.ManifoldPoint = null;
var posA:bullet.Bt.Vector3 = null;
var posB:bullet.Bt.Vector3 = null;
var nor:bullet.Bt.Vector3 = null;
var cp:ContactPair = null;
for (j in 0...numContacts) {
pt = contactManifold.getContactPoint(j);
#if js
posA = pt.get_m_positionWorldOnA();
posB = pt.get_m_positionWorldOnB();
nor = pt.get_m_normalWorldOnB();
cp = new ContactPair(untyped body0.userIndex, untyped body1.userIndex);
#else
posA = pt.m_positionWorldOnA;
posB = pt.m_positionWorldOnB;
nor = pt.m_normalWorldOnB;
cp = new ContactPair(body0.getUserIndex(), body1.getUserIndex());
#end
cp.posA = new Vec4(posA.x(), posA.y(), posA.z());
cp.posB = new Vec4(posB.x(), posB.y(), posB.z());
cp.normOnB = new Vec4(nor.x(), nor.y(), nor.z());
cp.impulse = pt.getAppliedImpulse();
cp.distance = pt.getDistance();
contacts.push(cp);
}
}
}
public function pickClosest(inputX:Float, inputY:Float):RigidBody {
var camera = iron.Scene.active.camera;
var start = new Vec4();
var end = new Vec4();
RayCaster.getDirection(start, end, inputX, inputY, camera);
return rayCast(camera.transform.world.getLoc(), end);
}
public function rayCast(from:Vec4, to:Vec4):RigidBody {
var rayFrom = vec1;
var rayTo = vec2;
rayFrom.setValue(from.x, from.y, from.z);
rayTo.setValue(to.x, to.y, to.z);
var rayCallback = new bullet.Bt.ClosestRayResultCallback(rayFrom, rayTo);
var worldDyn:bullet.Bt.DynamicsWorld = world;
var worldCol:bullet.Bt.CollisionWorld = worldDyn;
worldCol.rayTest(rayFrom, rayTo, rayCallback);
var rb:RigidBody = null;
var rc:bullet.Bt.RayResultCallback = rayCallback;
if (rc.hasHit()) {
#if js
var co = rayCallback.get_m_collisionObject();
var body = untyped bullet.Bt.Ammo.btRigidBody.prototype.upcast(co);
var hit = rayCallback.get_m_hitPointWorld();
hitPointWorld.set(hit.x(), hit.y(), hit.z());
var norm = rayCallback.get_m_hitNormalWorld();
hitNormalWorld.set(norm.x(), norm.y(), norm.z());
rb = rbMap.get(untyped body.userIndex);
#elseif cpp
var hit = rayCallback.m_hitPointWorld;
hitPointWorld.set(hit.x(), hit.y(), hit.z());
var norm = rayCallback.m_hitNormalWorld;
hitNormalWorld.set(norm.x(), norm.y(), norm.z());
rb = rbMap.get(rayCallback.m_collisionObject.getUserIndex());
#end
}
#if js
bullet.Bt.Ammo.destroy(rayCallback);
#else
rayCallback.delete();
#end
return rb;
}
public function notifyOnPreUpdate(f:Void->Void) {
if (preUpdates == null) preUpdates = [];
preUpdates.push(f);
}
public function removePreUpdate(f:Void->Void) {
preUpdates.remove(f);
}
}
#end