[ODE] dWorldStep problem.....

Sandeep KL dreamtheater39 at msn.com
Sat Aug 9 04:36:02 2003


Hi,

I have found that the simulation completely changes with the step size given 
to the dWorldStep function.

I actually downloaded the sample from "FreeFall"
http://freefall.freehosting.net/downloads/buggydemo.html

I have basically copied all the initialization functions (CreateWorld and 
CreateBuggy)

I have given the stepsize in my application as 0.05 a fixed value.
But the, wheels and the body seem unstable......they keep bobbing in the 
same location.
if i give step size as 0.02 its fine.

THE ANAMOLY -
But when i give step size as 0.03 or 0.04......the object disappears after 
dropping from air and colliding with the ground.......why is this?

Many times, the object just goes away to infinity for no particular reason. 
Please help me!

Thanks in advance,
Sandeep


Here is the code -

//-----------------------------------------------------------------------------
// File: Physics Handler.cpp
//-----------------------------------------------------------------------------

#include "dx9 xforce engine.h"


D3DXVECTOR3 odeLoc;

// ODE globals
static dWorldID world;
static dSpaceID space;
static dJointGroupID contactGroup;

// ground and environment objects
static dGeomID groundGeom, wallGeom[5], envGeomGroup;
// car objects
static dGeomID carGeom[5], carGeomGroup;
static dBodyID carBodies[5];
static dJointID carJoints[4];

static const D3DXVECTOR3 carScale( 2.4f, 0.4f, 1.4f );
static const dReal wheelRadius = 0.3, wheelWidth = 0.25;


typedef unsigned long ulong;

static void NearCallback(void* /*data*/, dGeomID o1, dGeomID o2)
{
	const ulong N = 6;
	dContact contacts[N];
	// get the contacts up to a maximum of N contacts
	const ulong n = dCollide(o1, o2, N, &contacts[0].geom, sizeof(dContact));
	for(ulong i = 0; i < n; ++i)
	{
        contacts[i].surface.mode = dContactBounce;
		contacts[i].surface.mu = 250;
		contacts[i].surface.bounce = 0.3;
		contacts[i].surface.bounce_vel = 0.05;
		dJointID c = dJointCreateContact(world, contactGroup, &contacts[i]);
		dJointAttach(c, dGeomGetBody(contacts[i].geom.g1), 
dGeomGetBody(contacts[i].geom.g2));
	}
}




// simulation loop
void simLoop (int pause, float time)
{
  if (!pause)
  {
    dSpaceCollide (space,0,&NearCallback);
    dWorldStep (world,0.05);

    // remove all contact joints
    dJointGroupEmpty (contactGroup);
  }

}



void CreateWorld()
{
	static const D3DXVECTOR3 groundExtents(50.0f, 30.0f, 30.0f);

	world = dWorldCreate();
	space = dHashSpaceCreate();
	contactGroup = dJointGroupCreate(0);
	dWorldSetGravity(world, 0, -9.81, 0);

	// create the ground and (invisible) walls
	groundGeom = dCreatePlane(0,  0, 1,  0, 0); // x,y,z,d
	wallGeom[0] = dCreatePlane(0,  1, 0,  0, -groundExtents.x); // x,y,z,d
	wallGeom[1] = dCreatePlane(0, -1, 0,  0, -groundExtents.x); // x,y,z,d
	wallGeom[2] = dCreatePlane(0,  0, 0,  1, -groundExtents.y); // x,y,z,d
	wallGeom[3] = dCreatePlane(0,  0, 0, -1, -groundExtents.y); // x,y,z,d
	wallGeom[4] = dCreatePlane(0,  0, -1,  0, -40); // x,y,z,d = 40m cieling

	envGeomGroup = dCreateGeomGroup(space);
	dGeomGroupAdd(envGeomGroup, groundGeom);
    for(ulong i = 0; i < 5; ++i)
		dGeomGroupAdd(envGeomGroup, wallGeom[i]);

}

void CreateBuggy()
{
	// car body
	dMass m;
	dMassSetBox(&m, 1, carScale.x, carScale.y, carScale.z); // density,lx,ly,lz
	dMassAdjust(&m, 50); // mass

	carBodies[0] = dBodyCreate(world);
	dBodySetMass(carBodies[0], &m);
	dBodySetPosition(carBodies[0], 0, 3, 10); // x,y,z

	carGeom[0] = dCreateBox(0, carScale.x, carScale.y, carScale.z); // lx,ly,lz
	dGeomSetBody(carGeom[0], carBodies[0]);

	// wheel bodies
	dMassSetSphere(&m, 1, wheelRadius); // density, radius
	dMassAdjust(&m, 10); // mass
	dQuaternion q;
	dQFromAxisAndAngle(q, 1, 0, 0, M_PI * 0.5);
	for(ulong i = 1; i <= 4; ++i)
	{
		carBodies[i] = dBodyCreate(world);
		dBodySetMass(carBodies[i], &m);
		dBodySetQuaternion(carBodies[i], q);
		carGeom[i] = dCreateSphere(0, wheelRadius);
		dGeomSetBody(carGeom[i], carBodies[i]);
	}
	dBodySetPosition(carBodies[1], -0.9, 2.6,  10.7); // x,y,z
	dBodySetPosition(carBodies[2], -0.9, 2.6,   9.3); // x,y,z
	dBodySetPosition(carBodies[3],  0.9, 2.6,  10.7); // x,y,z
	dBodySetPosition(carBodies[4],  0.9, 2.6,   9.3); // x,y,z

	// wheel joints
	for(ulong i = 0; i < 4; ++i)
	{
		carJoints[i] = dJointCreateHinge2(world, 0);
		dJointAttach(carJoints[i], carBodies[0], carBodies[i+1]);
		const dReal* const wPos = dBodyGetPosition(carBodies[i+1]);
		dJointSetHinge2Anchor(carJoints[i], wPos[0], wPos[1], wPos[2]);
		dJointSetHinge2Axis1(carJoints[i], 0, 1, 0);
		dJointSetHinge2Axis2(carJoints[i], 0, 0, ((i % 2) == 0) ? -1 : 1);

		dJointSetHinge2Param(carJoints[i], dParamLoStop, 0);
		dJointSetHinge2Param(carJoints[i], dParamHiStop, 0);
		dJointSetHinge2Param(carJoints[i], dParamFMax, 50);

		dJointSetHinge2Param(carJoints[i], dParamVel2, 0);
		dJointSetHinge2Param(carJoints[i], dParamFMax2, 80);

		dJointSetHinge2Param(carJoints[i], dParamSuspensionERP, 0.25);
		dJointSetHinge2Param(carJoints[i], dParamSuspensionCFM, 0.004);
	}

	// geometry goup for the car
	carGeomGroup = dCreateGeomGroup(space);
	for(ulong i = 0; i < 5; ++i)
		dGeomGroupAdd(carGeomGroup, carGeom[i]);


}


void SetBuggyUI(double velocity, double steering)
{
	static const dReal steeringRate = M_PI * 4 / 3;
	static const dReal steeringLimit = M_PI / 6;

	dReal wheelVelocity = 12 * M_PI * velocity;// * 1.20; (sorry Nate, but this 
is waaay to fast for keyboarders) :)

	// a little bit of deadband seems to make it a bit easier to control
	if (fabs (steering) < 0.1)
		steering = 0;

	for(int i = 0; i < 4; ++i)
	{
		double desiredPosition = steering * ((i > 1) ? -1 : 1);
		double actualPosition = dJointGetHinge2Angle1(carJoints[i]);
		double steeringVelocity = (desiredPosition - actualPosition) * 10;

		dJointSetHinge2Param(carJoints[i], dParamHiStop, steeringLimit);
		dJointSetHinge2Param(carJoints[i], dParamLoStop, -steeringLimit);
		dJointSetHinge2Param(carJoints[i], dParamVel, steeringVelocity);
	}

	for(int i = 0; i < 4; ++i)
		dJointSetHinge2Param(carJoints[i], dParamVel2, ((i % 2) == 0) ? 
-wheelVelocity : wheelVelocity);
}



void DrawBuggy( CD3DFile* boxmesh, CD3DFile* cylmesh, LPDIRECT3DDEVICE9 
d3dDevice )
{

		D3DXMATRIX rt0, rt, rt1, rt2, rt3;
		const dReal *abx;




		// Main Body
		abx = dBodyGetRotation( carBodies[0] );
		rt0._11 = abx[0];
		rt0._12 = abx[1];
		rt0._13 = abx[2];
		rt0._14 = abx[3];

		rt0._21 = abx[4];
		rt0._22 = abx[5];
		rt0._23 = abx[6];
		rt0._24 = abx[7];

		rt0._31 = abx[8];
		rt0._32 = abx[9];
		rt0._33 = abx[10];
		rt0._34 = abx[11];

		rt0._41 = 0.0f;
		rt0._42 = 0.0f;
		rt0._43 = 0.0f;
		rt0._44 = 1.0f;




		abx = dBodyGetRotation( carBodies[1] );

		rt._11 = abx[0];
		rt._12 = abx[1];
		rt._13 = abx[2];
		rt._14 = abx[3];

		rt._21 = abx[4];
		rt._22 = abx[5];
		rt._23 = abx[6];
		rt._24 = abx[7];

		rt._31 = abx[8];
		rt._32 = abx[9];
		rt._33 = abx[10];
		rt._34 = abx[11];

		rt._41 = 0.0f;
		rt._42 = 0.0f;
		rt._43 = 0.0f;
		rt._44 = 1.0f;


		abx = dBodyGetRotation( carBodies[2] );
		rt1._11 = abx[0];
		rt1._12 = abx[1];
		rt1._13 = abx[2];
		rt1._14 = abx[3];

		rt1._21 = abx[4];
		rt1._22 = abx[5];
		rt1._23 = abx[6];
		rt1._24 = abx[7];

		rt1._31 = abx[8];
		rt1._32 = abx[9];
		rt1._33 = abx[10];
		rt1._34 = abx[11];

		rt1._41 = 0.0f;
		rt1._42 = 0.0f;
		rt1._43 = 0.0f;
		rt1._44 = 1.0f;

		abx = dBodyGetRotation( carBodies[3] );

		rt2._11 = abx[0];
		rt2._12 = abx[1];
		rt2._13 = abx[2];
		rt2._14 = abx[3];

		rt2._21 = abx[4];
		rt2._22 = abx[5];
		rt2._23 = abx[6];
		rt2._24 = abx[7];

		rt2._31 = abx[8];
		rt2._32 = abx[9];
		rt2._33 = abx[10];
		rt2._34 = abx[11];

		rt2._41 = 0.0f;
		rt2._42 = 0.0f;
		rt2._43 = 0.0f;
		rt2._44 = 1.0f;

		abx = dBodyGetRotation( carBodies[4] );

		rt3._11 = abx[0];
		rt3._12 = abx[1];
		rt3._13 = abx[2];
		rt3._14 = abx[3];

		rt3._21 = abx[4];
		rt3._22 = abx[5];
		rt3._23 = abx[6];
		rt3._24 = abx[7];

		rt3._31 = abx[8];
		rt3._32 = abx[9];
		rt3._33 = abx[10];
		rt3._34 = abx[11];

		rt3._41 = 0.0f;
		rt3._42 = 0.0f;
		rt3._43 = 0.0f;
		rt3._44 = 1.0f;


		//OutputMatrix( "rt", rt );


		char ssss[222];

// ---------------------------BODY--------------------------------
		D3DXVECTOR3 offset = D3DXVECTOR3( 50.0f, 0.0f, 50.0f );
		abx = dBodyGetPosition( carBodies[0] );
		odeLoc.x = (const float)abx[0];
		odeLoc.y = (const float)abx[1];
		odeLoc.z = (const float)abx[2];


		sprintf( ssss, "Box pos %f %f %f\n", odeLoc.x, odeLoc.y, odeLoc.z );
		//OutputDebugString( ssss );
		odeLoc += offset;


		D3DXMATRIX matrix;
		D3DXMatrixIdentity( &matrix );
		matrix._11 = 2.4; matrix._22 = 0.4; matrix._33 = 1.4;
		matrix = matrix * rt0;




		matrix._41 = odeLoc.x;
		matrix._42 = odeLoc.y;
		matrix._43 = odeLoc.z;

		d3dDevice->SetTransform( D3DTS_WORLD, &matrix );
		d3dDevice->SetTexture( 0, NULL );
		boxmesh->Render( d3dDevice );

// ---------------------------WHEEL0--------------------------------

		D3DXMATRIX scale;
		D3DXMatrixIdentity( &scale );
		scale._11 = wheelRadius; scale._22 = wheelRadius; scale._33 = wheelRadius;

		D3DXMatrixIdentity( &matrix );
		matrix = rt;
		//D3DXMatrixRotationY( &rot, DegtoRad( 90.0f ) );
		matrix = matrix * scale;

		abx = dBodyGetPosition( carBodies[1] );
		odeLoc.x = (const float)abx[0];
		odeLoc.y = (const float)abx[1];
		odeLoc.z = (const float)abx[2];

		odeLoc += offset;
		matrix._41 = odeLoc.x;
		matrix._42 = odeLoc.y;
		matrix._43 = odeLoc.z;
		d3dDevice->SetTransform( D3DTS_WORLD, &matrix );
//		OutputMatrix( "Wheel", matrix );
		cylmesh->Render( d3dDevice );

		//D3DXMatrixIdentity( &matrix );
		//D3DXMatrixRotationY( &rot, DegtoRad( 90.0f ) );
		//matrix = matrix * scale * rot;

// ---------------------------WHEEL1--------------------------------

		D3DXMatrixIdentity( &matrix );
		matrix = rt1;
		//D3DXMatrixRotationY( &rot, DegtoRad( 90.0f ) );
		matrix = matrix * scale;


		abx = dBodyGetPosition( carBodies[2] );
		odeLoc.x = (const float)abx[0];
		odeLoc.y = (const float)abx[1];
		odeLoc.z = (const float)abx[2];

		odeLoc += offset;
		matrix._41 = odeLoc.x;
		matrix._42 = odeLoc.y;
		matrix._43 = odeLoc.z;
		d3dDevice->SetTransform( D3DTS_WORLD, &matrix );
		cylmesh->Render( d3dDevice );

// ---------------------------WHEEL2--------------------------------

		D3DXMatrixIdentity( &matrix );
		matrix = rt2;
		//D3DXMatrixRotationY( &rot, DegtoRad( 90.0f ) );
		matrix = matrix * scale;

		abx = dBodyGetPosition( carBodies[3] );
		odeLoc.x = (const float)abx[0];
		odeLoc.y = (const float)abx[1];
		odeLoc.z = (const float)abx[2];

		odeLoc += offset;
		matrix._41 = odeLoc.x;
		matrix._42 = odeLoc.y;
		matrix._43 = odeLoc.z;
		d3dDevice->SetTransform( D3DTS_WORLD, &matrix );
		cylmesh->Render( d3dDevice );

// ---------------------------WHEEL3--------------------------------

		D3DXMatrixIdentity( &matrix );
		matrix = rt3;
		//D3DXMatrixRotationY( &rot, DegtoRad( 90.0f ) );
		matrix = matrix * scale;

		abx = dBodyGetPosition( carBodies[4] );
		odeLoc.x = (const float)abx[0];
		odeLoc.y = (const float)abx[1];
		odeLoc.z = (const float)abx[2];

		odeLoc += offset;
		matrix._41 = odeLoc.x;
		matrix._42 = odeLoc.y;
		matrix._43 = odeLoc.z;
		d3dDevice->SetTransform( D3DTS_WORLD, &matrix );
		cylmesh->Render( d3dDevice );


}

_________________________________________________________________
Going on a holiday? Want to study abroad? 
http://server1.msn.co.in/msnleads/citibankpersonalloan/citibankploanjuly03.asp?type=txt 
Need a personal loan?