Character sinks through the ground when landing (Solved)

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Posts: 6
Joined: Tue Jan 03, 2017 11:56 pm

Character sinks through the ground when landing (Solved)

Post by Vanit » Sun Mar 31, 2019 8:20 am

So I'm posting my own question and its answer simultaneously here as I finally fixed a bug after a year of revisiting it and repeatedly failing, and hope this might help someone in the future.

Bit of background; I'm creating a 2d isometric platforming game with 3D physics. I use a topological sort on the sprites (ground tiles + game objects) such that the character can walk infront/behind/over/under objects dynamically and it all looks sane. Because I'm using 3D position + bounding box data to feed into the sort it also needs to be sane, and that means undesired collision penetration matters more than in a 3D game.

In my game the scale is 1 unit = 1 meter, with earth-like gravity. I setup my game like in the tutorials/docs/examples, yet if my character fell from a starting position of more than a few meters off the ground he'd penetrate it upon landing by ~0.1f on the Y axis, causing my sort to act like the character is in the ground for a single frame (not good), and the character would also visibly move a pixel downwards after landing.

In my journey to solve this issue I consulted the threads like this (my own seemingly similar post from a while ago), this (some great tidbits here), this, and probably 20 others that seemed to put me in reach of solving the issue, but not quite. No matter how much I messed with settings like DefaultMargin, AllowedPenetration, Softness, MaximumPenetrationRecoverySpeed, IterationLimit, InertiaTensorScale, DefaultMinimumImpulse, I could never get it to look quite right such that my character hit the ground and stopped immediately as expected.

It turns out the solution is infinitely more simple. Eventually I recalled Norbo has repeatedly advertised the benefits of lowering TimeStepDuration, but I had overlooked it because I'm intentionally using space.Update() which is only a single time step (I'd rather the physics look smooth even if that means slow downs if frames are dropped). Turns out you can get the best of both worlds by halving TimeStepDuration (set to 1 / 120), and simply calling space.Update() twice per frame. Increased accuracy, still a fixed time in physics per frame, and now penetration is small enough not to matter. I'm going to go eat my hat now.

Kudos Norbo for all the great posts, wish I'd figured this out sooner. ^^;

Site Admin
Posts: 4903
Joined: Tue Jul 04, 2006 4:45 am

Re: Character sinks through the ground when landing (Solved)

Post by Norbo » Sun Mar 31, 2019 9:13 pm


(as always, a v2 note: with a sufficiently large speculative margin or enabled continuous collision detection, this problem basically disappears.)

Post Reply