I created an entity.mom
property for shorthand access to the entity.rigidbody.linearVelocity
property, but I was not successful and the entity acts as though there’s no velocity. No errors are shown; it just simply does not work. My project is here: https://playcanvas.com/editor/scene/578037
This is my code:
var Momentum = pc.createScript('momentum');
//Check if exists
function is(object) {return typeof(object) != "undefined";}
pc.Entity.prototype.ResetMomentum = function() {
if (this.rigidbody) {
this.mom = [];
var vect = this.rigidbody.linearVelocity;
this.mom.x = vect.x;
this.mom.y = vect.y;
this.mom.z = vect.z;
}
};
// update code called every frame
Momentum.prototype.update = function(dt) {
var entity = this.entity;
if (is(entity.mom)) {
//Something's wrong with entity.mom -------------------------
//
if (typeof(entity.mom) != "object" || typeof(entity.mom.x) != "number" || typeof(entity.mom.y) != "number" || typeof(entity.mom.z) != "number") {
console.error("entity.mom must be an object with properties x, y, and z as numbers.");
//Momentum is invalid, so create it again
entity.ResetMomentum();
}
//
// -----------------------------------------------------------
//Set the velocity
entity.rigidbody.linearVelocity = new pc.Vec3(entity.mom.x, entity.mom.y, entity.mom.z);
}
//Make momentum consistent with velocity
entity.ResetMomentum();
// -----------------------------------------------------------------------------
//Do stuff with entity.mom
};