@threlte/rapier
Version:
Components and hooks to use the Rapier physics engine in Threlte
32 lines (31 loc) • 1.3 kB
JavaScript
const overrideMethods = {
position: ['setTranslation', 'setNextKinematicTranslation'],
rotation: ['setRotation', 'setNextKinematicRotation']
};
/**
* When using a fixed framerate, Threlte is interpolating the position and
* rotation of RigidBody objects. Sometimes, this is not desirable, especially
* when using methods on the rigidbody that teleport the object, e.g. should
* lead to a sudden change in translation/rotation such as `rb.setTranslation`.
* These methods are overridden to reset the physics simulation position and
* rotation to the current object position and rotation.
*/
export const overrideTeleportMethods = (rb, object) => {
const originalMethods = {};
overrideMethods.position.forEach((method) => {
originalMethods[method] = rb[method].bind(rb);
const proxy = (...args) => {
object.userData.physics.resetPosition = true;
return originalMethods[method](...args);
};
rb[method] = proxy;
});
overrideMethods.rotation.forEach((method) => {
originalMethods[method] = rb[method].bind(rb);
const proxy = (...args) => {
object.userData.physics.resetRotation = true;
return originalMethods[method](...args);
};
rb[method] = proxy;
});
};