@enable3d/ammo-physics
Advanced tools
Comparing version 0.0.16-alpha.2 to 0.0.16
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ import PhysicsBody from './physicsBody'; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -28,6 +28,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
Constraints.prototype.getTransform = function (body, targetBody) { | ||
var bodyTransform = body | ||
.getCenterOfMassTransform() | ||
.inverse() | ||
.op_mul(targetBody.getWorldTransform()); | ||
var bodyTransform = body.getCenterOfMassTransform().inverse().op_mul(targetBody.getWorldTransform()); | ||
var targetTransform = new Ammo.btTransform(); | ||
@@ -34,0 +31,0 @@ targetTransform.setIdentity(); |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @description This is a modified version of the original code from Kevin Lee | ||
@@ -5,0 +5,0 @@ */ |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @description This is a modified version of the original code from Kevin Lee | ||
@@ -6,0 +6,0 @@ */ |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ import EventEmitter from 'eventemitter3'; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -7,0 +7,0 @@ var __extends = (this && this.__extends) || (function () { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -21,9 +21,12 @@ import PhysicsBody from './physicsBody'; | ||
export { Types }; | ||
export { Clock } from './lib/Clock'; | ||
export { ServerClock } from '@enable3d/common/dist/serverClock'; | ||
interface AmmoPhysics extends Physics, Constraints, Shapes, Events { | ||
} | ||
declare class AmmoPhysics extends EventEmitter { | ||
scene: Scene; | ||
scene: Scene | 'headless'; | ||
config: Phaser3DConfig; | ||
tmpTrans: Ammo.btTransform; | ||
factory: Factories; | ||
isHeadless: boolean; | ||
protected rigidBodies: ExtendedObject3D[]; | ||
@@ -48,3 +51,3 @@ protected objectsAmmo: { | ||
protected defaultMaterial: DefaultMaterial; | ||
constructor(scene: Scene, config?: Phaser3DConfig); | ||
constructor(scene: Scene | 'headless', config?: Phaser3DConfig); | ||
get debug(): { | ||
@@ -54,3 +57,3 @@ enable: () => void; | ||
disable: () => void; | ||
}; | ||
} | null; | ||
private start; | ||
@@ -57,0 +60,0 @@ get add(): { |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -67,2 +67,7 @@ var __extends = (this && this.__extends) || (function () { | ||
exports.Types = Types; | ||
// Export THREE.Clock | ||
var Clock_1 = require("./lib/Clock"); | ||
exports.Clock = Clock_1.Clock; | ||
var serverClock_1 = require("@enable3d/common/dist/serverClock"); | ||
exports.ServerClock = serverClock_1.ServerClock; | ||
var AmmoPhysics = /** @class */ (function (_super) { | ||
@@ -78,2 +83,3 @@ __extends(AmmoPhysics, _super); | ||
_this.earlierDetectedCollisions = []; | ||
_this.isHeadless = scene === 'headless' ? true : false; | ||
_this.tmpEuler = new index_1.Euler(); | ||
@@ -84,5 +90,7 @@ _this.tmpQuaternion = new index_1.Quaternion(); | ||
_this.tmpBtQuaternion = new Ammo.btQuaternion(0, 0, 0, 1); | ||
_this.defaultMaterial = new defaultMaterial_1.default(); | ||
var version = "three.js version " + index_2.REVISION; | ||
console.log("%c %c %c %c %c " + version + " %c https://threejs.org/", 'background: #ff0000', 'background: #ffff00', 'background: #00ff00', 'background: #00ffff', 'color: #fff; background: #000000;', 'background: none'); | ||
if (scene !== 'headless') { | ||
_this.defaultMaterial = new defaultMaterial_1.default(); | ||
var version = "three.js version " + index_2.REVISION; | ||
console.log("%c %c %c %c %c " + version + " %c https://threejs.org/", 'background: #ff0000', 'background: #ffff00', 'background: #00ff00', 'background: #00ffff', 'color: #fff; background: #000000;', 'background: none'); | ||
} | ||
_this.factory = new factories_1.default(scene); | ||
@@ -99,2 +107,4 @@ _this.emptyV3 = new index_1.Vector3(); | ||
var _this = this; | ||
if (this.isHeadless) | ||
return null; | ||
return { | ||
@@ -101,0 +111,0 @@ enable: function () { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -11,4 +11,5 @@ import DebugDrawer from './debugDrawer'; | ||
import DefaultMaterial from '@enable3d/common/dist/defaultMaterial'; | ||
import PhysicsBody from './physicsBody'; | ||
declare class Physics extends EventEmitter { | ||
protected scene: Scene; | ||
protected scene: Scene | 'headless'; | ||
config: Phaser3DConfig; | ||
@@ -45,3 +46,5 @@ tmpTrans: Ammo.btTransform; | ||
protected defaultMaterial: DefaultMaterial; | ||
constructor(scene: Scene, config?: Phaser3DConfig); | ||
constructor(scene: Scene | 'headless', config?: Phaser3DConfig); | ||
/** Destroys a physics body. */ | ||
destroy(body: PhysicsBody): void; | ||
protected setup(): void; | ||
@@ -48,0 +51,0 @@ updateDebugger(): void; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -46,14 +46,47 @@ var __extends = (this && this.__extends) || (function () { | ||
} | ||
/** Destroys a physics body. */ | ||
Physics.prototype.destroy = function (body) { | ||
var _a; | ||
if (typeof (body === null || body === void 0 ? void 0 : body.ammo) === 'undefined') | ||
return; | ||
var ptr = Object.values(body.ammo)[0]; | ||
var name = Object.values(body.ammo)[1]; | ||
var obj = this.objectsAmmo[ptr]; | ||
// TODO: Remember why I track objectsAmmo and rigidBodies? | ||
// console.log(this.objectsAmmo) | ||
// console.log(this.rigidBodies) | ||
if (ptr && name && obj) { | ||
if ((_a = obj === null || obj === void 0 ? void 0 : obj.body) === null || _a === void 0 ? void 0 : _a.ammo) { | ||
// remove from physics world | ||
this.physicsWorld.removeRigidBody(obj.body.ammo); | ||
// reset properties | ||
obj.body = undefined; | ||
obj.hasBody = false; | ||
// remove from this.objectAmmo | ||
delete this.objectsAmmo[ptr]; | ||
// remove from this.rigidBodies | ||
for (var i = 0; i < this.rigidBodies.length; i++) { | ||
if (this.rigidBodies[i].name === name) { | ||
this.rigidBodies.splice(i, 1); | ||
i--; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
Physics.prototype.setup = function () { | ||
// Initialize convexBreaker | ||
// @ts-ignore | ||
this.convexBreaker = new convexObjectBreaker_1.ConvexObjectBreaker(); | ||
this.objectsToRemove = []; | ||
this.numObjectsToRemove = 0; | ||
for (var i = 0; i < 500; i++) { | ||
this.objectsToRemove[i] = null; | ||
if (this.scene !== 'headless') { | ||
// Initialize convexBreaker | ||
// @ts-ignore | ||
this.convexBreaker = new convexObjectBreaker_1.ConvexObjectBreaker(); | ||
this.objectsToRemove = []; | ||
this.numObjectsToRemove = 0; | ||
for (var i = 0; i < 500; i++) { | ||
this.objectsToRemove[i] = null; | ||
} | ||
} | ||
// setup ammo physics | ||
this.setupPhysicsWorld(); | ||
this.debugDrawer = new debugDrawer_1.default(this.scene, this.physicsWorld, {}); | ||
if (this.scene !== 'headless') | ||
this.debugDrawer = new debugDrawer_1.default(this.scene, this.physicsWorld, {}); | ||
/** | ||
@@ -75,2 +108,4 @@ * TODO add ghost object | ||
Physics.prototype.updateDebugger = function () { | ||
if (this.scene === 'headless') | ||
return; | ||
if (this.debugDrawer && this.debugDrawer.enabled) | ||
@@ -91,2 +126,4 @@ this.debugDrawer.update(); | ||
Physics.prototype.createDebrisFromBreakableObject = function (object, parent) { | ||
if (this.scene === 'headless') | ||
return; | ||
object.material = this.defaultMaterial.get(); | ||
@@ -107,2 +144,4 @@ object.shape = 'hull'; | ||
Physics.prototype.removeDebris = function (object) { | ||
if (this.scene === 'headless') | ||
return; | ||
this.scene.remove(object); | ||
@@ -125,42 +164,44 @@ this.physicsWorld.removeRigidBody(object.body.ammo); | ||
*/ | ||
for (var i = 0, il = this.rigidBodies.length; i < il; i++) { | ||
var objThree = this.rigidBodies[i]; | ||
var objPhys = objThree.body.ammo; | ||
var ms = objPhys.getMotionState(); | ||
if (ms) { | ||
ms.getWorldTransform(this.tmpTrans); | ||
// check if object did an update since last call | ||
if (objThree.body.didUpdate) { | ||
// @ts-ignore | ||
if (objThree.body._emitUpdateEvents) | ||
objThree.body.eventEmitter.emit('update'); | ||
objThree.body.didUpdate = false; | ||
if (this.scene !== 'headless') { | ||
for (var i = 0, il = this.rigidBodies.length; i < il; i++) { | ||
var objThree = this.rigidBodies[i]; | ||
var objPhys = objThree.body.ammo; | ||
var ms = objPhys.getMotionState(); | ||
if (ms) { | ||
ms.getWorldTransform(this.tmpTrans); | ||
// check if object did an update since last call | ||
if (objThree.body.didUpdate) { | ||
// @ts-ignore | ||
if (objThree.body._emitUpdateEvents) | ||
objThree.body.eventEmitter.emit('update'); | ||
objThree.body.didUpdate = false; | ||
} | ||
// update positions | ||
if (objThree.body.ammo.isKinematicObject() && objThree.body.needUpdate) { | ||
// get position and rotation | ||
objThree.getWorldQuaternion(this.tmpQuaternion); | ||
objThree.getWorldPosition(this.tmpVector3); | ||
// adjust tmp variables | ||
this.tmpBtVector3.setValue(this.tmpVector3.x, this.tmpVector3.y, this.tmpVector3.z); | ||
this.tmpBtQuaternion.setValue(this.tmpQuaternion.x, this.tmpQuaternion.y, this.tmpQuaternion.z, this.tmpQuaternion.w); | ||
// set position and rotation | ||
this.tmpTrans.setOrigin(this.tmpBtVector3); | ||
this.tmpTrans.setRotation(this.tmpBtQuaternion); | ||
// set transform | ||
ms.setWorldTransform(this.tmpTrans); | ||
// reset needsUpdate | ||
objThree.body.needUpdate = false; | ||
} | ||
else { | ||
// get position and rotation | ||
var p = this.tmpTrans.getOrigin(); | ||
var q = this.tmpTrans.getRotation(); | ||
// body offset | ||
var o = objThree.body.offset; | ||
// set position and rotation | ||
objThree.position.set(p.x() + o.x, p.y() + o.y, p.z() + o.z); | ||
objThree.quaternion.set(q.x(), q.y(), q.z(), q.w()); | ||
} | ||
objThree.body.collided = false; | ||
} | ||
// update positions | ||
if (objThree.body.ammo.isKinematicObject() && objThree.body.needUpdate) { | ||
// get position and rotation | ||
objThree.getWorldQuaternion(this.tmpQuaternion); | ||
objThree.getWorldPosition(this.tmpVector3); | ||
// adjust tmp variables | ||
this.tmpBtVector3.setValue(this.tmpVector3.x, this.tmpVector3.y, this.tmpVector3.z); | ||
this.tmpBtQuaternion.setValue(this.tmpQuaternion.x, this.tmpQuaternion.y, this.tmpQuaternion.z, this.tmpQuaternion.w); | ||
// set position and rotation | ||
this.tmpTrans.setOrigin(this.tmpBtVector3); | ||
this.tmpTrans.setRotation(this.tmpBtQuaternion); | ||
// set transform | ||
ms.setWorldTransform(this.tmpTrans); | ||
// reset needsUpdate | ||
objThree.body.needUpdate = false; | ||
} | ||
else { | ||
// get position and rotation | ||
var p = this.tmpTrans.getOrigin(); | ||
var q = this.tmpTrans.getRotation(); | ||
// body offset | ||
var o = objThree.body.offset; | ||
// set position and rotation | ||
objThree.position.set(p.x() + o.x, p.y() + o.y, p.z() + o.z); | ||
objThree.quaternion.set(q.x(), q.y(), q.z(), q.w()); | ||
} | ||
objThree.body.collided = false; | ||
} | ||
@@ -167,0 +208,0 @@ } |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -24,3 +24,6 @@ import { AmmoPhysics } from '.'; | ||
private _needUpdate; | ||
private tmpEuler; | ||
private tmpQuaternion; | ||
private tmpBtVector3; | ||
private tmpBtQuaternion; | ||
private eventEmitter; | ||
@@ -40,2 +43,20 @@ constructor(physics: AmmoPhysics, ammo: Ammo.btRigidBody); | ||
private onCollision; | ||
/** You have to call transform() before you can get or set the body's position or rotation. (for headless mode only) */ | ||
transform(): void; | ||
/** You have to call refresh() after you set the position or rotation of the body. (for headless mode only) */ | ||
refresh(): void; | ||
/** Set the rotation in radians. (for headless mode only) */ | ||
setRotation(x: number, y: number, z: number): void; | ||
/** Get the rotation in radians. (for headless mode only) */ | ||
get rotation(): { | ||
x: number; | ||
y: number; | ||
z: number; | ||
}; | ||
setPosition(x: number, y: number, z: number): void; | ||
get position(): { | ||
x: number; | ||
y: number; | ||
z: number; | ||
}; | ||
get velocity(): { | ||
@@ -42,0 +63,0 @@ x: number; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -12,2 +12,3 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | ||
var eventemitter3_1 = __importDefault(require("eventemitter3")); | ||
var index_1 = require("@enable3d/three-wrapper/dist/index"); | ||
var PhysicsBody = /** @class */ (function () { | ||
@@ -25,3 +26,6 @@ function PhysicsBody(physics, ammo) { | ||
this._needUpdate = false; | ||
this.tmpEuler = new index_1.Euler(); | ||
this.tmpQuaternion = new index_1.Quaternion(); | ||
this.tmpBtVector3 = new Ammo.btVector3(); | ||
this.tmpBtQuaternion = new Ammo.btQuaternion(0, 0, 0, 1); | ||
// @ts-ignore | ||
@@ -93,48 +97,56 @@ this.name = ammo.name; | ||
}; | ||
/** You have to call transform() before you can get or set the body's position or rotation. (for headless mode only) */ | ||
PhysicsBody.prototype.transform = function () { | ||
var t = this.physics.tmpTrans; | ||
this.ammo.getMotionState().getWorldTransform(t); | ||
}; | ||
/** You have to call refresh() after you set the position or rotation of the body. (for headless mode only) */ | ||
PhysicsBody.prototype.refresh = function () { | ||
var t = this.physics.tmpTrans; | ||
this.ammo.getMotionState().setWorldTransform(t); | ||
}; | ||
/** Set the rotation in radians. (for headless mode only) */ | ||
PhysicsBody.prototype.setRotation = function (x, y, z) { | ||
var e = this.tmpEuler.set(x, y, z); | ||
var q = this.tmpQuaternion.set(0, 0, 0, 1); | ||
q.setFromEuler(e); | ||
this.tmpBtQuaternion.setValue(0, 0, 0, 1); | ||
var ammoQuat = this.tmpBtQuaternion; | ||
ammoQuat.setValue(q.x, q.y, q.z, q.w); | ||
var t = this.physics.tmpTrans; | ||
t.setRotation(ammoQuat); | ||
}; | ||
Object.defineProperty(PhysicsBody.prototype, "rotation", { | ||
/** Get the rotation in radians. (for headless mode only) */ | ||
get: function () { | ||
var t = this.physics.tmpTrans; | ||
var ammoQuat = t.getRotation(); | ||
var q = this.tmpQuaternion.set(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w()); | ||
var qx = q.x; | ||
var qy = q.y; | ||
var qz = q.z; | ||
var qw = q.w; | ||
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm | ||
var angle = 2 * Math.acos(qw); | ||
var x = qx / Math.sqrt(1 - qw * qw); | ||
var y = qy / Math.sqrt(1 - qw * qw); | ||
var z = qz / Math.sqrt(1 - qw * qw); | ||
return { x: x * angle || 0, y: y * angle || 0, z: z * angle || 0 }; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
PhysicsBody.prototype.setPosition = function (x, y, z) { | ||
var t = this.physics.tmpTrans; | ||
t.getOrigin().setValue(x, y, z); | ||
}; | ||
Object.defineProperty(PhysicsBody.prototype, "position", { | ||
get: function () { | ||
var t = this.physics.tmpTrans; | ||
return { x: t.getOrigin().x(), y: t.getOrigin().y(), z: t.getOrigin().z() }; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(PhysicsBody.prototype, "velocity", { | ||
// /** You have to call transform() before you can get or set the body's position or rotation. */ | ||
// public transform() { | ||
// const t = this.physics.tmpTrans | ||
// this.ammo.getMotionState().getWorldTransform(t) | ||
// } | ||
// /** You have to call refresh() after you set the position or rotation of the body. */ | ||
// public refresh() { | ||
// const t = this.physics.tmpTrans | ||
// this.ammo.getMotionState().setWorldTransform(t) | ||
// } | ||
// /** Set the rotation in radians. */ | ||
// public setRotation(x: number, y: number, z: number) { | ||
// const e = this.tmpEuler.set(x, y, z) | ||
// const q = this.tmpQuaternion.set(0, 0, 0, 1) | ||
// q.setFromEuler(e) | ||
// this.tmpBtQuaternion.setValue(0, 0, 0, 1) | ||
// const ammoQuat = this.tmpBtQuaternion | ||
// ammoQuat.setValue(q.x, q.y, q.z, q.w) | ||
// const t = this.physics.tmpTrans | ||
// t.setRotation(ammoQuat) | ||
// } | ||
// /** Get the rotation in radians. */ | ||
// public getRotation() { | ||
// const t = this.physics.tmpTrans | ||
// const ammoQuat = t.getRotation() | ||
// const q = this.tmpQuaternion.set(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w()) | ||
// const qx = q.x | ||
// const qy = q.y | ||
// const qz = q.z | ||
// const qw = q.w | ||
// // https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm | ||
// const angle = 2 * Math.acos(qw) | ||
// const x = qx / Math.sqrt(1 - qw * qw) | ||
// const y = qy / Math.sqrt(1 - qw * qw) | ||
// const z = qz / Math.sqrt(1 - qw * qw) | ||
// return { x: x * angle || 0, y: y * angle || 0, z: z * angle || 0 } | ||
// } | ||
// public setPosition(x: number, y: number, z: number) { | ||
// const t = this.physics.tmpTrans | ||
// t.getOrigin().setValue(x, y, z) | ||
// } | ||
// public getPosition() { | ||
// const t = this.physics.tmpTrans | ||
// return { x: t.getOrigin().x(), y: t.getOrigin().y(), z: t.getOrigin().z() } | ||
// } | ||
get: function () { | ||
@@ -141,0 +153,0 @@ return { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ import { SphereConfig, MaterialConfig, BoxConfig, GroundConfig, CylinderConfig, ExtendedObject3D, ExtrudeConfig, TorusConfig, AddExistingConfig, PlaneConfig, ConeConfig } from '@enable3d/common/dist/types'; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -7,0 +7,0 @@ var __assign = (this && this.__assign) || function () { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @description This is a modified version of the original code from Kevin Lee | ||
@@ -628,12 +628,6 @@ */ | ||
var halfExtents = new index_1.Vector3(); | ||
return halfExtents | ||
.subVectors(bounds.max, bounds.min) | ||
.multiplyScalar(0.5) | ||
.clampScalar(minHalfExtent, maxHalfExtent); | ||
return halfExtents.subVectors(bounds.max, bounds.min).multiplyScalar(0.5).clampScalar(minHalfExtent, maxHalfExtent); | ||
}; | ||
var _computeLocalOffset = function (matrix, bounds, target) { | ||
target | ||
.addVectors(bounds.max, bounds.min) | ||
.multiplyScalar(0.5) | ||
.applyMatrix4(matrix); | ||
target.addVectors(bounds.max, bounds.min).multiplyScalar(0.5).applyMatrix4(matrix); | ||
return target; | ||
@@ -640,0 +634,0 @@ }; |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ import { Quaternion } from '@enable3d/three-wrapper/dist/index'; |
"use strict"; | ||
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -7,0 +7,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
{ | ||
"name": "@enable3d/ammo-physics", | ||
"version": "0.0.16-alpha.2", | ||
"version": "0.0.16", | ||
"description": "Physics Plugin for three.js", | ||
@@ -26,14 +26,14 @@ "main": "dist/index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/yandeu/enable3d.git", | ||
"url": "git+https://github.com/enable3d/enable3d.git", | ||
"directory": "packages/phaserExtension" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/yandeu/enable3d/issues" | ||
"url": "https://github.com/enable3d/enable3d/issues" | ||
}, | ||
"homepage": "https://enable3d.io", | ||
"dependencies": { | ||
"@enable3d/common": "^0.0.16-alpha.2", | ||
"@enable3d/three-wrapper": "^0.0.16-alpha.2", | ||
"@enable3d/common": "^0.0.16", | ||
"@enable3d/three-wrapper": "^0.0.16", | ||
"eventemitter3": "^4.0.0" | ||
} | ||
} |
@@ -6,3 +6,3 @@ <h1 align="center"> | ||
<h4 align="center"> | ||
Written in TypeScript, adds Physics to your three.js project using ammo.js | ||
Written in TypeScript, adds Physics to your three.js project using ammo.js. | ||
</h4> | ||
@@ -12,3 +12,3 @@ | ||
<a href="https://www.npmjs.com/package/@enable3d/ammo-physics"><img src="https://img.shields.io/npm/v/@enable3d/ammo-physics?style=flat-square" alt="NPM version"></a> | ||
<a href="https://github.com/yandeu/enable3d/commits/master"><img src="https://img.shields.io/github/last-commit/yandeu/enable3d.svg?style=flat-square" alt="GitHub last commit"></a> | ||
<a href="https://github.com/enable3d/enable3d/commits/master"><img src="https://img.shields.io/github/last-commit/yandeu/enable3d.svg?style=flat-square" alt="GitHub last commit"></a> | ||
<a href="https://github.com/prettier/prettier" alt="code style: prettier"><img src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"></a> | ||
@@ -15,0 +15,0 @@ <a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/built%20with-TypeScript-blue?style=flat-square"></a> |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -54,6 +54,3 @@ | ||
private getTransform(body: Ammo.btRigidBody, targetBody: Ammo.btRigidBody) { | ||
const bodyTransform = body | ||
.getCenterOfMassTransform() | ||
.inverse() | ||
.op_mul(targetBody.getWorldTransform()) | ||
const bodyTransform = body.getCenterOfMassTransform().inverse().op_mul(targetBody.getWorldTransform()) | ||
const targetTransform = new Ammo.btTransform() | ||
@@ -60,0 +57,0 @@ targetTransform.setIdentity() |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @description This is a modified version of the original code from Kevin Lee | ||
@@ -5,0 +5,0 @@ */ |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -42,2 +42,6 @@ | ||
// Export THREE.Clock | ||
export { Clock } from './lib/Clock' | ||
export { ServerClock } from '@enable3d/common/dist/serverClock' | ||
interface AmmoPhysics extends Physics, Constraints, Shapes, Events {} | ||
@@ -48,2 +52,3 @@ | ||
public factory: Factories | ||
public isHeadless: boolean | ||
@@ -63,5 +68,6 @@ protected rigidBodies: ExtendedObject3D[] = [] | ||
constructor(public scene: Scene, public config: Phaser3DConfig = {}) { | ||
constructor(public scene: Scene | 'headless', public config: Phaser3DConfig = {}) { | ||
super() | ||
this.isHeadless = scene === 'headless' ? true : false | ||
this.tmpEuler = new Euler() | ||
@@ -73,14 +79,16 @@ this.tmpQuaternion = new Quaternion() | ||
this.defaultMaterial = new DefaultMaterial() | ||
if (scene !== 'headless') { | ||
this.defaultMaterial = new DefaultMaterial() | ||
const version = `three.js version ${REVISION}` | ||
console.log( | ||
`%c %c %c %c %c ${version} %c https://threejs.org/`, | ||
'background: #ff0000', | ||
'background: #ffff00', | ||
'background: #00ff00', | ||
'background: #00ffff', | ||
'color: #fff; background: #000000;', | ||
'background: none' | ||
) | ||
const version = `three.js version ${REVISION}` | ||
console.log( | ||
`%c %c %c %c %c ${version} %c https://threejs.org/`, | ||
'background: #ff0000', | ||
'background: #ffff00', | ||
'background: #00ff00', | ||
'background: #00ffff', | ||
'color: #fff; background: #000000;', | ||
'background: none' | ||
) | ||
} | ||
@@ -99,2 +107,4 @@ this.factory = new Factories(scene) | ||
public get debug() { | ||
if (this.isHeadless) return null | ||
return { | ||
@@ -101,0 +111,0 @@ enable: () => { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -13,2 +13,3 @@ | ||
import DefaultMaterial from '@enable3d/common/dist/defaultMaterial' | ||
import PhysicsBody from './physicsBody' | ||
@@ -41,15 +42,51 @@ class Physics extends EventEmitter { | ||
constructor(protected scene: Scene, public config: Phaser3DConfig = {}) { | ||
constructor(protected scene: Scene | 'headless', public config: Phaser3DConfig = {}) { | ||
super() | ||
} | ||
/** Destroys a physics body. */ | ||
public destroy(body: PhysicsBody) { | ||
if (typeof body?.ammo === 'undefined') return | ||
const ptr = Object.values(body.ammo)[0] | ||
const name = Object.values(body.ammo)[1] | ||
const obj = this.objectsAmmo[ptr] | ||
// TODO: Remember why I track objectsAmmo and rigidBodies? | ||
// console.log(this.objectsAmmo) | ||
// console.log(this.rigidBodies) | ||
if (ptr && name && obj) { | ||
if (obj?.body?.ammo) { | ||
// remove from physics world | ||
this.physicsWorld.removeRigidBody(obj.body.ammo) | ||
// reset properties | ||
obj.body = undefined | ||
obj.hasBody = false | ||
// remove from this.objectAmmo | ||
delete this.objectsAmmo[ptr] | ||
// remove from this.rigidBodies | ||
for (var i = 0; i < this.rigidBodies.length; i++) { | ||
if (this.rigidBodies[i].name === name) { | ||
this.rigidBodies.splice(i, 1) | ||
i-- | ||
} | ||
} | ||
} | ||
} | ||
} | ||
protected setup() { | ||
// Initialize convexBreaker | ||
// @ts-ignore | ||
this.convexBreaker = new ConvexObjectBreaker() | ||
if (this.scene !== 'headless') { | ||
// Initialize convexBreaker | ||
// @ts-ignore | ||
this.convexBreaker = new ConvexObjectBreaker() | ||
this.objectsToRemove = [] | ||
this.numObjectsToRemove = 0 | ||
for (var i = 0; i < 500; i++) { | ||
this.objectsToRemove[i] = null | ||
this.objectsToRemove = [] | ||
this.numObjectsToRemove = 0 | ||
for (var i = 0; i < 500; i++) { | ||
this.objectsToRemove[i] = null | ||
} | ||
} | ||
@@ -60,3 +97,3 @@ | ||
this.debugDrawer = new DebugDrawer(this.scene, this.physicsWorld, {}) | ||
if (this.scene !== 'headless') this.debugDrawer = new DebugDrawer(this.scene, this.physicsWorld, {}) | ||
@@ -81,2 +118,4 @@ /** | ||
public updateDebugger() { | ||
if (this.scene === 'headless') return | ||
if (this.debugDrawer && this.debugDrawer.enabled) this.debugDrawer.update() | ||
@@ -100,2 +139,4 @@ } | ||
private createDebrisFromBreakableObject(object: ExtendedObject3D, parent: ExtendedObject3D) { | ||
if (this.scene === 'headless') return | ||
object.material = this.defaultMaterial.get() | ||
@@ -120,2 +161,4 @@ object.shape = 'hull' | ||
private removeDebris(object: any) { | ||
if (this.scene === 'headless') return | ||
this.scene.remove(object) | ||
@@ -140,49 +183,51 @@ this.physicsWorld.removeRigidBody(object.body.ammo) | ||
*/ | ||
for (var i = 0, il = this.rigidBodies.length; i < il; i++) { | ||
var objThree = this.rigidBodies[i] | ||
var objPhys = objThree.body.ammo | ||
var ms = objPhys.getMotionState() | ||
if (this.scene !== 'headless') { | ||
for (var i = 0, il = this.rigidBodies.length; i < il; i++) { | ||
var objThree = this.rigidBodies[i] | ||
var objPhys = objThree.body.ammo | ||
var ms = objPhys.getMotionState() | ||
if (ms) { | ||
ms.getWorldTransform(this.tmpTrans) | ||
if (ms) { | ||
ms.getWorldTransform(this.tmpTrans) | ||
// check if object did an update since last call | ||
if (objThree.body.didUpdate) { | ||
// @ts-ignore | ||
if (objThree.body._emitUpdateEvents) objThree.body.eventEmitter.emit('update') | ||
objThree.body.didUpdate = false | ||
} | ||
// check if object did an update since last call | ||
if (objThree.body.didUpdate) { | ||
// @ts-ignore | ||
if (objThree.body._emitUpdateEvents) objThree.body.eventEmitter.emit('update') | ||
objThree.body.didUpdate = false | ||
} | ||
// update positions | ||
if (objThree.body.ammo.isKinematicObject() && objThree.body.needUpdate) { | ||
// get position and rotation | ||
objThree.getWorldQuaternion(this.tmpQuaternion) | ||
objThree.getWorldPosition(this.tmpVector3) | ||
// adjust tmp variables | ||
this.tmpBtVector3.setValue(this.tmpVector3.x, this.tmpVector3.y, this.tmpVector3.z) | ||
this.tmpBtQuaternion.setValue( | ||
this.tmpQuaternion.x, | ||
this.tmpQuaternion.y, | ||
this.tmpQuaternion.z, | ||
this.tmpQuaternion.w | ||
) | ||
// set position and rotation | ||
this.tmpTrans.setOrigin(this.tmpBtVector3) | ||
this.tmpTrans.setRotation(this.tmpBtQuaternion) | ||
// set transform | ||
ms.setWorldTransform(this.tmpTrans) | ||
// reset needsUpdate | ||
objThree.body.needUpdate = false | ||
} else { | ||
// get position and rotation | ||
let p = this.tmpTrans.getOrigin() | ||
let q = this.tmpTrans.getRotation() | ||
// body offset | ||
let o = objThree.body.offset | ||
// set position and rotation | ||
objThree.position.set(p.x() + o.x, p.y() + o.y, p.z() + o.z) | ||
objThree.quaternion.set(q.x(), q.y(), q.z(), q.w()) | ||
// update positions | ||
if (objThree.body.ammo.isKinematicObject() && objThree.body.needUpdate) { | ||
// get position and rotation | ||
objThree.getWorldQuaternion(this.tmpQuaternion) | ||
objThree.getWorldPosition(this.tmpVector3) | ||
// adjust tmp variables | ||
this.tmpBtVector3.setValue(this.tmpVector3.x, this.tmpVector3.y, this.tmpVector3.z) | ||
this.tmpBtQuaternion.setValue( | ||
this.tmpQuaternion.x, | ||
this.tmpQuaternion.y, | ||
this.tmpQuaternion.z, | ||
this.tmpQuaternion.w | ||
) | ||
// set position and rotation | ||
this.tmpTrans.setOrigin(this.tmpBtVector3) | ||
this.tmpTrans.setRotation(this.tmpBtQuaternion) | ||
// set transform | ||
ms.setWorldTransform(this.tmpTrans) | ||
// reset needsUpdate | ||
objThree.body.needUpdate = false | ||
} else { | ||
// get position and rotation | ||
let p = this.tmpTrans.getOrigin() | ||
let q = this.tmpTrans.getRotation() | ||
// body offset | ||
let o = objThree.body.offset | ||
// set position and rotation | ||
objThree.position.set(p.x() + o.x, p.y() + o.y, p.z() + o.z) | ||
objThree.quaternion.set(q.x(), q.y(), q.z(), q.w()) | ||
} | ||
objThree.body.collided = false | ||
} | ||
objThree.body.collided = false | ||
} | ||
@@ -189,0 +234,0 @@ } |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -11,2 +11,3 @@ | ||
import EventEmitter from 'eventemitter3' | ||
import { Euler, Quaternion } from '@enable3d/three-wrapper/dist/index' | ||
@@ -24,3 +25,8 @@ class PhysicsBody { | ||
private _needUpdate = false | ||
private tmpEuler = new Euler() | ||
private tmpQuaternion = new Quaternion() | ||
private tmpBtVector3 = new Ammo.btVector3() | ||
private tmpBtQuaternion = new Ammo.btQuaternion(0, 0, 0, 1) | ||
private eventEmitter: EventEmitter | ||
@@ -84,57 +90,57 @@ | ||
// /** You have to call transform() before you can get or set the body's position or rotation. */ | ||
// public transform() { | ||
// const t = this.physics.tmpTrans | ||
// this.ammo.getMotionState().getWorldTransform(t) | ||
// } | ||
/** You have to call transform() before you can get or set the body's position or rotation. (for headless mode only) */ | ||
public transform() { | ||
const t = this.physics.tmpTrans | ||
this.ammo.getMotionState().getWorldTransform(t) | ||
} | ||
// /** You have to call refresh() after you set the position or rotation of the body. */ | ||
// public refresh() { | ||
// const t = this.physics.tmpTrans | ||
// this.ammo.getMotionState().setWorldTransform(t) | ||
// } | ||
/** You have to call refresh() after you set the position or rotation of the body. (for headless mode only) */ | ||
public refresh() { | ||
const t = this.physics.tmpTrans | ||
this.ammo.getMotionState().setWorldTransform(t) | ||
} | ||
// /** Set the rotation in radians. */ | ||
// public setRotation(x: number, y: number, z: number) { | ||
// const e = this.tmpEuler.set(x, y, z) | ||
// const q = this.tmpQuaternion.set(0, 0, 0, 1) | ||
// q.setFromEuler(e) | ||
/** Set the rotation in radians. (for headless mode only) */ | ||
public setRotation(x: number, y: number, z: number) { | ||
const e = this.tmpEuler.set(x, y, z) | ||
const q = this.tmpQuaternion.set(0, 0, 0, 1) | ||
q.setFromEuler(e) | ||
// this.tmpBtQuaternion.setValue(0, 0, 0, 1) | ||
// const ammoQuat = this.tmpBtQuaternion | ||
// ammoQuat.setValue(q.x, q.y, q.z, q.w) | ||
this.tmpBtQuaternion.setValue(0, 0, 0, 1) | ||
const ammoQuat = this.tmpBtQuaternion | ||
ammoQuat.setValue(q.x, q.y, q.z, q.w) | ||
// const t = this.physics.tmpTrans | ||
// t.setRotation(ammoQuat) | ||
// } | ||
const t = this.physics.tmpTrans | ||
t.setRotation(ammoQuat) | ||
} | ||
// /** Get the rotation in radians. */ | ||
// public getRotation() { | ||
// const t = this.physics.tmpTrans | ||
// const ammoQuat = t.getRotation() | ||
// const q = this.tmpQuaternion.set(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w()) | ||
/** Get the rotation in radians. (for headless mode only) */ | ||
public get rotation() { | ||
const t = this.physics.tmpTrans | ||
const ammoQuat = t.getRotation() | ||
const q = this.tmpQuaternion.set(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w()) | ||
// const qx = q.x | ||
// const qy = q.y | ||
// const qz = q.z | ||
// const qw = q.w | ||
const qx = q.x | ||
const qy = q.y | ||
const qz = q.z | ||
const qw = q.w | ||
// // https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm | ||
// const angle = 2 * Math.acos(qw) | ||
// const x = qx / Math.sqrt(1 - qw * qw) | ||
// const y = qy / Math.sqrt(1 - qw * qw) | ||
// const z = qz / Math.sqrt(1 - qw * qw) | ||
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm | ||
const angle = 2 * Math.acos(qw) | ||
const x = qx / Math.sqrt(1 - qw * qw) | ||
const y = qy / Math.sqrt(1 - qw * qw) | ||
const z = qz / Math.sqrt(1 - qw * qw) | ||
// return { x: x * angle || 0, y: y * angle || 0, z: z * angle || 0 } | ||
// } | ||
return { x: x * angle || 0, y: y * angle || 0, z: z * angle || 0 } | ||
} | ||
// public setPosition(x: number, y: number, z: number) { | ||
// const t = this.physics.tmpTrans | ||
// t.getOrigin().setValue(x, y, z) | ||
// } | ||
public setPosition(x: number, y: number, z: number) { | ||
const t = this.physics.tmpTrans | ||
t.getOrigin().setValue(x, y, z) | ||
} | ||
// public getPosition() { | ||
// const t = this.physics.tmpTrans | ||
// return { x: t.getOrigin().x(), y: t.getOrigin().y(), z: t.getOrigin().z() } | ||
// } | ||
public get position() { | ||
const t = this.physics.tmpTrans | ||
return { x: t.getOrigin().x(), y: t.getOrigin().y(), z: t.getOrigin().z() } | ||
} | ||
@@ -141,0 +147,0 @@ public get velocity() { |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @description This is a modified version of the original code from Kevin Lee | ||
@@ -45,3 +45,3 @@ */ | ||
export const createCollisionShapes = function(root, options) { | ||
export const createCollisionShapes = function (root, options) { | ||
switch (options.type) { | ||
@@ -76,3 +76,3 @@ case TYPE.BOX: | ||
export const createBoxShape = function(root, options) { | ||
export const createBoxShape = function (root, options) { | ||
options.type = TYPE.BOX | ||
@@ -98,3 +98,3 @@ _setOptions(options) | ||
export const createCylinderShape = function(root, options) { | ||
export const createCylinderShape = function (root, options) { | ||
options.type = TYPE.CYLINDER | ||
@@ -130,3 +130,3 @@ _setOptions(options) | ||
export const createCapsuleShape = function(root, options) { | ||
export const createCapsuleShape = function (root, options) { | ||
options.type = TYPE.CAPSULE | ||
@@ -161,3 +161,3 @@ _setOptions(options) | ||
export const createConeShape = function(root, options) { | ||
export const createConeShape = function (root, options) { | ||
options.type = TYPE.CONE | ||
@@ -192,3 +192,3 @@ _setOptions(options) | ||
export const createSphereShape = function(root, options) { | ||
export const createSphereShape = function (root, options) { | ||
options.type = TYPE.SPHERE | ||
@@ -210,6 +210,6 @@ _setOptions(options) | ||
export const createHullShape = (function() { | ||
export const createHullShape = (function () { | ||
const vertex = new Vector3() | ||
const center = new Vector3() | ||
return function(root, options) { | ||
return function (root, options) { | ||
options.type = TYPE.HULL | ||
@@ -278,6 +278,6 @@ _setOptions(options) | ||
export const createHACDShapes = (function() { | ||
export const createHACDShapes = (function () { | ||
const v = new Vector3() | ||
const center = new Vector3() | ||
return function(root, options) { | ||
return function (root, options) { | ||
options.type = TYPE.HACD | ||
@@ -388,6 +388,6 @@ _setOptions(options) | ||
export const createVHACDShapes = (function() { | ||
export const createVHACDShapes = (function () { | ||
const v = new Vector3() | ||
const center = new Vector3() | ||
return function(root, options) { | ||
return function (root, options) { | ||
options.type = TYPE.VHACD | ||
@@ -511,7 +511,7 @@ _setOptions(options) | ||
export const createTriMeshShape = (function() { | ||
export const createTriMeshShape = (function () { | ||
const va = new Vector3() | ||
const vb = new Vector3() | ||
const vc = new Vector3() | ||
return function(root, options) { | ||
return function (root, options) { | ||
options.type = TYPE.MESH | ||
@@ -575,3 +575,3 @@ _setOptions(options) | ||
export const createHeightfieldTerrainShape = function(root, options) { | ||
export const createHeightfieldTerrainShape = function (root, options) { | ||
_setOptions(options) | ||
@@ -658,3 +658,3 @@ | ||
const _finishCollisionShape = function(collisionShape, options, scale) { | ||
const _finishCollisionShape = function (collisionShape, options, scale) { | ||
collisionShape.type = options.type | ||
@@ -693,7 +693,7 @@ collisionShape.setMargin(options.margin) | ||
// `transform` is the transform required to transform the given geometry's vertices into root-local space. | ||
const _iterateGeometries = (function() { | ||
const _iterateGeometries = (function () { | ||
const transform = new Matrix4() | ||
const inverse = new Matrix4() | ||
const bufferGeometry = new BufferGeometry() | ||
return function(root, options, cb) { | ||
return function (root, options, cb) { | ||
inverse.getInverse(root.matrixWorld) | ||
@@ -720,3 +720,3 @@ root.traverse(mesh => { | ||
const _computeScale = function(root, options) { | ||
const _computeScale = function (root, options) { | ||
// const scale = new Vector3(1, 1, 1) | ||
@@ -731,6 +731,6 @@ // if (options.fit === FIT.ALL) { | ||
const _computeRadius = (function() { | ||
const _computeRadius = (function () { | ||
const v = new Vector3() | ||
const center = new Vector3() | ||
return function(root, options, bounds) { | ||
return function (root, options, bounds) { | ||
let maxRadiusSq = 0 | ||
@@ -752,15 +752,9 @@ let { x: cx, y: cy, z: cz } = bounds.getCenter(center) | ||
const _computeHalfExtents = function(root, bounds, minHalfExtent, maxHalfExtent) { | ||
const _computeHalfExtents = function (root, bounds, minHalfExtent, maxHalfExtent) { | ||
const halfExtents = new Vector3() | ||
return halfExtents | ||
.subVectors(bounds.max, bounds.min) | ||
.multiplyScalar(0.5) | ||
.clampScalar(minHalfExtent, maxHalfExtent) | ||
return halfExtents.subVectors(bounds.max, bounds.min).multiplyScalar(0.5).clampScalar(minHalfExtent, maxHalfExtent) | ||
} | ||
const _computeLocalOffset = function(matrix, bounds, target) { | ||
target | ||
.addVectors(bounds.max, bounds.min) | ||
.multiplyScalar(0.5) | ||
.applyMatrix4(matrix) | ||
const _computeLocalOffset = function (matrix, bounds, target) { | ||
target.addVectors(bounds.max, bounds.min).multiplyScalar(0.5).applyMatrix4(matrix) | ||
return target | ||
@@ -770,5 +764,5 @@ } | ||
// returns the bounding box for the geometries underneath `root`. | ||
const _computeBounds = (function() { | ||
const _computeBounds = (function () { | ||
const v = new Vector3() | ||
return function(root, options) { | ||
return function (root, options) { | ||
const bounds = new Box3() | ||
@@ -775,0 +769,0 @@ let minX = +Infinity |
/** | ||
* @author Yannick Deubel (https://github.com/yandeu) | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/yandeu/enable3d | ||
* @license {@link https://github.com/yandeu/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
* @copyright Copyright (c) 2020 Yannick Deubel; Project Url: https://github.com/enable3d/enable3d | ||
* @license {@link https://github.com/enable3d/enable3d/blob/master/LICENSE|GNU GPLv3} | ||
*/ | ||
@@ -6,0 +6,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3518985
69
16127
Updated@enable3d/common@^0.0.16