[ODE] Problems with friction and fdir1

Graham Fyffe gfyffe at gmail.com
Sun Apr 10 23:25:13 MST 2005


Hey all.  I've been playing more with friction and fdir1.  It seems to
me that if I use fdir1 that is not exactly aligned with the X, Y or Z
axis, then my objects start going madly off in all directions.  There
is a clear relationship between the fdir1 that I give the contact and
the deviation that I get.  I have hacked up some code to show this
very, very clearly.  I just push some boxes in the Z direction, and
each box has the *local* x-axis as fdir1.  This means I just used
dBodyVectorToWorld to calculate the fdir1.  Im just setting fdir1, and
not activating mu2, nor am I using motion1 or motion 2 or slip1 or
slip2... just plain vanilla MU.

In theory, all the boxes should behave exactly the same and just slide
along Z until they stop.  In practise, the differing fdir1 values seem
to make the boxes deviate from Z, which I can only speculate has to do
with the friction pyramid calculations.

Is there any way to fix this?  My my test code follows.

- Graham Fyffe

----- boxtest.cpp -----

#include <ode/ode.h>
#include <drawstuff/drawstuff.h>

#ifdef _MSC_VER
#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
#endif

// select correct drawing functions

#ifdef dDOUBLE
#define dsDrawBox dsDrawBoxD
#define dsDrawSphere dsDrawSphereD
#define dsDrawCylinder dsDrawCylinderD
#define dsDrawCappedCylinder dsDrawCappedCylinderD
#endif


// some constants

#define LENGTH 0.2	// box length & width
#define HEIGHT 0.05	// box height
#define MASS 0.2	// mass of box[i][j] = (i+1) * MASS
#define FORCE 0.05	// force applied to box[i][j] = (j+1) * FORCE
#define MU 0.5		// the global mu to use
#define GRAVITY 0.5	// the global gravity to use
#define N1 10		// number of different forces to try
#define N2 10		// number of different masses to try


// dynamics and collision objects

static dWorldID world;
static dSpaceID space;
static dBodyID body[N1][N2];
static dJointGroupID contactgroup;
static dGeomID ground;
static dGeomID box[N1][N2];



// this is called by dSpaceCollide when two objects in space are
// potentially colliding.

static void nearCallback (void *data, dGeomID o1, dGeomID o2)
{
  int i;

  // only collide things with the ground
  bool g1 = (o1 == ground);
  bool g2 = (o2 == ground);
  if (g1 == g2) return;

  dBodyID b1 = dGeomGetBody(o1);
  dBodyID b2 = dGeomGetBody(o2);
  if (g1)
  {
	  dBodyID t = b1;
	  b1 = b2;
	  b2 = t;
  }

  dContact contact[3];		// up to 3 contacts per box
  for (i=0; i<3; i++) {
    contact[i].surface.mode = dContactSoftCFM | dContactApprox1 | dContactFDir1;
    contact[i].surface.mu = MU;
    contact[i].surface.soft_cfm = 0.01;
	dBodyVectorToWorld(b1, 1, 0, 0, contact[i].fdir1);
  }
  if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
    for (i=0; i<numc; i++) {
      dJointID c = dJointCreateContact (world,contactgroup,contact+i);
      dJointAttach (c,b1,b2);
    }
  }
}


// start simulation - set viewpoint

static void start()
{
  static float xyz[3] = {1.7772,-0.7924,2.7600};
  static float hpr[3] = {90.0000,-54.0000,0.0000};
  dsSetViewpoint (xyz,hpr);
}


// simulation loop

static void simLoop (int pause)
{
  int i;
  if (!pause) {

	  int const maxIt = 10;
	  for (int t = 0; t < maxIt; t ++)
	  {

    dSpaceCollide (space,0,&nearCallback);
    dWorldStep (world,0.05 / maxIt);

    // remove all contact joints
    dJointGroupEmpty (contactgroup);

	  }
  }

  dsSetColor (1,0,1);
  dReal sides[3] = {LENGTH,LENGTH,HEIGHT};
  for (i=0; i<N1; i++) {
    for (int j=0; j<N2; j++) {
      dsDrawBox (dGeomGetPosition(box[i][j]),dGeomGetRotation(box[i][j]),
		 sides);
    }
  }
}

static void reset()
{
	int i,j;
	// bodies
	for (i=0; i<N1; i++)
	{
		for (j=0; j<N2; j++)
		{
			dBodySetPosition(body[i][j], i*2*LENGTH, j*2*LENGTH, HEIGHT*0.5);
			dMatrix3 R;
			dRFromAxisAndAngle(R, 0, 0, 1, i / (N1 - 1.0) * 3.14159 / 2.0);
			dBodySetRotation(body[i][j], R);
			dBodySetLinearVel(body[i][j], 0, j * 0.5, 0);
			dBodySetAngularVel(body[i][j], 0, 0, 0);
		}
	}
}

static void command (int cmd)
{
	switch (cmd)
	{
	case 'r':
		reset();
		break;
	}
}

int main (int argc, char **argv)
{
  int i,j;
  dMass m;

  // setup pointers to drawstuff callback functions
  dsFunctions fn;
  fn.version = DS_VERSION;
  fn.start = &start;
  fn.step = &simLoop;
  fn.command = command;
  fn.stop = 0;
  fn.path_to_textures = "../../drawstuff/textures";

  // create world
  world = dWorldCreate();
  space = dHashSpaceCreate (0);
  contactgroup = dJointGroupCreate (0);
  dWorldSetGravity (world,0,0,-GRAVITY);
  ground = dCreatePlane (space,0,0,1,0);
  
  // bodies
  for (i=0; i<N1; i++) {
	  for (j=0; j<N2; j++) {
		  body[i][j] = dBodyCreate (world);
		  dMassSetBox (&m,1,LENGTH,LENGTH,HEIGHT);
		  //dMassAdjust (&m,MASS*(j+1));
		  dBodySetMass (body[i][j],&m);
		  box[i][j] = dCreateBox (space,LENGTH,LENGTH,HEIGHT);
		  dGeomSetBody (box[i][j],body[i][j]);
	  }
  }

  reset();

  // run simulation
  dsSimulationLoop (argc,argv,352,288,&fn);

  dJointGroupDestroy (contactgroup);
  dSpaceDestroy (space);
  dWorldDestroy (world);

  return 0;
}


More information about the ODE mailing list