BNav * BSpace :: create_sphere(btVector3 &pos, btScalar radius, btScalar mass) { btSphereShape *sphere = new btSphereShape(radius); btTransform transform; transform.setIdentity(); transform.setOrigin(pos); // rigidbody is dynamic if and only if mass is non zero, otherwise static bool is_dynamic = (mass != 0.f); btVector3 local_inertia(0, 0, 0); if(is_dynamic) { sphere->calculateLocalInertia(mass, local_inertia); } //using motionstate is recommended, it provides interpolation capabilities, // and only synchronizes 'active' objects btDefaultMotionState* motion_state = new btDefaultMotionState(transform); btRigidBody::btRigidBodyConstructionInfo rbInfo( mass, motion_state, sphere, local_inertia ); // Dynamics btRigidBody *body = new btRigidBody(rbInfo); return new BNav(this, BNav::SPHERE, sphere, body); }
void Entity::InitializePhysics() { btTransform transform(btQuaternion(0, 0, 0, 1), btVector3(mPosition.x, mPosition.y, 0)); transform.setIdentity(); //btTransform transform; transform.setIdentity(); btScalar mass(1.f); btVector3 local_inertia(0, 0, 0); if(mUID.substr(0,5) == "empty") { mCollisionShape = boost::shared_ptr<btCollisionShape>(new btBoxShape(btVector3(15*mScale, 1*mScale, 1))); } else if(mImageFile == "evil1") { mCollisionShape = boost::shared_ptr<btCollisionShape>(new btBoxShape(btVector3(0.5*mScale, 0.22*mScale, 1))); } else if(mImageFile == "evil2") { mCollisionShape = boost::shared_ptr<btCollisionShape>(new btBoxShape(btVector3(0.5*mScale, 0.05*mScale, 1))); } else { mCollisionShape = boost::shared_ptr<btCollisionShape>(new btBoxShape(btVector3(1.2*mScale, 1.2*mScale, 1))); } mCollisionShape->calculateLocalInertia(mass, local_inertia); transform.setOrigin(btVector3(mPosition.x, mPosition.y, 0)); transform.setRotation(btQuaternion(0, 0, PI - mRotation)); mMotionState = boost::shared_ptr<btDefaultMotionState>(new btDefaultMotionState(transform)); btRigidBody::btRigidBodyConstructionInfo rb_info(mass, mMotionState.get(), mCollisionShape.get(), local_inertia); mBody = boost::shared_ptr<btRigidBody>(new btRigidBody(rb_info)); mBody->setDamping(0.2f, 0.2f); //mBody->setActivationState(DISABLE_DEACTIVATION); mBody->setLinearFactor(btVector3(1,1,0)); mBody->setAngularFactor(btVector3(0,0,1)); //mBody->setAngularFactor(btVector3(0,0,1)); mBody->setUserPointer(this); }
void PhysicsActor::set_scale(Ogre::Vector3 scale) { rigid_body_->getCollisionShape()->setLocalScaling(toBt(scale)); btVector3 local_inertia(0, 0, 0); rigid_body_->getCollisionShape()->calculateLocalInertia(mass_, local_inertia); rigid_body_->setMassProps(mass_, local_inertia); rigid_body_->updateInertiaTensor(); // This is required to make the new size update immediately, not sure why if (physics_enabled_) { removeActorFromDynamicsWorld(); addActorToDynamicsWorld(); } }
void CreatureNode::AddToWorld(btDynamicsWorld *world, const btVector3 &pos) { btTransform start_transform; start_transform.setIdentity(); start_transform.setOrigin(pos); btDefaultMotionState *motion_state = new btDefaultMotionState(start_transform); //TODO: calculate mass as density * volume using hierarchical scale btScalar mass(1.f); btVector3 local_inertia(0, 0, 0); shape_->calculateLocalInertia(mass, local_inertia); btRigidBody::btRigidBodyConstructionInfo info(mass, motion_state, shape_.get(), local_inertia); body_ = new btRigidBody(info); world->addRigidBody(body_); }
void make_floor(std::unique_ptr<PhysicsWorld>& world) { btTransform trans; trans.setIdentity(); trans.setOrigin(btVector3(0.0, -5.0, 0.0)); btScalar mass(0.0); btVector3 local_inertia(0, 0, 0); btStaticPlaneShape* shape = new btStaticPlaneShape(btVector3(0.0f, 1.0f, 0.0f), 0.0f); world->get_collision_shapes().push_back(shape); btDefaultMotionState* motion_state = new btDefaultMotionState(trans); shape->calculateLocalInertia(mass, local_inertia); btRigidBody::btRigidBodyConstructionInfo body_info(mass, motion_state, shape, local_inertia); btRigidBody* body = new btRigidBody(body_info); body->setRestitution(0); world->get_dynamics_world()->addRigidBody(body); }
void OCModel::init() { collision_configuration_ = new btDefaultCollisionConfiguration(); dispatcher_ = new btCollisionDispatcher(collision_configuration_); broadphase_ = new btDbvtBroadphase(); solver_ = new btSequentialImpulseConstraintSolver; dynamics_world_ = new btDiscreteDynamicsWorld(dispatcher_, broadphase_, solver_, collision_configuration_); dynamics_world_->setGravity(btVector3(0, -10, 0)); dynamics_world_->setDebugDrawer(new OCDebugDraw()); //Temporary stuff to get it working btBoxShape *ground_shape = new btBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.))); btTransform ground_transform; ground_transform.setIdentity(); ground_transform.setOrigin(btVector3(0, -50, 0)); btScalar mass(0.); btVector3 local_inertia(0, 0, 0); btDefaultMotionState *my_motion_state = new btDefaultMotionState(ground_transform); btRigidBody::btRigidBodyConstructionInfo ground_info(mass, my_motion_state, ground_shape, local_inertia); btRigidBody *ground = new btRigidBody(ground_info); dynamics_world_->addRigidBody(ground); //collision_shapes_.push_back(col_shape); //std::cout << "local inertia: ( " << local_inertia.x() << ", " << local_inertia.y() << ", " << local_inertia.z() << " )\n"; //CreatureNode *creature = new CreatureNode(1, 1, btVector3(1, 1, 1)); //CreatureNode *creature = new CreatureNode(1, 1, col_shape); //creatures_.push_back(creature); //creature->AddToWorld(dynamics_world_, btVector3( // btScalar(start_x + 20), // btScalar(20 + start_y), // btScalar(start_z + 20))); mass = 1.f; btTransform start_transform; start_transform.setIdentity(); start_transform.setOrigin(btVector3( btScalar(-5), btScalar(15), btScalar(-5))); my_motion_state = new btDefaultMotionState(start_transform); btBoxShape *col_shape = new btBoxShape(btVector3(1, 1, 1)); col_shape->calculateLocalInertia(mass, local_inertia); btRigidBody::btRigidBodyConstructionInfo rb_info(mass, my_motion_state, col_shape, local_inertia); btRigidBody *body = new btRigidBody(rb_info); dynamics_world_->addRigidBody(body); CreatureNode *c = new CreatureNode(1, 1, btVector3(2, 3, 2)); c->AddToWorld(dynamics_world_, btVector3(0, 15, 0)); c = new CreatureNode(1, 1, c->shape_); c->AddToWorld(dynamics_world_, btVector3(10, 15, 10)); }