How to stop physics drifting?

My character is in a physics world with hills in it. I’m using a SphereImpostor for the character and a MeshImpostor for the world. When they are on the side of a hill, they slide down the hill, which is good, but I’d like to be able to control how steep the hill needs to be before they slide. In my world, even a very shallow incline causes the character to drift slowly. I’ve tried changing the friction on both impostors up to 500, and it does slow things (including normal movement), but I’m still drifting on shallow inclines. I played with registerBeforePhysicsStep, even setting the impostor’s linear and angular velocity to 0, but still I’m drifting.

Is there any way to get the physics to let my character stop entirely, either through manipulating the variables (such as friction) or by manually modifying things in process (during the before physics step or somesuch)?

Thank you for your help!

Hi @iangilman - our physics champ is in vacations. Maybe @Evgeni_Popov can help you on this?

Sorry, I know nothing of physic engines but @RaananW may have an idea?

You can set the physics body to sleep and wake it up when you want:

bingo | Babylon.js Playground (babylonjs.com)

Note that some engine will wake the body up on external collisions, so it might be a little trickier in your case.

2 Likes

@RaananW that sounds perfect! I must have missed that in the docs.

Unfortunately, when I try it, I’m getting this error in the console (and it doesn’t seem to be working):

BJS: sleepBody is not currently supported by the Ammo physics plugin

Looks like the plugin we chose doesn’t have this feature. Can you recommend which plugin to use?

Thank you!

The Physics Engine used in RaananW’s playground is the default one : cannon.

Otherwise, it might be silly, but couldn’t your issue be solved by setting the body’s mass to 0 until the hill’s reached its critical steepness (or whatever condition you want) ?
That way, your body could still be subject to collision, impulse or angular velocity, if that’s something useful to you (I assume putting the physics body to sleep would forbid that).

2 Likes

What @Faber said :slight_smile:

There is difference is the way collision is being handled between the two cases. If a body is sleeping and a collision happens, it will wake up and the forces will be applied correctly. A mass : 0 body will cause a collision, but will stay in place.

And yes, I use cannon, which is usually the physics engine of my choice (mainly because I never really looked into Ammo too much…)

1 Like

Changing the mass to 0 does exactly what I was hoping for! Thank you for that suggestion :slight_smile:

One small caveat: I was hoping to taper the mass off so I would glide to a stop, but it looks like even minor changes to the mass will cause the object’s momentum to get reset to 0. I can work around that, but I thought I’d mention it. This is in Ammo; changing physics engines at this point in the project looks like it’ll be a big deal.

Thank you for all of your help!

You mentioned before that friction on your bodies does slow the drift down. Maybe you could use that for your tapering, until at some point you set the mass to 0 to immobilize the body completely ? Then you could use this ‘immunity’ to restore your standard friction.

Please tell us how your attempts go, or if you fixed your problem. :slight_smile:

1 Like

Something else you can do is to “clamp” the objects position by manually setting it in the registerAfterPhysicsStep Observable. Note that you previously tried the before step

@Faber Nice, using friction for the slowdown is a great idea (assuming setting it doesn’t reset the momentum like mass does)! At the moment we have an implementation we are happy enough with to put into testing (where we stop you when you slow down enough), but I’ll keep this idea in case we want to revisit the solution :slight_smile:

@jelster One thing I did try is to compare the mesh position from registerBeforPhysicsStep to registerAfterPhysicsStep, and it seemed to be the same. I’m pretty sure I cloned it (otherwise of course it would be the same), but now I can’t be sure. Anyway, you’re right that that would be another area to dig deeper into.

Thank you all for the help and suggestions! Having a lively community make such a difference :heart:

3 Likes

You know Babylon and its community never stops to amaze me. I’m far from an advanced user so I’m constantly looking for answers and explanations, it’s great to be able to give back a little !
If your problem is fixed though, please check the answer you think would help people with similar issues the most, so the topic is referenced under solved questions. :slight_smile:

1 Like

Oh yes, thank you for the reminder! I’m new here, so I didn’t think to mark a solution. I’ve done so now :slight_smile:

1 Like