arcade-physics
Version:
Use Arcade Physics without Phaser.
81 lines • 3.57 kB
JavaScript
;
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeparateX = void 0;
const GetOverlapX_1 = require("./GetOverlapX");
const ProcessX = __importStar(require("./ProcessX"));
/**
* Separates two overlapping bodies on the X-axis (horizontally).
*
* Separation involves moving two overlapping bodies so they don't overlap anymore and adjusting their velocities based on their mass. This is a core part of collision detection.
*
* The bodies won't be separated if there is no horizontal overlap between them, if they are static, or if either one uses custom logic for its separation.
*
* @function Phaser.Physics.Arcade.SeparateX
* @since 3.0.0
*
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
* @param {boolean} overlapOnly - If `true`, the bodies will only have their overlap data set and no separation will take place.
* @param {number} bias - A value to add to the delta value during overlap checking. Used to prevent sprite tunneling.
*
* @return {boolean} `true` if the two bodies overlap vertically, otherwise `false`.
*/
const SeparateX = (body1, body2, overlapOnly, bias) => {
const overlap = (0, GetOverlapX_1.GetOverlapX)(body1, body2, overlapOnly, bias);
const body1Immovable = body1.immovable;
const body2Immovable = body2.immovable;
// Can't separate two immovable bodies, or a body with its own custom separation logic
if (overlapOnly ||
overlap === 0 ||
(body1Immovable && body2Immovable) ||
body1.customSeparateX ||
body2.customSeparateX) {
// return true if there was some overlap, otherwise false
return overlap !== 0 || (body1.embedded && body2.embedded);
}
const blockedState = ProcessX.Set(body1, body2, overlap);
if (!body1Immovable && !body2Immovable) {
if (blockedState > 0) {
return true;
}
return ProcessX.Check();
}
else if (body1Immovable) {
ProcessX.RunImmovableBody1(blockedState);
}
else if (body2Immovable) {
ProcessX.RunImmovableBody2(blockedState);
}
// If we got this far then there WAS overlap, and separation is complete, so return true
return true;
};
exports.SeparateX = SeparateX;
//# sourceMappingURL=SeparateX.js.map