melonjs 10.0.0 → 10.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/melonjs.js +64 -68
- package/dist/melonjs.min.js +2 -2
- package/dist/melonjs.module.d.ts +47 -7
- package/dist/melonjs.module.js +66 -70
- package/package.json +3 -3
- package/src/physics/collision.js +2 -57
- package/src/physics/detector.js +61 -13
package/dist/melonjs.module.d.ts
CHANGED
|
@@ -9191,9 +9191,9 @@ export var audio: Readonly<{
|
|
|
9191
9191
|
*/
|
|
9192
9192
|
export function boot(): void;
|
|
9193
9193
|
export namespace collision {
|
|
9194
|
-
const maxChildren: number;
|
|
9195
|
-
const maxDepth: number;
|
|
9196
|
-
namespace types {
|
|
9194
|
+
export const maxChildren: number;
|
|
9195
|
+
export const maxDepth: number;
|
|
9196
|
+
export namespace types {
|
|
9197
9197
|
const NO_OBJECT: number;
|
|
9198
9198
|
const PLAYER_OBJECT: number;
|
|
9199
9199
|
const NPC_OBJECT: number;
|
|
@@ -9205,7 +9205,7 @@ export namespace collision {
|
|
|
9205
9205
|
const USER: number;
|
|
9206
9206
|
const ALL_OBJECT: number;
|
|
9207
9207
|
}
|
|
9208
|
-
|
|
9208
|
+
export { globalResponse as response };
|
|
9209
9209
|
/**
|
|
9210
9210
|
* Checks for object colliding with the given line
|
|
9211
9211
|
* @name rayCast
|
|
@@ -9233,7 +9233,7 @@ export namespace collision {
|
|
|
9233
9233
|
* // ...
|
|
9234
9234
|
* }
|
|
9235
9235
|
*/
|
|
9236
|
-
function rayCast(line: any, resultArray: any): any[];
|
|
9236
|
+
export function rayCast(line: any, resultArray: any): any[];
|
|
9237
9237
|
/**
|
|
9238
9238
|
* Checks for object colliding with the given line
|
|
9239
9239
|
* @name rayCast
|
|
@@ -9261,7 +9261,7 @@ export namespace collision {
|
|
|
9261
9261
|
* // ...
|
|
9262
9262
|
* }
|
|
9263
9263
|
*/
|
|
9264
|
-
function rayCast(line: any, resultArray: any): any[];
|
|
9264
|
+
export function rayCast(line: any, resultArray: any): any[];
|
|
9265
9265
|
}
|
|
9266
9266
|
export var deprecated: Readonly<{
|
|
9267
9267
|
__proto__: any;
|
|
@@ -12509,6 +12509,7 @@ declare function unload(sound_name: string): boolean;
|
|
|
12509
12509
|
* me.audio.unloadAll();
|
|
12510
12510
|
*/
|
|
12511
12511
|
declare function unloadAll(): void;
|
|
12512
|
+
declare var globalResponse: ResponseObject;
|
|
12512
12513
|
/**
|
|
12513
12514
|
* placeholder for all deprecated classes and corresponding alias for backward compatibility
|
|
12514
12515
|
* @namespace deprecated
|
|
@@ -12864,7 +12865,7 @@ declare class BasePlugin {
|
|
|
12864
12865
|
* this can be overridden by the plugin
|
|
12865
12866
|
* @public
|
|
12866
12867
|
* @type String
|
|
12867
|
-
* @default "10.0.
|
|
12868
|
+
* @default "10.0.1"
|
|
12868
12869
|
* @name me.plugin.Base#version
|
|
12869
12870
|
*/
|
|
12870
12871
|
public version: string;
|
|
@@ -12978,6 +12979,45 @@ declare function getParent(): HTMLElement;
|
|
|
12978
12979
|
* @param {Number} y y scaling multiplier
|
|
12979
12980
|
*/
|
|
12980
12981
|
declare function scale(x: number, y: number): void;
|
|
12982
|
+
/**
|
|
12983
|
+
* @classdesc
|
|
12984
|
+
* An object representing the result of an intersection.
|
|
12985
|
+
* @property {me.Renderable} a The first object participating in the intersection
|
|
12986
|
+
* @property {me.Renderable} b The second object participating in the intersection
|
|
12987
|
+
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
12988
|
+
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
12989
|
+
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
12990
|
+
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
12991
|
+
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
12992
|
+
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
12993
|
+
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
12994
|
+
* @name ResponseObject
|
|
12995
|
+
* @memberOf me.collision
|
|
12996
|
+
* @public
|
|
12997
|
+
* @see me.collision.check
|
|
12998
|
+
*/
|
|
12999
|
+
declare class ResponseObject {
|
|
13000
|
+
a: any;
|
|
13001
|
+
b: any;
|
|
13002
|
+
overlapN: Vector2d;
|
|
13003
|
+
overlapV: Vector2d;
|
|
13004
|
+
aInB: boolean;
|
|
13005
|
+
bInA: boolean;
|
|
13006
|
+
indexShapeA: number;
|
|
13007
|
+
indexShapeB: number;
|
|
13008
|
+
overlap: number;
|
|
13009
|
+
/**
|
|
13010
|
+
* Set some values of the response back to their defaults. <br>
|
|
13011
|
+
* Call this between tests if you are going to reuse a single <br>
|
|
13012
|
+
* Response object for multiple intersection tests <br>
|
|
13013
|
+
* (recommended as it will avoid allocating extra memory) <br>
|
|
13014
|
+
* @name clear
|
|
13015
|
+
* @memberOf me.collision.ResponseObject
|
|
13016
|
+
* @public
|
|
13017
|
+
* @function
|
|
13018
|
+
*/
|
|
13019
|
+
public clear(): ResponseObject;
|
|
13020
|
+
}
|
|
12981
13021
|
/**
|
|
12982
13022
|
* Get a vendor-prefixed property
|
|
12983
13023
|
* @public
|
package/dist/melonjs.module.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine - v10.0.
|
|
2
|
+
* melonJS Game Engine - v10.0.1
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
@@ -14546,20 +14546,71 @@ function shouldCollide(a, b) {
|
|
|
14546
14546
|
(a.body.collisionType & b.body.collisionMask) !== 0
|
|
14547
14547
|
);
|
|
14548
14548
|
}
|
|
14549
|
+
/**
|
|
14550
|
+
* @classdesc
|
|
14551
|
+
* An object representing the result of an intersection.
|
|
14552
|
+
* @property {me.Renderable} a The first object participating in the intersection
|
|
14553
|
+
* @property {me.Renderable} b The second object participating in the intersection
|
|
14554
|
+
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
14555
|
+
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
14556
|
+
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
14557
|
+
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
14558
|
+
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
14559
|
+
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
14560
|
+
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
14561
|
+
* @name ResponseObject
|
|
14562
|
+
* @memberOf me.collision
|
|
14563
|
+
* @public
|
|
14564
|
+
* @see me.collision.check
|
|
14565
|
+
*/
|
|
14566
|
+
class ResponseObject {
|
|
14567
|
+
constructor() {
|
|
14568
|
+
this.a = null;
|
|
14569
|
+
this.b = null;
|
|
14570
|
+
this.overlapN = new Vector2d();
|
|
14571
|
+
this.overlapV = new Vector2d();
|
|
14572
|
+
this.aInB = true;
|
|
14573
|
+
this.bInA = true;
|
|
14574
|
+
this.indexShapeA = -1;
|
|
14575
|
+
this.indexShapeB = -1;
|
|
14576
|
+
this.overlap = Number.MAX_VALUE;
|
|
14577
|
+
return this;
|
|
14578
|
+
}
|
|
14579
|
+
|
|
14580
|
+
/**
|
|
14581
|
+
* Set some values of the response back to their defaults. <br>
|
|
14582
|
+
* Call this between tests if you are going to reuse a single <br>
|
|
14583
|
+
* Response object for multiple intersection tests <br>
|
|
14584
|
+
* (recommended as it will avoid allocating extra memory) <br>
|
|
14585
|
+
* @name clear
|
|
14586
|
+
* @memberOf me.collision.ResponseObject
|
|
14587
|
+
* @public
|
|
14588
|
+
* @function
|
|
14589
|
+
*/
|
|
14590
|
+
clear () {
|
|
14591
|
+
this.aInB = true;
|
|
14592
|
+
this.bInA = true;
|
|
14593
|
+
this.overlap = Number.MAX_VALUE;
|
|
14594
|
+
this.indexShapeA = -1;
|
|
14595
|
+
this.indexShapeB = -1;
|
|
14596
|
+
return this;
|
|
14597
|
+
}
|
|
14598
|
+
}
|
|
14599
|
+
|
|
14600
|
+
// @ignore
|
|
14601
|
+
var globalResponse = new ResponseObject();
|
|
14549
14602
|
|
|
14550
14603
|
/**
|
|
14551
14604
|
* find all the collisions for the specified object
|
|
14552
14605
|
* @name collisionCheck
|
|
14553
|
-
* @memberOf me.collision
|
|
14554
14606
|
* @ignore
|
|
14555
14607
|
* @function
|
|
14556
14608
|
* @param {me.Renderable} obj object to be tested for collision
|
|
14557
14609
|
* @param {me.collision.ResponseObject} [response=me.collision.response] a user defined response object that will be populated if they intersect.
|
|
14558
14610
|
* @return {Boolean} in case of collision, false otherwise
|
|
14559
14611
|
*/
|
|
14560
|
-
function collisionCheck(objA, response =
|
|
14561
|
-
var
|
|
14562
|
-
|
|
14612
|
+
function collisionCheck(objA, response = globalResponse) {
|
|
14613
|
+
var collisionCounter = 0;
|
|
14563
14614
|
// retreive a list of potential colliding objects from the game world
|
|
14564
14615
|
var candidates = world.broadphase.retrieve(objA);
|
|
14565
14616
|
|
|
@@ -14597,7 +14648,7 @@ function collisionCheck(objA, response = collision.response) {
|
|
|
14597
14648
|
response.clear()) === true
|
|
14598
14649
|
) {
|
|
14599
14650
|
// we touched something !
|
|
14600
|
-
|
|
14651
|
+
collisionCounter++;
|
|
14601
14652
|
|
|
14602
14653
|
// set the shape index
|
|
14603
14654
|
response.indexShapeA = indexA;
|
|
@@ -14618,12 +14669,11 @@ function collisionCheck(objA, response = collision.response) {
|
|
|
14618
14669
|
}
|
|
14619
14670
|
}
|
|
14620
14671
|
// we could return the amount of objects we collided with ?
|
|
14621
|
-
return
|
|
14672
|
+
return collisionCounter > 0;
|
|
14622
14673
|
}
|
|
14623
14674
|
/**
|
|
14624
14675
|
* Checks for object colliding with the given line
|
|
14625
14676
|
* @name rayCast
|
|
14626
|
-
* @memberOf me.collision
|
|
14627
14677
|
* @ignore
|
|
14628
14678
|
* @function
|
|
14629
14679
|
* @param {me.Line} line line to be tested for collision
|
|
@@ -14648,7 +14698,7 @@ function collisionCheck(objA, response = collision.response) {
|
|
|
14648
14698
|
* }
|
|
14649
14699
|
*/
|
|
14650
14700
|
function rayCast(line, result = []) {
|
|
14651
|
-
var
|
|
14701
|
+
var collisionCounter = 0;
|
|
14652
14702
|
|
|
14653
14703
|
// retrieve a list of potential colliding objects from the game world
|
|
14654
14704
|
var candidates = world.broadphase.retrieve(line);
|
|
@@ -14681,8 +14731,8 @@ function rayCast(line, result = []) {
|
|
|
14681
14731
|
shapeB
|
|
14682
14732
|
)) {
|
|
14683
14733
|
// we touched something !
|
|
14684
|
-
result[
|
|
14685
|
-
|
|
14734
|
+
result[collisionCounter] = objB;
|
|
14735
|
+
collisionCounter++;
|
|
14686
14736
|
}
|
|
14687
14737
|
indexB++;
|
|
14688
14738
|
} while (indexB < bLen);
|
|
@@ -14690,7 +14740,7 @@ function rayCast(line, result = []) {
|
|
|
14690
14740
|
}
|
|
14691
14741
|
|
|
14692
14742
|
// cap result in case it was not empty
|
|
14693
|
-
result.length =
|
|
14743
|
+
result.length = collisionCounter;
|
|
14694
14744
|
|
|
14695
14745
|
// return the list of colliding objects
|
|
14696
14746
|
return result;
|
|
@@ -14703,58 +14753,6 @@ function rayCast(line, result = []) {
|
|
|
14703
14753
|
* @memberOf me
|
|
14704
14754
|
*/
|
|
14705
14755
|
|
|
14706
|
-
/**
|
|
14707
|
-
* @classdesc
|
|
14708
|
-
* An object representing the result of an intersection.
|
|
14709
|
-
* @property {me.Renderable} a The first object participating in the intersection
|
|
14710
|
-
* @property {me.Renderable} b The second object participating in the intersection
|
|
14711
|
-
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
14712
|
-
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
14713
|
-
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
14714
|
-
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
14715
|
-
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
14716
|
-
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
14717
|
-
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
14718
|
-
* @name ResponseObject
|
|
14719
|
-
* @memberOf me.collision
|
|
14720
|
-
* @public
|
|
14721
|
-
* @see me.collision.check
|
|
14722
|
-
*/
|
|
14723
|
-
class ResponseObject {
|
|
14724
|
-
constructor() {
|
|
14725
|
-
this.a = null;
|
|
14726
|
-
this.b = null;
|
|
14727
|
-
this.overlapN = new Vector2d();
|
|
14728
|
-
this.overlapV = new Vector2d();
|
|
14729
|
-
this.aInB = true;
|
|
14730
|
-
this.bInA = true;
|
|
14731
|
-
this.indexShapeA = -1;
|
|
14732
|
-
this.indexShapeB = -1;
|
|
14733
|
-
this.overlap = Number.MAX_VALUE;
|
|
14734
|
-
return this;
|
|
14735
|
-
}
|
|
14736
|
-
|
|
14737
|
-
/**
|
|
14738
|
-
* Set some values of the response back to their defaults. <br>
|
|
14739
|
-
* Call this between tests if you are going to reuse a single <br>
|
|
14740
|
-
* Response object for multiple intersection tests <br>
|
|
14741
|
-
* (recommended as it will avoid allocating extra memory) <br>
|
|
14742
|
-
* @name clear
|
|
14743
|
-
* @memberOf me.collision.ResponseObject
|
|
14744
|
-
* @public
|
|
14745
|
-
* @function
|
|
14746
|
-
*/
|
|
14747
|
-
clear () {
|
|
14748
|
-
this.aInB = true;
|
|
14749
|
-
this.bInA = true;
|
|
14750
|
-
this.overlap = Number.MAX_VALUE;
|
|
14751
|
-
this.indexShapeA = -1;
|
|
14752
|
-
this.indexShapeB = -1;
|
|
14753
|
-
return this;
|
|
14754
|
-
}
|
|
14755
|
-
}
|
|
14756
|
-
|
|
14757
|
-
|
|
14758
14756
|
var collision = {
|
|
14759
14757
|
|
|
14760
14758
|
/**
|
|
@@ -14850,8 +14848,7 @@ var collision = {
|
|
|
14850
14848
|
* @public
|
|
14851
14849
|
* @type {me.collision.ResponseObject}
|
|
14852
14850
|
*/
|
|
14853
|
-
response :
|
|
14854
|
-
|
|
14851
|
+
response : globalResponse,
|
|
14855
14852
|
|
|
14856
14853
|
/**
|
|
14857
14854
|
* Checks for object colliding with the given line
|
|
@@ -14881,7 +14878,6 @@ var collision = {
|
|
|
14881
14878
|
* }
|
|
14882
14879
|
*/
|
|
14883
14880
|
rayCast(line, resultArray) { return rayCast(line, resultArray); }
|
|
14884
|
-
|
|
14885
14881
|
};
|
|
14886
14882
|
|
|
14887
14883
|
/**
|
|
@@ -31448,10 +31444,10 @@ class BasePlugin {
|
|
|
31448
31444
|
* this can be overridden by the plugin
|
|
31449
31445
|
* @public
|
|
31450
31446
|
* @type String
|
|
31451
|
-
* @default "10.0.
|
|
31447
|
+
* @default "10.0.1"
|
|
31452
31448
|
* @name me.plugin.Base#version
|
|
31453
31449
|
*/
|
|
31454
|
-
this.version = "10.0.
|
|
31450
|
+
this.version = "10.0.1";
|
|
31455
31451
|
}
|
|
31456
31452
|
}
|
|
31457
31453
|
|
|
@@ -35685,7 +35681,7 @@ var deprecated = /*#__PURE__*/Object.freeze({
|
|
|
35685
35681
|
* @name version
|
|
35686
35682
|
* @type {string}
|
|
35687
35683
|
*/
|
|
35688
|
-
const version = "10.0.
|
|
35684
|
+
const version = "10.0.1";
|
|
35689
35685
|
|
|
35690
35686
|
|
|
35691
35687
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melonjs",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.1",
|
|
4
4
|
"description": "melonJS Game Engine",
|
|
5
5
|
"homepage": "http://www.melonjs.org/",
|
|
6
6
|
"keywords": [
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"@rollup/plugin-replace": "^3.0.0",
|
|
65
65
|
"cheerio": "^1.0.0-rc.10",
|
|
66
66
|
"del-cli": "^4.0.1",
|
|
67
|
-
"eslint": "^8.0
|
|
67
|
+
"eslint": "^8.1.0",
|
|
68
68
|
"ghpages": "0.0.10",
|
|
69
|
-
"jasmine-core": "^3.10.
|
|
69
|
+
"jasmine-core": "^3.10.1",
|
|
70
70
|
"jsdoc": "^3.6.7",
|
|
71
71
|
"karma": "^6.3.5",
|
|
72
72
|
"karma-chrome-launcher": "^3.1.0",
|
package/src/physics/collision.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { rayCast } from "./detector.js";
|
|
1
|
+
import { rayCast, globalResponse } from "./detector.js";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Collision detection (and projection-based collision response) of 2D shapes.<br>
|
|
@@ -8,58 +7,6 @@ import { rayCast } from "./detector.js";
|
|
|
8
7
|
* @memberOf me
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
|
-
/**
|
|
12
|
-
* @classdesc
|
|
13
|
-
* An object representing the result of an intersection.
|
|
14
|
-
* @property {me.Renderable} a The first object participating in the intersection
|
|
15
|
-
* @property {me.Renderable} b The second object participating in the intersection
|
|
16
|
-
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
17
|
-
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
18
|
-
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
19
|
-
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
20
|
-
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
21
|
-
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
22
|
-
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
23
|
-
* @name ResponseObject
|
|
24
|
-
* @memberOf me.collision
|
|
25
|
-
* @public
|
|
26
|
-
* @see me.collision.check
|
|
27
|
-
*/
|
|
28
|
-
class ResponseObject {
|
|
29
|
-
constructor() {
|
|
30
|
-
this.a = null;
|
|
31
|
-
this.b = null;
|
|
32
|
-
this.overlapN = new Vector2d();
|
|
33
|
-
this.overlapV = new Vector2d();
|
|
34
|
-
this.aInB = true;
|
|
35
|
-
this.bInA = true;
|
|
36
|
-
this.indexShapeA = -1;
|
|
37
|
-
this.indexShapeB = -1;
|
|
38
|
-
this.overlap = Number.MAX_VALUE;
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Set some values of the response back to their defaults. <br>
|
|
44
|
-
* Call this between tests if you are going to reuse a single <br>
|
|
45
|
-
* Response object for multiple intersection tests <br>
|
|
46
|
-
* (recommended as it will avoid allocating extra memory) <br>
|
|
47
|
-
* @name clear
|
|
48
|
-
* @memberOf me.collision.ResponseObject
|
|
49
|
-
* @public
|
|
50
|
-
* @function
|
|
51
|
-
*/
|
|
52
|
-
clear () {
|
|
53
|
-
this.aInB = true;
|
|
54
|
-
this.bInA = true;
|
|
55
|
-
this.overlap = Number.MAX_VALUE;
|
|
56
|
-
this.indexShapeA = -1;
|
|
57
|
-
this.indexShapeB = -1;
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
10
|
var collision = {
|
|
64
11
|
|
|
65
12
|
/**
|
|
@@ -155,8 +102,7 @@ var collision = {
|
|
|
155
102
|
* @public
|
|
156
103
|
* @type {me.collision.ResponseObject}
|
|
157
104
|
*/
|
|
158
|
-
response :
|
|
159
|
-
|
|
105
|
+
response : globalResponse,
|
|
160
106
|
|
|
161
107
|
/**
|
|
162
108
|
* Checks for object colliding with the given line
|
|
@@ -186,7 +132,6 @@ var collision = {
|
|
|
186
132
|
* }
|
|
187
133
|
*/
|
|
188
134
|
rayCast(line, resultArray) { return rayCast(line, resultArray); }
|
|
189
|
-
|
|
190
135
|
};
|
|
191
136
|
|
|
192
137
|
export default collision;
|
package/src/physics/detector.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as SAT from "./sat.js";
|
|
2
2
|
import Vector2d from "./../math/vector2.js";
|
|
3
3
|
import { world } from "./../game.js";
|
|
4
|
-
import collision from "./collision.js";
|
|
5
|
-
|
|
6
4
|
|
|
7
5
|
// a dummy object when using Line for raycasting
|
|
8
6
|
var dummyObj = {
|
|
@@ -35,20 +33,71 @@ function shouldCollide(a, b) {
|
|
|
35
33
|
);
|
|
36
34
|
};
|
|
37
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @classdesc
|
|
38
|
+
* An object representing the result of an intersection.
|
|
39
|
+
* @property {me.Renderable} a The first object participating in the intersection
|
|
40
|
+
* @property {me.Renderable} b The second object participating in the intersection
|
|
41
|
+
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
42
|
+
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
43
|
+
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
44
|
+
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
45
|
+
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
46
|
+
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
47
|
+
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
48
|
+
* @name ResponseObject
|
|
49
|
+
* @memberOf me.collision
|
|
50
|
+
* @public
|
|
51
|
+
* @see me.collision.check
|
|
52
|
+
*/
|
|
53
|
+
class ResponseObject {
|
|
54
|
+
constructor() {
|
|
55
|
+
this.a = null;
|
|
56
|
+
this.b = null;
|
|
57
|
+
this.overlapN = new Vector2d();
|
|
58
|
+
this.overlapV = new Vector2d();
|
|
59
|
+
this.aInB = true;
|
|
60
|
+
this.bInA = true;
|
|
61
|
+
this.indexShapeA = -1;
|
|
62
|
+
this.indexShapeB = -1;
|
|
63
|
+
this.overlap = Number.MAX_VALUE;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Set some values of the response back to their defaults. <br>
|
|
69
|
+
* Call this between tests if you are going to reuse a single <br>
|
|
70
|
+
* Response object for multiple intersection tests <br>
|
|
71
|
+
* (recommended as it will avoid allocating extra memory) <br>
|
|
72
|
+
* @name clear
|
|
73
|
+
* @memberOf me.collision.ResponseObject
|
|
74
|
+
* @public
|
|
75
|
+
* @function
|
|
76
|
+
*/
|
|
77
|
+
clear () {
|
|
78
|
+
this.aInB = true;
|
|
79
|
+
this.bInA = true;
|
|
80
|
+
this.overlap = Number.MAX_VALUE;
|
|
81
|
+
this.indexShapeA = -1;
|
|
82
|
+
this.indexShapeB = -1;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// @ignore
|
|
88
|
+
export var globalResponse = new ResponseObject();
|
|
38
89
|
|
|
39
90
|
/**
|
|
40
91
|
* find all the collisions for the specified object
|
|
41
92
|
* @name collisionCheck
|
|
42
|
-
* @memberOf me.collision
|
|
43
93
|
* @ignore
|
|
44
94
|
* @function
|
|
45
95
|
* @param {me.Renderable} obj object to be tested for collision
|
|
46
96
|
* @param {me.collision.ResponseObject} [response=me.collision.response] a user defined response object that will be populated if they intersect.
|
|
47
97
|
* @return {Boolean} in case of collision, false otherwise
|
|
48
98
|
*/
|
|
49
|
-
export function collisionCheck(objA, response =
|
|
50
|
-
var
|
|
51
|
-
|
|
99
|
+
export function collisionCheck(objA, response = globalResponse) {
|
|
100
|
+
var collisionCounter = 0;
|
|
52
101
|
// retreive a list of potential colliding objects from the game world
|
|
53
102
|
var candidates = world.broadphase.retrieve(objA);
|
|
54
103
|
|
|
@@ -86,7 +135,7 @@ export function collisionCheck(objA, response = collision.response) {
|
|
|
86
135
|
response.clear()) === true
|
|
87
136
|
) {
|
|
88
137
|
// we touched something !
|
|
89
|
-
|
|
138
|
+
collisionCounter++;
|
|
90
139
|
|
|
91
140
|
// set the shape index
|
|
92
141
|
response.indexShapeA = indexA;
|
|
@@ -107,13 +156,12 @@ export function collisionCheck(objA, response = collision.response) {
|
|
|
107
156
|
}
|
|
108
157
|
}
|
|
109
158
|
// we could return the amount of objects we collided with ?
|
|
110
|
-
return
|
|
159
|
+
return collisionCounter > 0;
|
|
111
160
|
};
|
|
112
161
|
|
|
113
162
|
/**
|
|
114
163
|
* Checks for object colliding with the given line
|
|
115
164
|
* @name rayCast
|
|
116
|
-
* @memberOf me.collision
|
|
117
165
|
* @ignore
|
|
118
166
|
* @function
|
|
119
167
|
* @param {me.Line} line line to be tested for collision
|
|
@@ -138,7 +186,7 @@ export function collisionCheck(objA, response = collision.response) {
|
|
|
138
186
|
* }
|
|
139
187
|
*/
|
|
140
188
|
export function rayCast(line, result = []) {
|
|
141
|
-
var
|
|
189
|
+
var collisionCounter = 0;
|
|
142
190
|
|
|
143
191
|
// retrieve a list of potential colliding objects from the game world
|
|
144
192
|
var candidates = world.broadphase.retrieve(line);
|
|
@@ -171,8 +219,8 @@ export function rayCast(line, result = []) {
|
|
|
171
219
|
shapeB
|
|
172
220
|
)) {
|
|
173
221
|
// we touched something !
|
|
174
|
-
result[
|
|
175
|
-
|
|
222
|
+
result[collisionCounter] = objB;
|
|
223
|
+
collisionCounter++;
|
|
176
224
|
}
|
|
177
225
|
indexB++;
|
|
178
226
|
} while (indexB < bLen);
|
|
@@ -180,7 +228,7 @@ export function rayCast(line, result = []) {
|
|
|
180
228
|
}
|
|
181
229
|
|
|
182
230
|
// cap result in case it was not empty
|
|
183
|
-
result.length =
|
|
231
|
+
result.length = collisionCounter;
|
|
184
232
|
|
|
185
233
|
// return the list of colliding objects
|
|
186
234
|
return result;
|