How do I reset a force applied to an object?

Hey, just a noob question here but I need a way to reset the force applied to an object’s physics impostor with applyForce(). I’m using the CannonJSPlugin physics plugin.

I don’t think this is really necessary but here’s my code anyway: https://playground.babylonjs.com/indexStable.html#RGV550#11

As you can (hopefully) see, I’m trying to move the sphere the direction the camera is facing every time “W” is pressed, and then I invert the force when they let go of W. This works, but it would be a lot nicer if there’s a way to just reset the force applied to an object in all directions to 0. Thanks!

Edit: Found a solution to my issue. https://playground.babylonjs.com/indexStable.html#RGV550#12. It would still be nice to be able to reset the force if anyone knows of an easy way to do that

Hi @Coming_Soon And welcome to the forum :slight_smile:

Did you try to set velocity and angularVelocity to 0?

I believe @Cedric has the answer you’re looking for. Buffer the values you want to represent the original state (if necessary), and you can then apply these values at any time.

mesh.physicsImpostor.setAngularVelocity( BABYLON.Vector3.Zero() );
mesh.physicsImpostor.setLinearVelocity( BABYLON.Vector3.Zero() );

var x = new BABYLON.Vector3(0, 0, 0);
var a = Math.PI / 8;
var rot = new BABYLON.Quaternion.RotationAxis(x, a);
mesh.rotationQuaternion = rot; 

Galen

2 Likes

Hi guys! Welcome @Coming_Soon … pretty nice physics work… good job! I like it!

Experiments: https://playground.babylonjs.com/#RGV550#14

Up there, I’m goofing around with linearVelocity, angularVelocity, and damping. I made quite a few modifications… and left notes. :slight_smile:

CS - notice that .setAngularVelocity() and .setLinearVelocity() happen on sphere.physicsImpostor… but .angularDamping and .linearDamping happen on sphere.physicsImpostor.physicsBody. I believe… physicsImpostor is a BabylonJS object (created by the CannonJS plugin for BJS), but physicsBody is a CannonJS “native” object. So, you see, you can “interface” with physics at the BJS plugin level, OR at the CannonJS “native” level. Knowing this… might be important to your physics work… later.

I’ve always liked native-level damping, because it does gradual “brakes”… but I don’t know if that’s what CS prefers. Still fun to play-with. The more 9’s you add to line 76, the faster it stops on key-up.

Note: The friction value that is sometimes used when adding physicsImpostors to sphere/mesh… doesn’t work very well for spheres, because spheres don’t have much ground surface-contact area. This is why damping is used with rolling spheres, quite often.

I believe there is an option for .fixedRotation nearby, too, if you want the sphere to “skid” instead of roll. Also, setting angularDamping to 1… and keeping it set there… might prevent sphere rolling.

Keep in mind that .linearDamping and .angularDamping are NATIVE properties on CannonJS Bodies… and so… are probably named/used differently… when NOT using CannonJS physics (when using Oimo or Ammo, for example).

Now CS knows all of our physics tricks and secrets. :slight_smile: Time to issue CS a BabylonJS physics safety suit, helmet, and goggles. heh.