-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics_Space.js.html
More file actions
341 lines (275 loc) · 19.9 KB
/
physics_Space.js.html
File metadata and controls
341 lines (275 loc) · 19.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: physics/Space.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: physics/Space.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>//------------------------------------------------------------------------------
// Constructor scope
//------------------------------------------------------------------------------
/**
* Creates a new instance of the Space class.
*
* @constructor
*
* @class
* @classdesc
*
* The Space class contains static methods for collision handling of display
* objects. The logic behind the class consists of a modified version of the
* collision logic used by Adam Saltsman's Flixel engine. Collision is limited
* to the object's hitbox and therefore ignores the size of the object's
* graphic representation. Note that all class content is static, so the class
* never needs to be instantiated.
*/
rune.physics.Space = function() {
console.warn("This class is not meant to be instantiated.");
}
//------------------------------------------------------------------------------
// Public static constants
//------------------------------------------------------------------------------
/**
* Bit field indicating the orthogonal direction down.
*
* @const {number}
* @default 0x1000
*/
rune.physics.Space.DOWN = 0x1000;
/**
* Bit field indicating the orthogonal direction left.
*
* @const {number}
* @default 0x0001
*/
rune.physics.Space.LEFT = 0x0001;
/**
* Indicates no direction.
*
* @const {number}
* @default 0x0000
*/
rune.physics.Space.NONE = 0x0000;
/**
* Bit field indicating the orthogonal direction right.
*
* @const {number}
* @default 0x0010
*/
rune.physics.Space.RIGHT = 0x0010;
/**
* Bit field indicating the orthogonal direction up.
*
* @const {number}
* @default 0x0100
*/
rune.physics.Space.UP = 0x0100;
/**
* Bit field indicating any, or all orthogonal directions.
*
* @const {number}
* @default 0x1111
*/
rune.physics.Space.ANY = rune.physics.Space.LEFT | rune.physics.Space.RIGHT | rune.physics.Space.UP | rune.physics.Space.DOWN;
//------------------------------------------------------------------------------
// Private static constants
//------------------------------------------------------------------------------
/**
* Used to resolve collisions between two objects.
*
* @const {number}
* @private
*/
rune.physics.Space.OVERLAP_BIAS = 2;
//------------------------------------------------------------------------------
// Public static methods
//------------------------------------------------------------------------------
/**
* Resolves collision between two objects based on their physical properties.
* The objects are separated in both x and y directions.
*
* @param {rune.display.InteractiveObject} obj1 The first object.
* @param {rune.display.InteractiveObject} obj2 The second object.
*
* @returns {boolean} If the objects were separated.
*/
rune.physics.Space.separate = function(obj1, obj2) {
var separatedX = rune.physics.Space.separateX(obj1, obj2);
var separatedY = rune.physics.Space.separateY(obj1, obj2);
return separatedX || separatedY;
};
/**
* Resolves collision between two objects, but only in x direction. The y
* coordinates of the objects will not change.
*
* @param {rune.display.InteractiveObject} obj1 The first object.
* @param {rune.display.InteractiveObject} obj2 obj2 The second object.
*
* @returns {boolean} If the objects were separated.
*/
rune.physics.Space.separateX = function(obj1, obj2) {
var obj1immovable = obj1['immovable'];
var obj2immovable = obj2['immovable'];
if (obj1immovable && obj2immovable) {
return false;
}
var overlap = 0;
var obj1delta = obj1['hitbox']['x'] - obj1['hitbox']['previousX'];
var obj2delta = obj2['hitbox']['x'] - obj2['hitbox']['previousX'];
if (obj1delta != obj2delta) {
var obj1deltaAbs = Math.abs(obj1delta);
var obj2deltaAbs = Math.abs(obj2delta);
var obj1rect = new rune.geom.Rectangle(obj1['hitbox']['x'] - ((obj1delta > 0) ? obj1delta : 0), obj1['hitbox']['previousY'], obj1['hitbox']['width'] + ((obj1delta > 0) ? obj1delta : -obj1delta), obj1['hitbox']['height']);
var obj2rect = new rune.geom.Rectangle(obj2['hitbox']['x'] - ((obj2delta > 0) ? obj2delta : 0), obj2['hitbox']['previousY'], obj2['hitbox']['width'] + ((obj2delta > 0) ? obj2delta : -obj2delta), obj2['hitbox']['height']);
if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) {
var maxOverlap = obj1deltaAbs + obj2deltaAbs + rune.physics.Space.OVERLAP_BIAS;
var LEFT = rune.physics.Space.LEFT;
var RIGHT = rune.physics.Space.RIGHT;
if (obj1delta > obj2delta) {
overlap = obj1['hitbox']['x'] + obj1['hitbox']['width'] - obj2['hitbox']['x'];
if ((overlap > maxOverlap) || !(obj1['allowCollisions'] & RIGHT) || !(obj2['allowCollisions'] & LEFT)) {
overlap = 0;
} else {
obj1.touching |= RIGHT;
obj2.touching |= LEFT;
}
} else if (obj1delta < obj2delta) {
overlap = obj1['hitbox']['x'] - obj2['hitbox']['width'] - obj2['hitbox']['x'];
if ((-overlap > maxOverlap) || !(obj1['allowCollisions'] & LEFT) || !(obj2['allowCollisions'] & RIGHT)) {
overlap = 0;
} else {
obj1.touching |= LEFT;
obj2.touching |= RIGHT;
}
}
}
}
if (overlap != 0) {
var obj1v = obj1['velocity']['x'];
var obj2v = obj2['velocity']['x'];
if (!obj1immovable && !obj2immovable) {
overlap *= 0.5;
obj1.x = obj1.x - overlap;
obj2.x += overlap;
var obj1velocity = Math.sqrt((obj2v * obj2v * obj2.mass) / obj1.mass) * ((obj2v > 0) ? 1 : -1);
var obj2velocity = Math.sqrt((obj1v * obj1v * obj1.mass) / obj2.mass) * ((obj1v > 0) ? 1 : -1);
var average = (obj1velocity + obj2velocity) * 0.5;
obj1velocity -= average;
obj2velocity -= average;
obj1['velocity']['x'] = average + obj1velocity * obj1.elasticity;
obj2['velocity']['x'] = average + obj2velocity * obj2.elasticity;
} else if (!obj1immovable) {
obj1.x = obj1.x - overlap;
obj1['velocity']['x'] = obj2v - obj1v * obj1.elasticity;
} else if (!obj2immovable) {
obj2.x += overlap;
obj2['velocity']['x'] = obj1v - obj2v * obj2.elasticity;
}
return true;
} else {
return false;
}
};
/**
* Resolves collision between two objects, but only in y direction. The x
* coordinates of the objects will not change.
*
* @param {rune.display.InteractiveObject} obj1 The first object.
* @param {rune.display.InteractiveObject} obj2 The second object.
*
* @returns {boolean} If the objects were separated.
*/
rune.physics.Space.separateY = function(obj1, obj2) {
var obj1immovable = obj1['immovable'];
var obj2immovable = obj2['immovable'];
if (obj1immovable && obj2immovable) {
return false;
}
var overlap = 0;
var obj1delta = obj1['hitbox']['y'] - obj1['hitbox']['previousY'];
var obj2delta = obj2['hitbox']['y'] - obj2['hitbox']['previousY'];
if (obj1delta != obj2delta) {
var obj1deltaAbs = Math.abs(obj1delta);
var obj2deltaAbs = Math.abs(obj2delta);
var obj1rect = new rune.geom.Rectangle(obj1['hitbox']['x'], obj1['hitbox']['y'] - ((obj1delta > 0) ? obj1delta : 0), obj1['hitbox']['width'], obj1['hitbox']['height'] + obj1deltaAbs);
var obj2rect = new rune.geom.Rectangle(obj2['hitbox']['x'], obj2['hitbox']['y'] - ((obj2delta > 0) ? obj2delta : 0), obj2['hitbox']['width'], obj2['hitbox']['height'] + obj2deltaAbs);
if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) {
var maxOverlap = obj1deltaAbs + obj2deltaAbs + rune.physics.Space.OVERLAP_BIAS;
var UP = rune.physics.Space.UP;
var DOWN = rune.physics.Space.DOWN;
if (obj1delta > obj2delta) {
overlap = obj1['hitbox']['y'] + obj1['hitbox']['height'] - obj2['hitbox']['y'];
if ((overlap > maxOverlap) || !(obj1['allowCollisions'] & DOWN) || !(obj2['allowCollisions'] & UP)) {
overlap = 0;
} else {
obj1.touching |= DOWN;
obj2.touching |= UP;
}
} else if (obj1delta < obj2delta) {
overlap = obj1['hitbox']['y'] - obj2['hitbox']['height'] - obj2['hitbox']['y'];
if ((-overlap > maxOverlap) || !(obj1['allowCollisions'] & UP) || !(obj2['allowCollisions'] & DOWN)) {
overlap = 0;
} else {
obj1.touching |= UP;
obj2.touching |= DOWN;
}
}
}
}
if (overlap != 0) {
var obj1v = obj1['velocity']['y'];
var obj2v = obj2['velocity']['y'];
if (!obj1immovable && !obj2immovable) {
overlap *= 0.5;
obj1.y = obj1.y - overlap;
obj2.y += overlap;
var obj1velocity = Math.sqrt((obj2v * obj2v * obj2.mass) / obj1.mass) * ((obj2v > 0) ? 1 : -1);
var obj2velocity = Math.sqrt((obj1v * obj1v * obj1.mass) / obj2.mass) * ((obj1v > 0) ? 1 : -1);
var average = (obj1velocity + obj2velocity) * 0.5;
obj1velocity -= average;
obj2velocity -= average;
obj1['velocity']['y'] = average + obj1velocity * obj1.elasticity;
obj2['velocity']['y'] = average + obj2velocity * obj2.elasticity;
} else if (!obj1immovable) {
obj1.y = obj1.y - overlap;
obj1['velocity']['y'] = obj2v - obj1v * obj1.elasticity;
if (obj2.active && obj2.sticky && (obj1delta > obj2delta)) {
obj1.x += obj2['hitbox']['x'] - obj2['hitbox']['previousX'];
}
} else if (!obj2immovable) {
obj2.y += overlap;
obj2['velocity']['y'] = obj1v - obj2v * obj2.elasticity;
if (obj1.active && obj1.sticky && (obj1delta < obj2delta)) {
obj2.x += obj1.x - obj1['hitbox']['previousX'];
}
}
return true;
} else {
return false;
}
};</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="rune.html">rune</a></li><li><a href="rune.animation.html">animation</a></li><li><a href="rune.camera.html">camera</a></li><li><a href="rune.color.html">color</a></li><li><a href="rune.console.html">console</a></li><li><a href="rune.data.html">data</a></li><li><a href="rune.debug.html">debug</a></li><li><a href="rune.display.html">display</a></li><li><a href="rune.geom.html">geom</a></li><li><a href="rune.input.html">input</a></li><li><a href="rune.media.html">media</a></li><li><a href="rune.net.html">net</a></li><li><a href="rune.particle.html">particle</a></li><li><a href="rune.physics.html">physics</a></li><li><a href="rune.resource.html">resource</a></li><li><a href="rune.scene.html">scene</a></li><li><a href="rune.state.html">state</a></li><li><a href="rune.system.html">system</a></li><li><a href="rune.text.html">text</a></li><li><a href="rune.tilemap.html">tilemap</a></li><li><a href="rune.timer.html">timer</a></li><li><a href="rune.tween.html">tween</a></li><li><a href="rune.ui.html">ui</a></li><li><a href="rune.util.html">util</a></li></ul><h3>Classes</h3><ul><li><a href="rune.animation.Animation.html">Animation</a></li><li><a href="rune.animation.Animations.html">Animations</a></li><li><a href="rune.animation.AnimationScripts.html">AnimationScripts</a></li><li><a href="rune.camera.Camera.html">Camera</a></li><li><a href="rune.camera.CameraFade.html">CameraFade</a></li><li><a href="rune.camera.CameraFlash.html">CameraFlash</a></li><li><a href="rune.camera.Cameras.html">Cameras</a></li><li><a href="rune.camera.CameraShake.html">CameraShake</a></li><li><a href="rune.camera.CameraTargets.html">CameraTargets</a></li><li><a href="rune.camera.CameraTint.html">CameraTint</a></li><li><a href="rune.camera.CameraTintTween.html">CameraTintTween</a></li><li><a href="rune.camera.CameraViewport.html">CameraViewport</a></li><li><a href="rune.color.Color24.html">Color24</a></li><li><a href="rune.color.Color32.html">Color32</a></li><li><a href="rune.color.ColorComponent.html">ColorComponent</a></li><li><a href="rune.console.Console.html">Console</a></li><li><a href="rune.console.ConsoleCommand.html">ConsoleCommand</a></li><li><a href="rune.console.ConsoleCommands.html">ConsoleCommands</a></li><li><a href="rune.console.ConsoleCursor.html">ConsoleCursor</a></li><li><a href="rune.console.ConsoleHistory.html">ConsoleHistory</a></li><li><a href="rune.console.ConsoleInput.html">ConsoleInput</a></li><li><a href="rune.console.ConsoleManager.html">ConsoleManager</a></li><li><a href="rune.console.ConsoleOutput.html">ConsoleOutput</a></li><li><a href="rune.data.Highscores.html">Highscores</a></li><li><a href="rune.data.Loader.html">Loader</a></li><li><a href="rune.data.LoaderDebug.html">LoaderDebug</a></li><li><a href="rune.data.Logo.html">Logo</a></li><li><a href="rune.data.Requests.html">Requests</a></li><li><a href="rune.debug.Debugger.html">Debugger</a></li><li><a href="rune.debug.Framerate.html">Framerate</a></li><li><a href="rune.debug.Histogram.html">Histogram</a></li><li><a href="rune.debug.Master.html">Master</a></li><li><a href="rune.debug.Memory.html">Memory</a></li><li><a href="rune.debug.Music.html">Music</a></li><li><a href="rune.debug.Render.html">Render</a></li><li><a href="rune.debug.Sound.html">Sound</a></li><li><a href="rune.debug.Update.html">Update</a></li><li><a href="rune.display.Artboard.html">Artboard</a></li><li><a href="rune.display.Canvas.html">Canvas</a></li><li><a href="rune.display.DisplayGroup.html">DisplayGroup</a></li><li><a href="rune.display.DisplayGroups.html">DisplayGroups</a></li><li><a href="rune.display.DisplayObject.html">DisplayObject</a></li><li><a href="rune.display.DisplayObjectContainer.html">DisplayObjectContainer</a></li><li><a href="rune.display.Flicker.html">Flicker</a></li><li><a href="rune.display.Frame.html">Frame</a></li><li><a href="rune.display.Graphic.html">Graphic</a></li><li><a href="rune.display.Graphics.html">Graphics</a></li><li><a href="rune.display.Hitbox.html">Hitbox</a></li><li><a href="rune.display.InteractiveObject.html">InteractiveObject</a></li><li><a href="rune.display.Quadtree.html">Quadtree</a></li><li><a href="rune.display.RepeatedGraphic.html">RepeatedGraphic</a></li><li><a href="rune.display.Screen.html">Screen</a></li><li><a href="rune.display.Sprite.html">Sprite</a></li><li><a href="rune.display.Stage.html">Stage</a></li><li><a href="rune.display.Texture.html">Texture</a></li><li><a href="rune.geom.Point.html">Point</a></li><li><a href="rune.geom.Rectangle.html">Rectangle</a></li><li><a href="rune.geom.Vector2D.html">Vector2D</a></li><li><a href="rune.input.Gamepad.html">Gamepad</a></li><li><a href="rune.input.Gamepads.html">Gamepads</a></li><li><a href="rune.input.GamepadsOptions.html">GamepadsOptions</a></li><li><a href="rune.input.Inputs.html">Inputs</a></li><li><a href="rune.input.Keyboard.html">Keyboard</a></li><li><a href="rune.input.KeyboardKey.html">KeyboardKey</a></li><li><a href="rune.input.KeyboardOptions.html">KeyboardOptions</a></li><li><a href="rune.media.Sound.html">Sound</a></li><li><a href="rune.media.SoundChannel.html">SoundChannel</a></li><li><a href="rune.media.Sounds.html">Sounds</a></li><li><a href="rune.net.URLLoader.html">URLLoader</a></li><li><a href="rune.net.URLRequest.html">URLRequest</a></li><li><a href="rune.net.URLResponse.html">URLResponse</a></li><li><a href="rune.particle.Emitter.html">Emitter</a></li><li><a href="rune.particle.EmitterOptions.html">EmitterOptions</a></li><li><a href="rune.particle.Particle.html">Particle</a></li><li><a href="rune.physics.Space.html">Space</a></li><li><a href="rune.physics.Velocity.html">Velocity</a></li><li><a href="rune.resource.Request.html">Request</a></li><li><a href="rune.resource.Requester.html">Requester</a></li><li><a href="rune.resource.RequesterOptions.html">RequesterOptions</a></li><li><a href="rune.resource.Requests.html">Requests</a></li><li><a href="rune.resource.Resource.html">Resource</a></li><li><a href="rune.resource.Resources.html">Resources</a></li><li><a href="rune.scene.Scene.html">Scene</a></li><li><a href="rune.scene.Scenes.html">Scenes</a></li><li><a href="rune.state.State.html">State</a></li><li><a href="rune.state.States.html">States</a></li><li><a href="rune.system.Application.html">Application</a></li><li><a href="rune.system.Config.html">Config</a></li><li><a href="rune.system.Time.html">Time</a></li><li><a href="rune.text.BitmapField.html">BitmapField</a></li><li><a href="rune.text.BitmapFormat.html">BitmapFormat</a></li><li><a href="rune.tilemap.Block.html">Block</a></li><li><a href="rune.tilemap.Tile.html">Tile</a></li><li><a href="rune.tilemap.Tilemap.html">Tilemap</a></li><li><a href="rune.tilemap.TilemapLayer.html">TilemapLayer</a></li><li><a href="rune.timer.Timer.html">Timer</a></li><li><a href="rune.timer.TimerOptions.html">TimerOptions</a></li><li><a href="rune.timer.Timers.html">Timers</a></li><li><a href="rune.tween.Circular.html">Circular</a></li><li><a href="rune.tween.Cubic.html">Cubic</a></li><li><a href="rune.tween.Expo.html">Expo</a></li><li><a href="rune.tween.Linear.html">Linear</a></li><li><a href="rune.tween.Quad.html">Quad</a></li><li><a href="rune.tween.Quart.html">Quart</a></li><li><a href="rune.tween.Quint.html">Quint</a></li><li><a href="rune.tween.Sine.html">Sine</a></li><li><a href="rune.tween.Tween.html">Tween</a></li><li><a href="rune.tween.Tweens.html">Tweens</a></li><li><a href="rune.tween.TweenValue.html">TweenValue</a></li><li><a href="rune.ui.Counter.html">Counter</a></li><li><a href="rune.ui.CounterDigit.html">CounterDigit</a></li><li><a href="rune.ui.Progressbar.html">Progressbar</a></li><li><a href="rune.ui.VTList.html">VTList</a></li><li><a href="rune.ui.VTListElement.html">VTListElement</a></li><li><a href="rune.ui.VTMenu.html">VTMenu</a></li><li><a href="rune.ui.VTMenuPointer.html">VTMenuPointer</a></li><li><a href="rune.util.Executable.html">Executable</a></li><li><a href="rune.util.Filter.html">Filter</a></li><li><a href="rune.util.Math.html">Math</a></li><li><a href="rune.util.Palette.html">Palette</a></li><li><a href="rune.util.Path.html">Path</a></li><li><a href="rune.util.Paths.html">Paths</a></li><li><a href="rune.util.Stack.html">Stack</a></li><li><a href="rune.util.URL.html">URL</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Apr 01 2025 09:55:09 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>