@c-frame/aframe-physics-system
Version:
Physics system for A-Frame VR, built on Cannon.js & Ammo.js
45 lines (35 loc) • 1.1 kB
JavaScript
/**
* Velocity, in m/s.
*/
module.exports = AFRAME.registerComponent('velocity', {
schema: {type: 'vec3'},
init: function () {
this.system = this.el.sceneEl.systems.physics;
if (this.system) {
this.system.addComponent(this);
}
},
remove: function () {
if (this.system) {
this.system.removeComponent(this);
}
},
tick: function (t, dt) {
if (!dt) return;
if (this.system) return;
this.afterStep(t, dt);
},
afterStep: function (t, dt) {
if (!dt) return;
var physics = this.el.sceneEl.systems.physics || {data: {maxInterval: 1 / 60}},
// TODO - There's definitely a bug with getComputedAttribute and el.data.
velocity = this.el.getAttribute('velocity') || {x: 0, y: 0, z: 0},
position = this.el.object3D.position || {x: 0, y: 0, z: 0};
dt = Math.min(dt, physics.data.maxInterval * 1000);
this.el.object3D.position.set(
position.x + velocity.x * dt / 1000,
position.y + velocity.y * dt / 1000,
position.z + velocity.z * dt / 1000
);
}
});