-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Body.ts
335 lines (289 loc) · 10.4 KB
/
Body.ts
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
import { Vector } from '../Algebra';
import { Actor } from '../Actor';
import { Collider } from './Collider';
import { CollisionType } from './CollisionType';
import { Physics } from '../Physics';
import { obsolete } from '../Util/Decorators';
import { PreCollisionEvent, PostCollisionEvent, CollisionStartEvent, CollisionEndEvent } from '../Events';
import { Clonable } from '../Interfaces/Clonable';
import { Shape } from './Shape';
export interface BodyOptions {
/**
* Optionally the actory associated with this body
*/
actor?: Actor;
/**
* An optional collider to use in this body, if none is specified a default Box collider will be created.
*/
collider?: Collider;
}
/**
* Body describes all the physical properties pos, vel, acc, rotation, angular velocity
*/
export class Body implements Clonable<Body> {
private _collider: Collider;
public actor: Actor;
/**
* Constructs a new physics body associated with an actor
*/
constructor({ actor, collider }: BodyOptions) {
if (!actor && !collider) {
throw new Error('An actor or collider are required to create a body');
}
this.actor = actor;
if (!collider && actor) {
this.collider = this.useBoxCollider(actor.width, actor.height, actor.anchor);
} else {
this.collider = collider;
}
}
public get id() {
return this.actor ? this.actor.id : -1;
}
/**
* Returns a clone of this body, not associated with any actor
*/
public clone() {
return new Body({
actor: null,
collider: this.collider.clone()
});
}
public get active() {
return this.actor ? !this.actor.isKilled() : false;
}
public get center() {
return this.pos;
}
// TODO allow multiple colliders for a single body
public set collider(collider: Collider) {
if (collider) {
this._collider = collider;
this._collider.body = this;
this._wireColliderEventsToActor();
}
}
public get collider(): Collider {
return this._collider;
}
/**
* The (x, y) position of the actor this will be in the middle of the actor if the
* [[Actor.anchor]] is set to (0.5, 0.5) which is default.
* If you want the (x, y) position to be the top left of the actor specify an anchor of (0, 0).
*/
public pos: Vector = new Vector(0, 0);
/**
* The position of the actor last frame (x, y) in pixels
*/
public oldPos: Vector = new Vector(0, 0);
/**
* The current velocity vector (vx, vy) of the actor in pixels/second
*/
public vel: Vector = new Vector(0, 0);
/**
* The velocity of the actor last frame (vx, vy) in pixels/second
*/
public oldVel: Vector = new Vector(0, 0);
/**
* The curret acceleration vector (ax, ay) of the actor in pixels/second/second. An acceleration pointing down such as (0, 100) may
* be useful to simulate a gravitational effect.
*/
public acc: Vector = new Vector(0, 0);
/**
* Gets/sets the acceleration of the actor from the last frame. This does not include the global acc [[Physics.acc]].
*/
public oldAcc: Vector = Vector.Zero;
/**
* The current torque applied to the actor
*/
public torque: number = 0;
/**
* The current "motion" of the actor, used to calculated sleep in the physics simulation
*/
public motion: number = 10;
/**
* Gets/sets the rotation of the body from the last frame.
*/
public oldRotation: number = 0; // radians
/**
* The rotation of the actor in radians
*/
public rotation: number = 0; // radians
/**
* The scale vector of the actor
* @obsolete ex.Body.scale will be removed in v0.24.0
*/
public scale: Vector = Vector.One;
/**
* The scale of the actor last frame
* @obsolete ex.Body.scale will be removed in v0.24.0
*/
public oldScale: Vector = Vector.One;
/**
* The x scalar velocity of the actor in scale/second
* @obsolete ex.Body.scale will be removed in v0.24.0
*/
public sx: number = 0; //scale/sec
/**
* The y scalar velocity of the actor in scale/second
* @obsolete ex.Body.scale will be removed in v0.24.0
*/
public sy: number = 0; //scale/sec
/**
* The rotational velocity of the actor in radians/second
*/
public rx: number = 0; //radians/sec
private _geometryDirty = false;
private _totalMtv: Vector = Vector.Zero;
/**
* Add minimum translation vectors accumulated during the current frame to resolve collisions.
*/
public addMtv(mtv: Vector) {
this._totalMtv.addEqual(mtv);
}
/**
* Applies the accumulated translation vectors to the actors position
*/
public applyMtv(): void {
this.pos.addEqual(this._totalMtv);
this._totalMtv.setTo(0, 0);
}
/**
* Flags the shape dirty and must be recalculated in world space
*/
public markCollisionShapeDirty() {
this._geometryDirty = true;
}
public get isColliderShapeDirty(): boolean {
return this._geometryDirty;
}
/**
* Sets the old versions of pos, vel, acc, and scale.
*/
public captureOldTransform() {
// Capture old values before integration step updates them
this.oldVel.setTo(this.vel.x, this.vel.y);
this.oldPos.setTo(this.pos.x, this.pos.y);
this.oldAcc.setTo(this.acc.x, this.acc.y);
this.oldScale.setTo(this.scale.x, this.scale.y);
this.oldRotation = this.rotation;
}
/**
* Perform euler integration at the specified time step
*/
public integrate(delta: number) {
// Update placements based on linear algebra
const seconds = delta / 1000;
const totalAcc = this.acc.clone();
// Only active vanilla actors are affected by global acceleration
if (this.collider.type === CollisionType.Active) {
totalAcc.addEqual(Physics.acc);
}
this.vel.addEqual(totalAcc.scale(seconds));
this.pos.addEqual(this.vel.scale(seconds)).addEqual(totalAcc.scale(0.5 * seconds * seconds));
this.rx += this.torque * (1.0 / this.collider.inertia) * seconds;
this.rotation += this.rx * seconds;
this.scale.x += (this.sx * delta) / 1000;
this.scale.y += (this.sy * delta) / 1000;
if (!this.scale.equals(this.oldScale)) {
// change in scale effects the geometry
this._geometryDirty = true;
}
// Update colliders
this.collider.update();
this._geometryDirty = false;
}
/**
* Sets up a box geometry based on the current bounds of the associated actor of this physics body.
*
* By default, the box is center is at (0, 0) which means it is centered around the actors anchor.
*/
public useBoxCollider(width: number, height: number, anchor: Vector = Vector.Half, center: Vector = Vector.Zero): Collider {
this.collider.shape = Shape.Box(width, height, anchor, center);
return this.collider;
}
/**
* @obsolete Body.useBoxCollision will be removed in v0.24.0 use [[Body.useBoxCollider]]
*/
@obsolete({ message: 'Will be removed in v0.24.0', alternateMethod: 'Body.useBoxCollider' })
public useBoxCollision(center: Vector = Vector.Zero) {
this.useBoxCollider(this.actor.width, this.actor.height, this.actor.anchor, center);
}
/**
* Sets up a [[ConvexPolygon|convex polygon]] collision geometry based on a list of of points relative
* to the anchor of the associated actor
* of this physics body.
*
* Only [convex polygon](https://en.wikipedia.org/wiki/Convex_polygon) definitions are supported.
*
* By default, the box is center is at (0, 0) which means it is centered around the actors anchor.
*/
public usePolygonCollider(points: Vector[], center: Vector = Vector.Zero): Collider {
this.collider.shape = Shape.Polygon(points, false, center);
return this.collider;
}
/**
* @obsolete Body.usePolygonCollision will be removed in v0.24.0 use [[Body.usePolygonCollider]]
*/
@obsolete({ message: 'Will be removed in v0.24.0', alternateMethod: 'Body.usePolygonCollider' })
public usePolygonCollision(points: Vector[], center: Vector = Vector.Zero) {
this.usePolygonCollider(points, center);
}
/**
* Sets up a [[Circle|circle collision geometry]] with a specified radius in pixels.
*
* By default, the box is center is at (0, 0) which means it is centered around the actors anchor.
*/
public useCircleCollider(radius: number, center: Vector = Vector.Zero): Collider {
this.collider.shape = Shape.Circle(radius, center);
return this.collider;
}
/**
* @obsolete Body.useCircleCollision will be removed in v0.24.0, use [[Body.useCircleCollider]]
*/
@obsolete({ message: 'Will be removed in v0.24.0', alternateMethod: 'Body.useCircleCollider' })
public useCircleCollision(radius?: number, center: Vector = Vector.Zero) {
this.useCircleCollider(radius, center);
}
/**
* Sets up an [[Edge|edge collision geometry]] with a start point and an end point relative to the anchor of the associated actor
* of this physics body.
*
* By default, the box is center is at (0, 0) which means it is centered around the actors anchor.
*/
public useEdgeCollider(begin: Vector, end: Vector): Collider {
this.collider.shape = Shape.Edge(begin, end);
return this.collider;
}
/**
* @obsolete Body.useEdgeCollision will be removed in v0.24.0, use [[Body.useEdgeCollider]]
*/
@obsolete({ message: 'Will be removed in v0.24.0', alternateMethod: 'Body.useEdgeCollider' })
public useEdgeCollision(begin: Vector, end: Vector) {
this.useEdgeCollider(begin, end);
}
// TODO remove this, eventually events will stay local to the thing they are around
private _wireColliderEventsToActor() {
this.collider.clear();
this.collider.on('precollision', (evt: PreCollisionEvent<Collider>) => {
if (this.actor) {
this.actor.emit('precollision', new PreCollisionEvent(evt.target.body.actor, evt.other.body.actor, evt.side, evt.intersection));
}
});
this.collider.on('postcollision', (evt: PostCollisionEvent<Collider>) => {
if (this.actor) {
this.actor.emit('postcollision', new PostCollisionEvent(evt.target.body.actor, evt.other.body.actor, evt.side, evt.intersection));
}
});
this.collider.on('collisionstart', (evt: CollisionStartEvent<Collider>) => {
if (this.actor) {
this.actor.emit('collisionstart', new CollisionStartEvent(evt.target.body.actor, evt.other.body.actor, evt.pair));
}
});
this.collider.on('collisionend', (evt: CollisionEndEvent<Collider>) => {
if (this.actor) {
this.actor.emit('collisionend', new CollisionEndEvent(evt.target.body.actor, evt.other.body.actor));
}
});
}
}