Search code examples
javascriptphysicsjs

PhysicsJs - how to remove a world behavior, "constant-acceration' after it has been added


My behaviors on initialization are added as follows:

world.add([
    Physics.behavior('interactive', { el: renderer.el }),
     Physics.behavior('constant-acceleration'),
    Physics.behavior('body-impulse-response'),
    Physics.behavior('sweep-prune'),
    edgeBounce
]);

I'd like to, at a later time, remove the "constant-acceleration" behavior. I read a couple of posts that said to use the remove() method but I'm not getting anything to happen using it like follows:

world.remove( Physics.behavior('constant-acceleration') );

Can anyone advise how I could achieve removing a specific behavior from the world after it has been added?


Solution

  • The Physics.behavior docs indicate that a Behavior object is returned when you call Physics.behavior (because it constructs a new one). So you need to keep a reference to the Behavior object you'd get back from the call you've put into your world.add array, then pass that reference to world.remove later. As it is now, you're making a new Behavior (separate from the one you made first) and immediately passing that brand new object to world.remove, which will basically do nothing.