-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
ArcadeSolver.ts
207 lines (179 loc) · 7.26 KB
/
ArcadeSolver.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
import { PostCollisionEvent, PreCollisionEvent } from '../../Events';
import { CollisionContact } from '../Detection/CollisionContact';
import { CollisionType } from '../CollisionType';
import { Side } from '../Side';
import { CollisionSolver } from './Solver';
import { BodyComponent } from '../BodyComponent';
import { ContactBias, ContactSolveBias, HorizontalFirst, None, VerticalFirst } from './ContactBias';
import { PhysicsConfig } from '../PhysicsConfig';
import { DeepRequired } from '../../Util/Required';
/**
* ArcadeSolver is the default in Excalibur. It solves collisions so that there is no overlap between contacts,
* and negates velocity along the collision normal.
*
* This is usually the type of collisions used for 2D games that don't need a more realistic collision simulation.
*
*/
export class ArcadeSolver implements CollisionSolver {
directionMap = new Map<string, 'horizontal' | 'vertical'>();
distanceMap = new Map<string, number>();
constructor(public config: DeepRequired<Pick<PhysicsConfig, 'arcade'>['arcade']>) {}
public solve(contacts: CollisionContact[]): CollisionContact[] {
// Events and init
this.preSolve(contacts);
// Remove any canceled contacts
contacts = contacts.filter((c) => !c.isCanceled());
// Locate collision bias order
let bias: ContactBias;
switch (this.config.contactSolveBias) {
case ContactSolveBias.HorizontalFirst: {
bias = HorizontalFirst;
break;
}
case ContactSolveBias.VerticalFirst: {
bias = VerticalFirst;
break;
}
default: {
bias = None;
}
}
// Sort by bias (None, VerticalFirst, HorizontalFirst) to avoid artifacts with seams
// Sort contacts by distance to avoid artifacts with seams
// It's important to solve in a specific order
contacts.sort((a, b) => {
const aDir = this.directionMap.get(a.id);
const bDir = this.directionMap.get(b.id);
const aDist = this.distanceMap.get(a.id);
const bDist = this.distanceMap.get(b.id);
return bias[aDir] - bias[bDir] || aDist - bDist;
});
for (const contact of contacts) {
// Solve position first in arcade
this.solvePosition(contact);
// Solve velocity second in arcade
this.solveVelocity(contact);
}
// Events and any contact house-keeping the solver needs
this.postSolve(contacts);
return contacts;
}
public preSolve(contacts: CollisionContact[]) {
const epsilon = 0.0001;
for (const contact of contacts) {
if (Math.abs(contact.mtv.x) < epsilon && Math.abs(contact.mtv.y) < epsilon) {
// Cancel near 0 mtv collisions
contact.cancel();
continue;
}
const side = Side.fromDirection(contact.mtv);
const mtv = contact.mtv.negate();
const distance = contact.colliderA.worldPos.squareDistance(contact.colliderB.worldPos);
this.distanceMap.set(contact.id, distance);
this.directionMap.set(contact.id, side === Side.Left || side === Side.Right ? 'horizontal' : 'vertical');
// Publish collision events on both participants
contact.colliderA.events.emit('precollision', new PreCollisionEvent(contact.colliderA, contact.colliderB, side, mtv, contact));
contact.colliderB.events.emit(
'precollision',
new PreCollisionEvent(contact.colliderB, contact.colliderA, Side.getOpposite(side), mtv.negate(), contact)
);
}
}
public postSolve(contacts: CollisionContact[]) {
for (const contact of contacts) {
if (contact.isCanceled()) {
continue;
}
const colliderA = contact.colliderA;
const colliderB = contact.colliderB;
const bodyA = colliderA.owner?.get(BodyComponent);
const bodyB = colliderB.owner?.get(BodyComponent);
if (bodyA && bodyB) {
if (bodyA.collisionType === CollisionType.Passive || bodyB.collisionType === CollisionType.Passive) {
continue;
}
}
const side = Side.fromDirection(contact.mtv);
const mtv = contact.mtv.negate();
// Publish collision events on both participants
contact.colliderA.events.emit('postcollision', new PostCollisionEvent(contact.colliderA, contact.colliderB, side, mtv, contact));
contact.colliderB.events.emit(
'postcollision',
new PostCollisionEvent(contact.colliderB, contact.colliderA, Side.getOpposite(side), mtv.negate(), contact)
);
}
}
public solvePosition(contact: CollisionContact) {
const epsilon = 0.0001;
// if bounds no longer intersect skip to the next
// this removes jitter from overlapping/stacked solid tiles or a wall of solid tiles
if (!contact.colliderA.bounds.overlaps(contact.colliderB.bounds, epsilon)) {
// Cancel the contact to prevent and solving
contact.cancel();
return;
}
if (Math.abs(contact.mtv.x) < epsilon && Math.abs(contact.mtv.y) < epsilon) {
// Cancel near 0 mtv collisions
contact.cancel();
return;
}
let mtv = contact.mtv;
const colliderA = contact.colliderA;
const colliderB = contact.colliderB;
const bodyA = colliderA.owner?.get(BodyComponent);
const bodyB = colliderB.owner?.get(BodyComponent);
if (bodyA && bodyB) {
if (bodyA.collisionType === CollisionType.Passive || bodyB.collisionType === CollisionType.Passive) {
return;
}
if (bodyA.collisionType === CollisionType.Active && bodyB.collisionType === CollisionType.Active) {
// split overlaps if both are Active
mtv = mtv.scale(0.5);
}
// Resolve overlaps
if (bodyA.collisionType === CollisionType.Active) {
bodyA.globalPos.x -= mtv.x;
bodyA.globalPos.y -= mtv.y;
colliderA.update(bodyA.transform.get());
}
if (bodyB.collisionType === CollisionType.Active) {
bodyB.globalPos.x += mtv.x;
bodyB.globalPos.y += mtv.y;
colliderB.update(bodyB.transform.get());
}
}
}
public solveVelocity(contact: CollisionContact) {
if (contact.isCanceled()) {
return;
}
const colliderA = contact.colliderA;
const colliderB = contact.colliderB;
const bodyA = colliderA.owner?.get(BodyComponent);
const bodyB = colliderB.owner?.get(BodyComponent);
if (bodyA && bodyB) {
if (bodyA.collisionType === CollisionType.Passive || bodyB.collisionType === CollisionType.Passive) {
return;
}
const normal = contact.normal;
const opposite = normal.negate();
if (bodyA.collisionType === CollisionType.Active) {
// only adjust velocity if the contact normal is opposite to the current velocity
// this avoids catching edges on a platform when sliding off
if (bodyA.vel.normalize().dot(opposite) < 0) {
// Cancel out velocity opposite direction of collision normal
const velAdj = normal.scale(normal.dot(bodyA.vel.negate()));
bodyA.vel = bodyA.vel.add(velAdj);
}
}
if (bodyB.collisionType === CollisionType.Active) {
// only adjust velocity if the contact normal is opposite to the current velocity
// this avoids catching edges on a platform
if (bodyB.vel.normalize().dot(normal) < 0) {
const velAdj = opposite.scale(opposite.dot(bodyB.vel.negate()));
bodyB.vel = bodyB.vel.add(velAdj);
}
}
}
}
}