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 EQPhysics::RegisterMesh(const std::string &ident, const std::vector<glm::vec3>& verts, const std::vector<unsigned int>& inds, const glm::vec3 &pos, EQPhysicsFlags flag) { UnregisterMesh(ident); if (verts.size() == 0 || inds.size() == 0) { return; } btTriangleMesh *mesh = new btTriangleMesh(); int face_count = (int)inds.size() / 3; for (int i = 0; i < face_count; ++i) { unsigned int i1 = inds[i * 3 + 0]; unsigned int i2 = inds[i * 3 + 1]; unsigned int i3 = inds[i * 3 + 2]; btVector3 v1(verts[i1].x, verts[i1].y, verts[i1].z); btVector3 v2(verts[i2].x, verts[i2].y, verts[i2].z); btVector3 v3(verts[i3].x, verts[i3].y, verts[i3].z); mesh->addTriangle(v1, v2, v3); } btBvhTriangleMeshShape *mesh_shape = new btBvhTriangleMeshShape(mesh, true, true); btTransform origin_transform; origin_transform.setIdentity(); origin_transform.setOrigin(btVector3(pos.x, pos.y, pos.z)); btDefaultMotionState* motionState = new btDefaultMotionState(origin_transform); btRigidBody::btRigidBodyConstructionInfo rb_info(0.0f, motionState, mesh_shape, btVector3(0.0f, 0.0f, 0.0f)); btRigidBody *rb = new btRigidBody(rb_info); imp->collision_world->addRigidBody(rb, (short)flag, (short)flag); imp->entity_info->insert(std::make_pair(ident, btMeshInfo(mesh, mesh_shape, rb))); }
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)); }