Game.h virtual OgreBulletDynamics::RigidBody *buildSphere(Ogre::Vector3 position, bool makeStatic = false); Game.cpp #include #include #include #include OgreBulletDynamics::RigidBody *MarbleGame::buildSphere(Ogre::Vector3 position, bool makeStatic){ //create entity Ogre::Entity *entity = mSceneMgr->createEntity("Entity" + Ogre::StringConverter::toString(m_numEntities), "sphere.mesh"); //create node Ogre::SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node" + Ogre::StringConverter::toString(m_numEntities), position); node->attachObject(entity); Ogre::AxisAlignedBox bound = entity->getBoundingBox(); Ogre::Vector3 size = bound.getSize(); size /= 2.0f; //size *= 0.95f; entity->setMaterialName("Examples/BumpyMetal"); //scale both the node and the size node->scale(0.05f, 0.05f, 0.05f); size *= 0.05f; //create bullet stuff OgreBulletCollisions::SphereCollisionShape *shape = new OgreBulletCollisions::SphereCollisionShape(size.y); OgreBulletDynamics::RigidBody *body = new OgreBulletDynamics::RigidBody("rigid" + Ogre::StringConverter::toString(m_numEntities), m_world); if(makeStatic){ body->setStaticShape(node, shape, 0.6f, //dynamic restitution 0.6f, //dynamic friction position, //starting position Ogre::Quaternion(0, 0, 0, 1) //orientation ); } else { body->setShape(node, shape, 0.6f, //dynamic restitution 0.6f, //dynamic friction 1.0f, //1.0f, //dynamic body mass position, //starting position Ogre::Quaternion(0, 0, 0, 1) //orientation ); } m_numEntities++; m_bodies.push_back(body); m_shapes.push_back(shape); return body; } createScene method //build a chain of connected objects //an anchor OgreBulletDynamics::RigidBody *anchor = buildSphere(Ogre::Vector3(0, 20, 0), true); OgreBulletDynamics::RigidBody *parent = anchor; //then several children for(int i = 1; i <= 10; i++){ OgreBulletDynamics::RigidBody *body = buildSphere(Ogre::Vector3(i*20, 20, 0)); body->disableDeactivation(); //OgreBulletDynamics::PointToPointConstraint *p2p = // new OgreBulletDynamics::PointToPointConstraint(body, Ogre::Vector3(10, 20, 0)); //m_world->addConstraint(p2p); OgreBulletDynamics::HingeConstraint *hConst = new OgreBulletDynamics::HingeConstraint( parent, //object A body, //object B Ogre::Vector3(0, 20, 0), //pivot A Ogre::Vector3(20, 20, 0), //pivot B Ogre::Vector3::UNIT_Y, //axis A Ogre::Vector3::UNIT_Y //axis B ); m_world->addConstraint(hConst); /* btHingeConstraint *btHinge = static_cast(hConst->getBulletTypedConstraint()); if(i == 1){ btHinge->enableMotor(true); btHinge->setMaxMotorImpulse(10000); btHinge->setMotorTarget(Ogre::Math::PI/2, 1); } */ /* OgreBulletDynamics::ConeTwistConstraint *coneTwist = new OgreBulletDynamics::ConeTwistConstraint( parent, //object A body, //object b Ogre::Vector3(0, 20, 0), //frame A Ogre::Quaternion::IDENTITY, //orientation A Ogre::Vector3(20, 20, 0), //frame B Ogre::Quaternion::IDENTITY //orientation B ); m_world->addConstraint(coneTwist); */ /* OgreBulletDynamics::SixDofConstraint *constraint = new OgreBulletDynamics::SixDofConstraint( parent, //object A body, //object b Ogre::Vector3(0, 20, 0), //frame A Ogre::Quaternion::IDENTITY, //orientation A Ogre::Vector3(20, 20, 0), //frame B Ogre::Quaternion::IDENTITY //orientation B ); //parent->disableDeactivation(); //body->disableDeactivation(); //for limits: //free: upper < lower //locked: upper == lower //limited: upper > lower //set linear motion limits constraint->setLinearLowerLimit(Ogre::Vector3(-10, -2, -1)); constraint->setLinearUpperLimit(Ogre::Vector3(10, 2, 1)); //set angular motion limits constraint->setAngularLowerLimit(Ogre::Vector3(-Ogre::Math::PI/2, -Ogre::Math::PI/2, 0)); constraint->setAngularUpperLimit(Ogre::Vector3(Ogre::Math::PI/2, Ogre::Math::PI/2, 0)); //2*Ogre::Math::PI)); //constraint->setAngularUpperLimit(Ogre::Vector3(1, 1, 1)*2*Ogre::Math::PI); //constraint->setLimit(1, 0, 2*Ogre::Math::PI); m_world->addConstraint(constraint); */ parent = body; }