how to detect the collision between Rigidbodies?

lolly
Posts: 4
Joined: Wed Dec 14, 2011 8:12 am

how to detect the collision between Rigidbodies?

Post by lolly »

hi I've been looked for a solusion for my project, I have two static bodies and one kinematic body. with the mothode in
http://bulletphysics.org/mediawiki-1.5. ... d_Triggers
I can check if there is a collision appears, but I want to filter this, so that I can just check for the collisions between my static bodies and the kinematic body. I have tried to use groups & masks like http://www.bulletphysics.org/mediawiki- ... _Filtering:

Code: Select all

OgreBulletDynamics::RigidBody *ersetzArrowBody;	
OgreBulletDynamics::RigidBody *aim1Body;
OgreBulletDynamics::RigidBody *aim2Body;
OgreBulletDynamics::RigidBody *defaultPlaneBody;

//....
OgreBulletDynamics::DynamicsWorld *mWorld;	// OgreBullet World

Code: Select all

// Collision groups & masks
#define BIT(x) (1<<(x))
enum collisiontypes {
	COL_NOTHING = 0,
	COL_PLANE = BIT(1),
	COL_ERSETZARROW = BIT(2), // collide with the arrow, which was shoot 	
	COL_AIM1 = BIT(3), // collide with the Ring 
	COL_AIM2 = BIT(4), // collide with the circle
};

short arrowCollidesWith = COL_AIM1 | COL_AIM2;
short aim1CollidesWith = COL_ERSETZARROW;
short aim2CollidesWith = COL_ERSETZARROW;
short planeCOllidesWith = COL_NOTHING;

Code: Select all

void checkForCollision()
	{  			
			mWorld->addRigidBody(defaultPlaneBody,COL_PLANE,planeCOllidesWith);
			mWorld->addRigidBody(ersetzArrowBody,COL_ERSETZARROW,COL_ERSETZARROW|COL_AIM1 | COL_AIM2);
			mWorld->addRigidBody(aim1Body,COL_AIM1,aim1CollidesWith);
			mWorld->addRigidBody(aim2Body,COL_AIM2,aim2CollidesWith);

			mWorld->getBulletCollisionWorld()->performDiscreteCollisionDetection();
			
			///one way to draw all the contact points is iterating over contact manifolds / points:
			int  numManifolds =mWorld->getBulletCollisionWorld()->getDispatcher()->getNumManifolds();
			
			// go through every manifolds
			for (unsigned int i=0;i < numManifolds; i++)
			{
				// get current manifold
				btPersistentManifold* contactManifold = mWorld->getBulletCollisionWorld()->getDispatcher()->getManifoldByIndexInternal(i);

				// get collisionpair
				btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
				btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
				

				// get number of contact points of these objects			
				const unsigned int numContacts = contactManifold->getNumContacts();
				// go through every contact point
				for (unsigned int j = 0;j < numContacts; j++)
				{
					//get current manifold point
					btManifoldPoint& pt = contactManifold->getContactPoint(j);

					/*g_CSkeletalViewerApp.m_f<<pt.getDistance()<<endl;*/

						if (pt.getDistance()<0.f)
							{
													getroffen = true;
								g_CSkeletalViewerApp.m_f<<" Collision found: "<<endl;
							}
						else if (pt.getDistance()>0.f)
							{
								g_CSkeletalViewerApp.m_f<<" Collision not found: "<<endl;
							}			
					}
				}	
		}
Now I get an error (assertion failed!), if I start to debug. Anyone can help me? Thanks
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: how to detect the collision between Rigidbodies?

Post by dphil »

Could you provide more information about the error? What is the assertion that failed?
lolly
Posts: 4
Joined: Wed Dec 14, 2011 8:12 am

Re: how to detect the collision between Rigidbodies?

Post by lolly »

dphil wrote:Could you provide more information about the error? What is the assertion that failed?
The error message:
Exception at 0x0134d9f1 in OgreBullettutorialftest.exe: 0xC0000005: access exception when read at position 0x0000010c (Original in german: "Unbehandelte Ausnahme bei 0x0134d9f1 in OgreBullettutorialftest.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x0000010c.")

It comes up with the line

Code: Select all

 mWorld->addRigidBody(defaultPlaneBody,COL_PLANE,COL_NOTHING);
If I start to debug step by step, it stops hier:

Code: Select all

 inline btRigidBody*  RigidBody::getBulletRigidBody() const 
    {
        return static_cast <btRigidBody* > (mObject);
    };
which is in OgreBulletDynamicsRigidBody.h
trollington
Posts: 8
Joined: Thu Nov 24, 2011 3:08 pm

Re: how to detect the collision between Rigidbodies?

Post by trollington »

0x10c is 268 decimal, which is smaller than the size of a collision object. My guess is that you are passing a null (0) pointer to mWorld->addRigidBody() -- you should be allocating defaultPlaneBody and the others to something earlier in your code, otherwise it will assume that the memory at 0x0000000 (outside your program) has a btRigidBody allocated, which causes the access violation (will probably give you an address like 0xcdcd???? in Debug mode).
lolly
Posts: 4
Joined: Wed Dec 14, 2011 8:12 am

Re: how to detect the collision between Rigidbodies?

Post by lolly »

trollington wrote:0x10c is 268 decimal, which is smaller than the size of a collision object. My guess is that you are passing a null (0) pointer to mWorld->addRigidBody() -- you should be allocating defaultPlaneBody and the others to something earlier in your code, otherwise it will assume that the memory at 0x0000000 (outside your program) has a btRigidBody allocated, which causes the access violation (will probably give you an address like 0xcdcd???? in Debug mode).
Thanks, I find out the problem, I added six RigidBodies already with these codes:
1.

Code: Select all

defaultPlaneBody = new OgreBulletDynamics::RigidBody("FloorPlane", mWorld,COL_PLANE,COL_ERSETZARROW);

2.

Code: Select all

aim1Body = new RigidBody("Rigid" + StringConverter::toString(mNumEntitiesInstanced), mWorld,COL_AIM1,COL_ERSETZARROW);
3.

Code: Select all

aim2Body = new RigidBody("Rigid" + StringConverter::toString(mNumEntitiesInstanced), mWorld,COL_AIM2,COL_ERSETZARROW);
4.

Code: Select all

bowBody = new OgreBulletDynamics::RigidBody("bow" + StringConverter::toString(mNumEntitiesInstanced), mWorld,COL_BOW,COL_NOTHING);
5.

Code: Select all

arrowBody = new OgreBulletDynamics::RigidBody("arrow" + StringConverter::toString(mNumEntitiesInstanced), mWorld,COL_ARROW,COL_NOTHING);
6.

Code: Select all

ersetzArrowBody = new OgreBulletDynamics::RigidBody("Ersetzarrow" + StringConverter::toString(mNumEntitiesInstanced), mWorld,COL_ERSETZARROW,COL_AIM1|COL_AIM2|COL_PLANE);
with these codes I can filter the Collisions between bow and arrow, then I want to find out the collisions between aim1, aim2, ersetzArrow and Plane. I checked the collision objects with:

Code: Select all

if(obA==ersetzArrowBody->getBulletObject())
				{ 
					g_CSkeletalViewerApp.m_f<<"A Objekt is Ersetzarrow "<<endl;
				}
                                                   ...
				if(obB==ersetzArrowBody->getBulletObject())
				{ 
					g_CSkeletalViewerApp.m_f<<"B Objekt is Ersetzarrow "<<endl;
				}
				...

finally i find out that the obB is always the Plane and the obA is ersetzArrow, hier btw aim1 and aim2 are static objects, do you have any ideals what happend hier? I hope you can understand want I mean... thx