-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathphysicsBody.ts
More file actions
372 lines (322 loc) · 11.9 KB
/
Copy pathphysicsBody.ts
File metadata and controls
372 lines (322 loc) · 11.9 KB
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
/**
* @author Yannick Deubel (https://github.com/yandeu)
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|LGPL-3.0}
*/
import { ExtendedObject3D, XYZ } from './types.js'
import { Events } from '@yandeu/events'
import { Euler, Quaternion } from 'three'
import { CollisionEvent } from './types.js'
class PhysicsBody {
public ptr: any | undefined
public body: any | undefined
public ignoreScale = false
public isSoftBody = false
public offset = { x: 0, y: 0, z: 0 }
public name: string
public errors: string[] = []
public checkCollisions = false
public impact: { impulse: number; point: XYZ; normal: XYZ; name: string }[] = []
public breakable = false
public fractureImpulse = 1
public didUpdate = false
/** Skip syncing the mesh to the physics body. */
public skipUpdate = false
private _emitUpdateEvents = false
private _needUpdate = false
private tmpEuler = new Euler()
private tmpQuaternion = new Quaternion()
private tmpBtVector3 = new Ammo.btVector3()
private tmpBtVector3_1 = new Ammo.btVector3()
private tmpBtQuaternion = new Ammo.btQuaternion(0, 0, 0, 1)
private eventEmitter = new Events()
constructor(
private physics: any,
public ammo: Ammo.btRigidBody
) {
// @ts-expect-error: Ammo.btRigidBody does not have a property name.
this.name = ammo.name
}
/**
* Don't call this manually! Do physics.destroy() instead.
*/
public destructor() {
// remove events
if (this.eventEmitter) this.eventEmitter.removeAllListeners()
// destroy tmp ammo values
Ammo.destroy(this.tmpBtVector3)
Ammo.destroy(this.tmpBtVector3_1)
Ammo.destroy(this.tmpBtQuaternion)
// destory collision shape
Ammo.destroy(this.ammo.getCollisionShape())
// destroy body
Ammo.destroy(this.ammo)
}
private setupEventEmitter() {
if (typeof this.eventEmitter === 'undefined') this.eventEmitter = new Events()
}
public get needUpdate() {
return this._needUpdate
}
public set needUpdate(need: boolean) {
if (!need && this._needUpdate) this.didUpdate = true
this._needUpdate = need
}
private onUpdateEvent(updateCallback: Function, once: boolean = false) {
this.setupEventEmitter()
this._emitUpdateEvents = true
if (once)
this.eventEmitter.once('update', () => {
updateCallback()
})
else
this.eventEmitter.on('update', () => {
updateCallback()
})
}
public get on() {
return {
update: (updateCallback: Function) => this.onUpdateEvent(updateCallback),
collision: (collisionCallback: (otherObject: ExtendedObject3D, event: CollisionEvent) => void) =>
this.onCollision(collisionCallback)
}
}
public get once() {
return {
update: (updateCallback: Function) => this.onUpdateEvent(updateCallback, true)
}
}
private onCollision(collisionCallback: (otherObject: ExtendedObject3D, event: CollisionEvent) => void) {
this.checkCollisions = true
this.physics.collisionEvents.on('collision', (data: { bodies: ExtendedObject3D[]; event: CollisionEvent }) => {
const { bodies, event } = data
if (bodies[0].name === this.name) collisionCallback(bodies[1], event)
else if (bodies[1].name === this.name) collisionCallback(bodies[0], event)
})
}
/** You have to call transform() before you can get or set the body's position or rotation. (for headless mode only) */
public transform() {
const t = this.physics.worldTransform
this.ammo.getMotionState().getWorldTransform(t)
}
/** You have to call refresh() after you set the position or rotation of the body. (for headless mode only) */
public refresh() {
const t = this.physics.worldTransform
this.ammo.getMotionState().setWorldTransform(t)
}
/** Set the rotation in radians. (for headless mode only) */
public setRotation(x: number, y: number, z: number) {
const e = this.tmpEuler.set(x, y, z)
const q = this.tmpQuaternion.set(0, 0, 0, 1)
q.setFromEuler(e)
this.tmpBtQuaternion.setValue(0, 0, 0, 1)
const ammoQuat = this.tmpBtQuaternion
ammoQuat.setValue(q.x, q.y, q.z, q.w)
const t = this.physics.worldTransform
t.setRotation(ammoQuat)
}
/** Get the rotation in radians. (for headless mode only) */
public get rotation() {
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
let x, y, z
const t = this.physics.worldTransform
const ammoQuat = t.getRotation()
let q1 = this.tmpQuaternion.set(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w())
if (q1.w > 1) q1 = q1.normalize() // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalized
const angle = 2 * Math.acos(q1.w)
const s = Math.sqrt(1 - q1.w * q1.w) // assuming quaternion normalized then w is less than 1, so term always positive.
if (s < 0.001) {
// test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
x = q1.x // if it is important that axis is normalized then replace with x=1; y=z=0;
y = q1.y
z = q1.z
} else {
x = q1.x / s // normalized axis
y = q1.y / s
z = q1.z / s
}
return { x: x * angle, y: y * angle, z: z * angle }
}
/** Get the quaternion. (for headless mode only) */
public get quaternion(): { x: number; y: number; z: number; w: number } {
const t = this.physics.worldTransform
const q = t.getRotation()
return { x: q.x(), y: q.y(), z: q.z(), w: q.w() }
}
/** Set position. (for headless mode only) */
public setPosition(x: number, y: number, z: number) {
const t = this.physics.worldTransform
t.getOrigin().setValue(x, y, z)
}
/** Get position. (for headless mode only) */
public get position(): { x: number; y: number; z: number } {
const t = this.physics.worldTransform
return { x: t.getOrigin().x(), y: t.getOrigin().y(), z: t.getOrigin().z() }
}
public get velocity() {
return {
x: this.ammo.getLinearVelocity().x(),
y: this.ammo.getLinearVelocity().y(),
z: this.ammo.getLinearVelocity().z()
}
}
public get angularVelocity() {
return {
x: this.ammo.getAngularVelocity().x(),
y: this.ammo.getAngularVelocity().y(),
z: this.ammo.getAngularVelocity().z()
}
}
public setVelocity(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.setLinearVelocity(this.tmpBtVector3)
}
public setVelocityX(value: number) {
this.tmpBtVector3.setValue(value, this.velocity.y, this.velocity.z)
this.ammo.setLinearVelocity(this.tmpBtVector3)
}
public setVelocityY(value: number) {
this.tmpBtVector3.setValue(this.velocity.x, value, this.velocity.z)
this.ammo.setLinearVelocity(this.tmpBtVector3)
}
public setVelocityZ(value: number) {
this.tmpBtVector3.setValue(this.velocity.x, this.velocity.y, value)
this.ammo.setLinearVelocity(this.tmpBtVector3)
}
public setAngularVelocity(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.setAngularVelocity(this.tmpBtVector3)
}
public setAngularVelocityX(value: number) {
this.tmpBtVector3.setValue(value, this.angularVelocity.y, this.angularVelocity.z)
this.ammo.setAngularVelocity(this.tmpBtVector3)
}
public setAngularVelocityY(value: number) {
this.tmpBtVector3.setValue(this.angularVelocity.x, value, this.angularVelocity.z)
this.ammo.setAngularVelocity(this.tmpBtVector3)
}
public setAngularVelocityZ(value: number) {
this.tmpBtVector3.setValue(this.angularVelocity.x, this.angularVelocity.y, value)
this.ammo.setAngularVelocity(this.tmpBtVector3)
}
// Apply Force methods
public applyForce(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.applyCentralImpulse(this.tmpBtVector3)
}
public applyForceX(value: number) {
this.tmpBtVector3.setValue(value, 0, 0)
this.ammo.applyCentralImpulse(this.tmpBtVector3)
}
public applyForceY(value: number) {
this.tmpBtVector3.setValue(0, value, 0)
this.ammo.applyCentralImpulse(this.tmpBtVector3)
}
public applyForceZ(value: number) {
this.tmpBtVector3.setValue(0, 0, value)
this.ammo.applyCentralImpulse(this.tmpBtVector3)
}
// Apply "Something" Methods
public applyCentralForce(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.applyCentralForce(this.tmpBtVector3)
}
public applyCentralImpulse(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.applyCentralImpulse(this.tmpBtVector3)
}
/**
* @deprecated applyLocalTorque has been removed in the newer versions of ammo.js
*/
public applyCentralLocalForce(x: number, y: number, z: number) {
console.warn('applyCentralLocalForce has been removed in the newer versions of ammo.js')
// this.tmpBtVector3.setValue(x, y, z)
// this.ammo.applyCentralLocalForce(this.tmpBtVector3)
}
public applyImpulse(impulse: XYZ, relativePosition: XYZ) {
this.tmpBtVector3.setValue(impulse.x || 0, impulse.y || 0, impulse.z || 0)
this.tmpBtVector3_1.setValue(relativePosition.x || 0, relativePosition.y || 0, relativePosition.z || 0)
this.ammo.applyImpulse(this.tmpBtVector3, this.tmpBtVector3_1)
}
/**
* @deprecated applyLocalTorque has been removed in the newer versions of ammo.js
*/
public applyLocalTorque(x: number, y: number, z: number) {
console.warn('applyLocalTorque has been removed in the newer versions of ammo.js')
// this.tmpBtVector3.setValue(x, y, z)
// this.ammo.applyLocalTorque(this.tmpBtVector3)
}
public applyTorque(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.applyTorque(this.tmpBtVector3)
}
public applyTorqueImpulse(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.applyTorqueImpulse(this.tmpBtVector3)
}
/**
* Add the collision flags
* @param value 0 is DYNAMIC, 1 is STATIC, 2 is KINEMATIC, 4 GHOST
*/
// https://github.com/bulletphysics/bullet3/blob/aae8048722f2596f7e2bdd52d2a1dcb52a218f2b/src/BulletCollision/CollisionDispatch/btCollisionObject.h#L128
public setCollisionFlags(value: number) {
this.ammo.setCollisionFlags(value)
}
/**
* Get the collision flags
* @param value 0 is DYNAMIC, 1 is STATIC, 2 is KINEMATIC, 4 GHOST
*/
public getCollisionFlags() {
return this.ammo.getCollisionFlags()
}
/**
* Set the restitution (same as bounciness)
* @param value A number from 0 to 1.
*/
public setRestitution(value: number) {
this.ammo.setRestitution(value)
}
/**
* Set the bounciness (same as restitution)
* @param value A number from 0 to 1.
*/
public setBounciness(value: number) {
this.setRestitution(value)
}
/**
* Set the friction
* @param value A number from 0 to 1.
*/
public setFriction(value: number) {
this.ammo.setFriction(value)
}
/**
* Set the linear and angular damping
* @param linear A number from 0 to 1.
* @param angular A number from 0 to 1.
*/
public setDamping(linear: number, angular: number) {
this.ammo.setDamping(linear, angular)
}
/** Set per body gravity */
public setGravity(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.setGravity(this.tmpBtVector3)
}
public setLinearFactor(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.setLinearFactor(this.tmpBtVector3)
}
public setAngularFactor(x: number, y: number, z: number) {
this.tmpBtVector3.setValue(x, y, z)
this.ammo.setAngularFactor(this.tmpBtVector3)
}
public setCcdMotionThreshold(threshold: number) {
this.ammo.setCcdMotionThreshold(threshold)
}
public setCcdSweptSphereRadius(radius: number) {
this.ammo.setCcdSweptSphereRadius(radius)
}
}
export default PhysicsBody