Entity moving unexpectedly on corners/edges

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Jullo
Posts: 4
Joined: Tue Dec 16, 2014 8:57 pm

Entity moving unexpectedly on corners/edges

Post by Jullo »

Hey Norbo!

This is the first physics engine for me and I've learned a lot by reading this forum so thanks to you and all the questions and answers :)

But now I have no idea how to fix my problem. I'm using Bepu v1.3.0 and its slightly modified "GettingStartedDemo". This is my test project to test this issue but the same problem occurs on my real project.

I have a scaled box of type StaticMesh as flat platform, on which I move my dynamic Box entity (I'll call this "dude" from now on). Gravity is on all the time and dude's LocalInertiaTensorInverse is set to zero. Now, if I move "dude" around on an edge or any corner of the platform, dude jumps occasionally (Y changes by like 0,4-1,8 units). Feels like it happens more often when dude goes from flat area toward edge/corner... Also rotating dude on a corner causes sometimes strange movement on X and Z axis of LinearVelocity but not Y. But funny thing is that I couldn't reproduce this issue if the platform is massless (kinematic?) Box. I move THE Dude with LinearVelocity and AngularVelocity (I've tried EntityRotator and EntityMover but I'd like to do it on my own to understand things better). Strange part is that I'm trying to avoid changing LinearVelocity.Y to let gravity pull entities but it still changes and I've been thinking it's gravity somehow? But I would LOVE to know why this happens and how to fix it properly without hacky ways :)

This is how I move THE Dude:

Code: Select all

if (KeyboardState.IsKeyDown(Keys.G))
{
   controlledDude.AngularVelocity += Vector3.Up / 1.5f;
}

if (KeyboardState.IsKeyDown(Keys.J))
{
   controlledDude.AngularVelocity += Vector3.Down / 1.5f;
}

if (KeyboardState.IsKeyDown(Keys.Y))
{
   controlledDude.LinearVelocity += new Vector3(controlledDude.OrientationMatrix.Forward.X * 1.5f, 0, controlledDude.OrientationMatrix.Forward.Z * 1.5f);
}

if (KeyboardState.IsKeyDown(Keys.H))
{
   controlledDude.LinearVelocity += new Vector3(controlledDude.OrientationMatrix.Backward.X * 1.5f, 0, controlledDude.OrientationMatrix.Backward.Z * 1.5f);
}
Here's my very unoptimized code to slow down LinearVelocity (SlowDownEntity() handler is fired by PositionUpdated event):

Code: Select all

void SlowDownEntity(Entity e)
{
   if (e.LinearVelocity.Length() > .01f)
   {
      Vector3 velocity = e.LinearVelocity;

      // Reduce X and set to zero if it's slow enough
      float xDot = Vector3.Dot(Vector3.UnitX, velocity);
      Vector3 xDiff = Vector3.Zero;
      Vector3 reduce = new Vector3(velocity.X * .2f, 0, 0);
      Vector3 xVelocity = new Vector3(velocity.X, 0, 0);
      Vector3.Subtract(ref xVelocity, ref reduce, out xDiff);

      if (Math.Abs(xDot) < .2f)
         velocity.X = 0;

      // Same thing for Z
      float zDot = Vector3.Dot(Vector3.UnitZ, velocity);
      Vector3 zDiff = Vector3.Zero;
      reduce = new Vector3(0, 0, velocity.Z * .2f);
      Vector3 zVelocity = new Vector3(0, 0, velocity.Z);
      Vector3.Subtract(ref zVelocity, ref reduce, out zDiff);

      if (Math.Abs(zDot) < .2f)
         velocity.Z = 0;

      velocity.X = xDiff.X;
      velocity.Z = zDiff.Z;
      e.LinearVelocity = velocity;
   }
}
Dude's rotation is slowed down by its AngularDamping. To avoid linear movement during rotation, I remove friction between collision pairs when InitialCollisionDetected event is fired like you answered to someone:

Code: Select all

void RemoveFriction(EntityCollidable sender, BroadPhaseEntry other, NarrowPhasePair pair)
{
   var collidablePair = pair as CollidablePairHandler;
   if (collidablePair != null)
   {
      collidablePair.UpdateMaterialProperties(new InteractionProperties());
   }
}
Looking forward to any kind of solutions -- Jullo
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Entity moving unexpectedly on corners/edges

Post by Norbo »

It sounds like The Dude is hitting the boundaries of triangles in the box mesh. The Dude is generating contacts with normals that oppose its motion, and since it can't just tip over, it gets launched. A description of a similar problem can be found in this post: viewtopic.php?f=4&t=2240.

Some ways to eliminate/mitigate the bumps:
1) Make sure the mesh itself has the necessary connectivity information in the index list so that the engine can smooth out the edges. An edge (or vertex) is smoothed when the adjacent triangles refer to the same involved vertices through the index list, rather than duplicate vertices that happen to be the same place.
2) Use single contiguous wholes (like the Box entity you tested).
3) As mentioned in the linked post, increasing the collision margin of the shape can smooth things out.
Jullo
Posts: 4
Joined: Tue Dec 16, 2014 8:57 pm

Re: Entity moving unexpectedly on corners/edges

Post by Jullo »

Yes, now that all makes sense :D I tried changing collision margin but even large values didn't fix it, only made it worse and ruined collision. The original value of 0.04 seemed to be fine. I didn't even think of 3D model of my cube because it was the original one of the GettingStartedDemo. But now I created my own one and it indeed fixed the problem! Maybe I should check what's the difference between the original and my own to know it exactly...

Thanks a lot! :) Really gives another reason to keep using this forum :P
-- Jullo
Post Reply