How to prevent wall penetrations?

Post Reply
lucky7456969
Posts: 26
Joined: Thu Sep 11, 2014 5:40 am

How to prevent wall penetrations?

Post by lucky7456969 »

Hello,
I am a newbie of bullet.
And I am using the Ogre3D renderer with OgreBullet wrapper.
Here comes the question....

Here I iterate the scene and add walls to bullet so that later when I add cubes to the scene,
they will bounce off the walls naturally,

Code: Select all

Ogre::SceneManager::MovableObjectIterator iterator = mSceneMgr->getMovableObjectIterator("Entity");
	while(iterator.hasMoreElements())
{
          Ogre::Entity* e = static_cast<Ogre::Entity*>(iterator.getNext());
	   
	  addWorldObject(e->getName(), e, e->getParentSceneNode(), 1.0f);
} 

Code: Select all

void OgreBulletListener::addWorldObject(Ogre::String name, Ogre::Entity* pEntity, Ogre::SceneNode* pNode, btScalar mass)
{

        if(!pEntity) return;
        if(!pNode) return;
 
	AxisAlignedBox aabb = pEntity->getWorldBoundingBox(true);
 	Ogre::Vector3 h = aabb.getHalfSize();
 

	OgreBulletCollisions::BoxCollisionShape *sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(h);
	OgreBulletCollisions::CompoundCollisionShape* comShape = new OgreBulletCollisions::CompoundCollisionShape();
	comShape->addChildShape(sceneBoxShape, Ogre::Vector3(0,-0.7,-1.3), Ogre::Quaternion(1,0,0,0));
		 

	Ogre::Vector3 position = pNode->getPosition();
	Ogre::Quaternion direction = pNode->getOrientation();
   
	OgreBulletDynamics::RigidBody *defaultBody = new 
	OgreBulletDynamics::RigidBody("defaultBoxRigid" + Ogre::StringConverter::toString(mNumEntitiesInstanced++),mWorld);
	 
	defaultBody->setStaticShape(sceneBoxShape,
		0.6f,
	        0.8f,
    		position,
		direction); 
		mBodies.push_back(defaultBody); 
		mNumEntitiesInstanced++; 
         
}

Code: Select all

RigidBody *OgreBulletListener::addCube(const Ogre::String instanceName,
                                       const Ogre::Vector3 &pos, const Ogre::Quaternion &q, const Ogre::Vector3 &size,
                                       const Ogre::Real bodyRestitution, const Ogre::Real bodyFriction, 
                                       const Ogre::Real bodyMass)
{
    Entity *entity = mSceneMgr->createEntity(
        instanceName + StringConverter::toString(mNumEntitiesInstanced),
        "Bulletbox.mesh");
    // "Crate.mesh");
    // "Crate1.mesh");
    // "Crate2.mesh");


    entity->setQueryFlags (GEOMETRY_QUERY_MASK);
#if (OGRE_VERSION < ((1 << 16) | (5 << 8) | 0)) // only applicable before shoggoth (1.5.0)
    entity->setNormaliseNormals(true);
#endif
	entity->setCastShadows(true);

    entity->setMaterialName("Bullet/box");

    BoxCollisionShape *sceneCubeShape = new BoxCollisionShape(size);

    RigidBody *defaultBody = new RigidBody(
        "defaultCubeRigid" + StringConverter::toString(mNumEntitiesInstanced), 
        mWorld);

    SceneNode *node = mSceneMgr->getRootSceneNode ()->createChildSceneNode ();
    node->attachObject (entity);

    defaultBody->setShape (node,  sceneCubeShape, bodyRestitution, bodyFriction, bodyMass, pos, q);

    mEntities.push_back(entity);
    mShapes.push_back(sceneCubeShape);
    mBodies.push_back(defaultBody);
    mNumEntitiesInstanced++;

    return defaultBody;
}

Code: Select all

void OgreBulletListener::throwDynamicObject(BULLET_KEY_CODE key)
{
    const float trowDist = 2.0f;
    switch(key)
    {
    case KC_N: 
        if ( checkIfEnoughPlaceToAddObject(trowDist))
        {
            const Ogre::Vector3 vec (mCamera->getDerivedPosition());
            OgreBulletDynamics::RigidBody *body = addCube("cube", vec, Quaternion(0,0,0,1), 
                gCubeBodyBounds, gDynamicBodyRestitution, gDynamicBodyFriction, gDynamicBodyMass);

            body->setLinearVelocity(
                mCamera->getDerivedDirection().normalisedCopy() * mShootSpeed
				);		 
	}
        break;
And I am probing the keypressed event and change the camera accordingly, however, when I pressed 'N'
on the keyboard, the cube is always located at the same location, and it doesn't move at all.
Note: When I don't addWorldObject (walls etc), the cubes will run thru walls, if I do, they will be trapped in the same location.
Could anyone shed some lights on this?

Thanks
Jacl
Post Reply