Boxes fall through height field

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Boxes fall through height field

Postby jamesm6162 » Fri Apr 08, 2016 4:41 am

Hi

I have a pretty sticky problem. In my library where I wrapped Newton, I have 100 boxes spaced equally in the air over a heightfield with random heights between -0.1 and 0.1. The boxes all hit the heightfield, and some start falling around, but then some of them just start sinking through the heightfield and then fall away into space below.

When I reproduced the exact same scenario in DemosSandbox the issue does not happen. I don't know what I am doing differently as I compared all the values set for both the solver, boxes, heightfield, and everything is exactly the same. I am even now linking directly with the static libs produced by the DemosSandbox.sln (VisualStudio2010). This happens in double and single precision.

So I can't even give you a reproducible piece of code, as the problem disappears in the sandbox.

Do you have any idea where I can start looking?

Any help would be appreciated.

Thanks

James
jamesm6162
 
Posts: 49
Joined: Wed Aug 12, 2015 8:50 am

Re: Boxes fall through height field

Postby Julio Jerez » Fri Apr 08, 2016 9:21 am

can you can serialize the scene and the load it in the sand box?

that will make with the exact same parameters you are using.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Boxes fall through height field

Postby jamesm6162 » Fri Apr 08, 2016 10:43 am

Hi Julio

I did try serializing but it didn't work when I tried to deserialize, I think my callback must have done something wrong.

However I did manage to reproduce the problem in the Sandbox. I replaced the HeightFieldCollision demo with the code below. Note: I removed the visuals and forced the debug to draw wireframe for easier debugging (for me). Problem is both single and double precision float configurations.

Code: Select all
void LoadHeights(dFloat* elevation, int width, int height)
{
   dFloat max_height = 0.2f;
   for (int i = 0; i < width*height; ++i)
   {
      elevation[i] = rand() * max_height / RAND_MAX;
   }
}

void HeightFieldCollision (DemoEntityManager* const scene)
{
   // load the sky box
   scene->CreateSkyBox();
   int size = 64;
   
   dFloat* elevation = new dFloat [size * size];

   LoadHeights(elevation, size, size);

   dFloat extent = 128.0f; // world size
   dFloat horizontal_scale = 128.0f / (size - 1);

   // create the attribute map
   char* const attibutes = new char [size * size];
   memset (attibutes, 0, size * size * sizeof (char));
   NewtonCollision* height_field= NewtonCreateHeightFieldCollision (scene->GetNewton(), size, size, 0, 0, elevation, attibutes, 1.0f, horizontal_scale, 0);

   float offset = -extent * 0.5f;

   dMatrix mLocal(dGetIdentityMatrix());
   mLocal.m_posit = dVector(offset,0, offset);
   NewtonCollisionSetMatrix(height_field, &mLocal[0][0]);

   dMatrix matrix(dGetIdentityMatrix());
   NewtonBody* terrain_body = NewtonCreateDynamicBody(scene->GetNewton(), height_field, &matrix[0][0]);
   NewtonBodySetMassProperties(terrain_body, 0.0, NewtonBodyGetCollision(terrain_body));
   NewtonDestroyCollision(height_field);

   NewtonCollision* collision_pool[3] = {
      NewtonCreateBox(scene->GetNewton(), 1,1,1, 0, nullptr),
      NewtonCreateSphere(scene->GetNewton(), 0.5, 0, nullptr),
      NewtonCreateCapsule(scene->GetNewton(), 0.5, 0.5, 1.0, 0, nullptr),
   };

   const int num_bodies_x = 10;
   const int num_bodies_z = 10;

   dFloat usable = extent;

   dFloat spacing_x = usable / (num_bodies_x + 1);      
   dFloat spacing_z = usable / (num_bodies_z + 1);
   dFloat start = -usable * 0.5f;

   dVector center(start,10,start);
   for (int i = 0; i < num_bodies_z; ++i)
   {
      for (int j = 0; j < num_bodies_x; ++j)
      {
         dFloat offset_x = spacing_x * (j+1);
         dFloat offset_z = spacing_z * (i+1);
         matrix.m_posit = center + dVector(offset_x, 0, offset_z);
         NewtonCollision* col = collision_pool[0];
         NewtonBody* body = NewtonCreateDynamicBody(scene->GetNewton(),col,&matrix[0][0]);
         NewtonBodySetForceAndTorqueCallback(body, PhysicsApplyGravityForce);
         NewtonBodySetMassProperties(body, 1.0, NewtonBodyGetCollision(body));
      }
   }

   for (int i = 0; i < 3; ++i)
   {
      NewtonDestroyCollision(collision_pool[i]);
   }
   delete [] attibutes;
   delete [] elevation;

   dMatrix locationTransform (dGetIdentityMatrix());   
   locationTransform.m_posit.m_y = 2.0f;

   scene->SetCameraMatrix(dQuaternion(locationTransform), locationTransform.m_posit + dVector(0, 5, 0));
   
   NewtonDemos* const mainWindow = scene->GetRootWindow();
   mainWindow->m_debugDisplayMode = 2;
}


Let me know if you don't see anything wrong the rand() might generate numbers that happen to work fine. Then I will send you a file with height data.

Thanks

EDIT: Also I had to remove the lines in DebugDisplay to draw the heightfield always-on.
Code: Select all
         case SERIALIZE_ID_HEIGHTFIELD:
//         case SERIALIZE_ID_COMPOUND_BREAKABLE:
            break;
jamesm6162
 
Posts: 49
Joined: Wed Aug 12, 2015 8:50 am

Re: Boxes fall through height field

Postby Julio Jerez » Fri Apr 08, 2016 2:23 pm

Ok I just paste that script in the demo, and yes I can see the bug just like you said.

thanks for the repro.
I do not have time right now to debug it, I will do these afternoon.

I am curios why this do no happen in the normal demo, my guess is that is does it is just that but
chance the dynamics bodies happen to be spawn in place where the bug collision does not happens
But thank to this repro, that bug will be corrected.

Thanks. :D
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Boxes fall through height field

Postby Julio Jerez » Sat Apr 09, 2016 8:50 am

ok, yes there was subtle bug there in the calculation of the aabb of the box over the terrain.
sync, it was rounding error with small chance of happening, my guess that the change I made to he collision that translate the objects to a common local space exposed it.

it is fixed now. you can sync
Thank for the repro bug report this would be hard to find for large shapes like there are in the demo
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Boxes fall through height field

Postby jamesm6162 » Sun Apr 10, 2016 7:42 am

Hi Julio

Yes I had quit a bit of trouble trying to reproduce it in the Sandbox. Like you said it was probably just small differences in the spawning position. Which is why I had to add 100 boxes :)

Thanks very much for the fix. I will check it out tomorrow.

Regards

James
jamesm6162
 
Posts: 49
Joined: Wed Aug 12, 2015 8:50 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest