why the mobile robot move itself??

Post Reply
abcd1236386
Posts: 1
Joined: Tue Apr 01, 2014 9:46 am

why the mobile robot move itself??

Post by abcd1236386 »

hi ,i'm a freshman to bullet.
i use bullet build a simple wheeled robot,with two driving wheel. i change the code in the first bullet demo-App_BasicDemo
when i start running the program ,the robot always move it self ,i didn't set the hinge motor at all!! i thought maybe the gravity made it move ,but how can i solve this problem??
i'm very confused!
following is my code .
// it is in the basic demo.cpp

Code: Select all

/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/


///create 125 (5x5x5) dynamic object
#define ARRAY_SIZE_X 5
#define ARRAY_SIZE_Y 5
#define ARRAY_SIZE_Z 5

//maximum number of objects (and allow user to shoot additional boxes)
#define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)

///scaling of the objects (0.1 = 20 centimeter boxes )
#define SCALING 1.
#define START_POS_X -5
#define START_POS_Y -5
#define START_POS_Z -3

#include "BasicDemo.h"
#include "GlutStuff.h"
///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
#include "btBulletDynamicsCommon.h"

#include <stdio.h> //printf debugging
#include "GLDebugDrawer.h"
#include "LinearMath/btAabbUtil2.h"

static GLDebugDrawer gDebugDraw;

///The MyOverlapCallback is used to show how to collect object that overlap with a given bounding box defined by aabbMin and aabbMax. 
///See m_dynamicsWorld->getBroadphase()->aabbTest.
struct	MyOverlapCallback : public btBroadphaseAabbCallback
{
	btVector3 m_queryAabbMin;
	btVector3 m_queryAabbMax;
	
	int m_numOverlap;
	MyOverlapCallback(const btVector3& aabbMin, const btVector3& aabbMax ) : m_queryAabbMin(aabbMin),m_queryAabbMax(aabbMax),m_numOverlap(0)	{}
	virtual bool	process(const btBroadphaseProxy* proxy)
	{
		btVector3 proxyAabbMin,proxyAabbMax;
		btCollisionObject* colObj0 = (btCollisionObject*)proxy->m_clientObject;
		colObj0->getCollisionShape()->getAabb(colObj0->getWorldTransform(),proxyAabbMin,proxyAabbMax);
		if (TestAabbAgainstAabb2(proxyAabbMin,proxyAabbMax,m_queryAabbMin,m_queryAabbMax))
		{
			m_numOverlap++;
		}
		return true;
	}
};
struct btMyResult : public btCollisionWorld::ContactResultCallback
{
	virtual	btScalar	addSingleResult(btManifoldPoint& cp,	const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1)
	{

		printf("hello\n");

		return 0;
	}
};
btRigidBody* colObj=NULL;
btHingeConstraint *hgs[2]={NULL};
btRigidBody*wl,*wr,*bd;
void BasicDemo::clientMoveAndDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	//simple dynamics world doesn't handle fixed-time-stepping
	float ms = getDeltaTimeMicroseconds();
	///step the simulation
	if (m_dynamicsWorld)
	{
		
		m_dynamicsWorld->stepSimulation(ms/1000000,10,1/60.0);
		
		//optional but useful: debug drawing
		m_dynamicsWorld->debugDrawWorld();

		btVector3 aabbMin(1,1,1);
		btVector3 aabbMax(2,2,2);

		MyOverlapCallback aabbOverlap(aabbMin,aabbMax);
		m_dynamicsWorld->getBroadphase()->aabbTest(aabbMin,aabbMax,aabbOverlap);
		
		if (aabbOverlap.m_numOverlap)
			printf("#aabb overlap = %d\n", aabbOverlap.m_numOverlap);
	}
	
	renderme(); 

	glFlush();

	swapBuffers();

}



void BasicDemo::displayCallback(void) {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	
	renderme();

	//optional but useful: debug drawing to detect problems
	if (m_dynamicsWorld)
		m_dynamicsWorld->debugDrawWorld();

	glFlush();
	swapBuffers();
}



const float myscale=50;
void	BasicDemo::initPhysics()
{
	setTexturing(true);
	setShadows(true);

	setCameraDistance(btScalar(SCALING*5.5));
	m_enableshadows=false;
	///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

	///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
	btCollisionDispatcher* dispatcher = new	btCollisionDispatcher(collisionConfiguration);

	///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
	btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();

	///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

	m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
	
	m_dynamicsWorld->setGravity(btVector3(0,-10,0));

	///create a few basic rigid bodies
	btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(5.),btScalar(1),btScalar(5.)));
	//groundShape->initializePolyhedralFeatures();
//	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
	
	m_collisionShapes.push_back(groundShape);

	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0,-1,0));

	//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
	{
		btScalar mass(0.);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			groundShape->calculateLocalInertia(mass,localInertia);

		//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);

		//add the body to the dynamics world
		m_dynamicsWorld->addRigidBody(body);
	}



	{ //the two driving wheels.
		//create a few dynamic rigidbodies
		// Re-using the same collision is better for memory usage and performance

		
		//btCollisionShape* colShape = new btConeShape(1,10);
		btCollisionShape* colShape = new btCylinderShapeX(btVector3(0.01,0.04,0.04));
		m_collisionShapes.push_back(colShape);
		/// Create Dynamic Objects
		btTransform twr,twl;
		twr.setIdentity();
		twr.setOrigin(btVector3(0.12,0.04,0.05));
		twl.setIdentity();
		twl.setOrigin(btVector3(-0.12,0.04,0.05));
		//startTransform.setRotation(btQuaternion(btVector3(1,0,0),3.14159/2));
		btScalar	mass(0.0008);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			colShape->calculateLocalInertia(mass,localInertia);
		//startTransform.setOrigin(SCALING*btVector3(0,10,-30));				
						//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		btDefaultMotionState* myMotionState = new btDefaultMotionState(twr);
		btRigidBody::btRigidBodyConstructionInfo rbInfor(mass,myMotionState,colShape,localInertia);
		wr = new btRigidBody(rbInfor);

		myMotionState = new btDefaultMotionState(twl);
		btRigidBody::btRigidBodyConstructionInfo rbInfol(mass,myMotionState,colShape,localInertia);
		wl = new btRigidBody(rbInfol);

		m_dynamicsWorld->addRigidBody(wr);
		m_dynamicsWorld->addRigidBody(wl);
		wr->setActivationState(DISABLE_DEACTIVATION);
		wl->setActivationState(DISABLE_DEACTIVATION);
		wr->setFriction(0.9);
		wl->setFriction(0.9);
		
	}
	//the body
	{
		//create a few dynamic rigidbodies
		// Re-using the same collision is better for memory usage and performance

		btCollisionShape* box = new btBoxShape(btVector3(0.1,0.1,0.1));
		btCompoundShape* colShape = new btCompoundShape();
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(0,0,0));
		colShape->addChildShape(tr,box);
		tr.setOrigin(btVector3(0,-0.1,-0.07));
		colShape->addChildShape(tr,new btSphereShape(0.02));

		/// Create Dynamic Objects
		btTransform startTransform;
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(0,0.12,0));
		//startTransform.setRotation(btQuaternion(btVector3(1,0,0),3.14159/2));
		btScalar	mass(0.001);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			colShape->calculateLocalInertia(mass,localInertia);
		//startTransform.setOrigin(SCALING*btVector3(0,10,-30));				
		//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
		bd = new btRigidBody(rbInfo);
		//m_dynamicsWorld->addCollisionObject(body);
		//body->setCollisionFlags(body->getCollisionFlags()|btCollisionObject::CF_NO_CONTACT_RESPONSE|btCollisionObject::CF_KINEMATIC_OBJECT);//设置碰撞标志
		//body->setActivationState(DISABLE_DEACTIVATION);
		m_dynamicsWorld->addRigidBody(bd);
		bd->setFriction(0.2);
		bd->setActivationState(DISABLE_DEACTIVATION);
		//m_dynamicsWorld->addCollisionObject(body);
		

	}

	////hinge constraint
	btTransform jtA,jtB;
	jtA.setIdentity();
	jtB.setIdentity();
	jtA.setRotation(btQuaternion(btVector3(0,1,0),-3.1415926/2));
	jtA.setOrigin(btVector3(0.12,-0.079,0.05));
	jtB.setRotation(btQuaternion(btVector3(0,1,0),-3.1415926/2));
	jtB.setOrigin(btVector3(0,0,0));
	
	//btHingeConstraint* pHinge = new btHingeConstraint(*cart,*wheel,piva,pivb,axisa,axisb);
	btHingeConstraint* pHinger = new btHingeConstraint(*wr,*bd,jtB,jtA);
	jtA.setOrigin(btVector3(-0.12,-0.079,0.05));
	jtB.setOrigin(btVector3(0,0,0));
	btHingeConstraint* pHingel = new btHingeConstraint(*wl,*bd,jtB,jtA);
	m_dynamicsWorld->addConstraint(pHinger,true);
	m_dynamicsWorld->addConstraint(pHingel,true);
	hgs[0]=pHingel;
	hgs[1]=pHinger;

	//i disable the motor
	//pHingel->enableAngularMotor(true, -0.0f, 1.15f);
	//pHinger->enableAngularMotor(true, -0.0f, 1.15f);


}
void	BasicDemo::clientResetScene()
{
	exitPhysics();
	initPhysics();
}
	

void	BasicDemo::exitPhysics()
{

	//cleanup in the reverse order of creation/initialization

	//remove the rigidbodies from the dynamics world and delete them
	int i;
	for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
	{
		btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
		btRigidBody* body = btRigidBody::upcast(obj);
		if (body && body->getMotionState())
		{
			delete body->getMotionState();
		}
		m_dynamicsWorld->removeCollisionObject( obj );
		delete obj;
	}

	//delete collision shapes
	for (int j=0;j<m_collisionShapes.size();j++)
	{
		btCollisionShape* shape = m_collisionShapes[j];
		delete shape;
	}
	m_collisionShapes.clear();

	delete m_dynamicsWorld;
	
	delete m_solver;
	
	delete m_broadphase;
	
	delete m_dispatcher;

	delete m_collisionConfiguration;

	
}
Post Reply