engine
engine copied to clipboard
Be able to activate a trigger entity with static rigidbody
Currently it's not possible to activate (trigger) a trigger entity with a static rigidbody. Sinds I use moving triggers (such as a bullet), it will be nice if these will be activated by a static rigidbody as well. (The reason why I use a trigger is because otherwise dynamic rigidbodies will be affected with a force when hit by for example the bullet).
My current work around is to use kinematic rigidbodies for all static objects, but performance wise this is not recommended.
I did a monkey patch to enable this that can be added project side:
var TriggerOnAll = pc.createScript('triggerOnAll');
// initialize code called once per entity
TriggerOnAll.prototype.initialize = function () {
this.entity.trigger.enable = function() {
const body = this.body;
if (!body) return;
const system = this.app.systems.rigidbody;
const idx = system._triggers.indexOf(this);
if (idx < 0) {
system.addBody(body, pc.BODYGROUP_TRIGGER, pc.BODYMASK_ALL ^ pc.BODYGROUP_TRIGGER);
system._triggers.push(this);
}
// set the body's activation state to active so that it is
// simulated properly again
body.forceActivationState(pc.BODYSTATE_ACTIVE_TAG);
this.updateTransform();
}.bind(this.entity.trigger)
// Remove and re-add the trigger
this.entity.collision.enabled = false
this.entity.collision.enabled = true
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// TriggerOnAll.prototype.swap = function(old) { };
// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/
Project for as long as it lives https://playcanvas.com/project/1327793/overview/triggers-with-static-objects