void Humanoid::InstantiateAtom() { //a single cuboid girder to cover this cell Ogre::SceneManager& sceneManager = GetSceneManager(); m_pAtomEntity = sceneManager.createEntity("human_" + num2string(m_AtomID), "ninja.mesh"); m_pAtomEntitySceneNode->attachObject(m_pAtomEntity); m_pAtomEntitySceneNode->scale(0.004f, 0.004f, 0.004f); m_pAtomEntitySceneNode->setPosition(0, -0.4f, 0); //create physics collider btVector3 halfExtents = btVector3(0.15f, 0.4f, 0.1f); m_pCollisionShape = new btBoxShape(halfExtents); btDefaultMotionState* startMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), OGRE2BT(m_pAtomEntitySceneNode->_getDerivedPosition()))); btScalar mass = 100.f; btVector3 fallInertia(0,0,0); m_pCollisionShape->calculateLocalInertia(mass,fallInertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, startMotionState, m_pCollisionShape, fallInertia); m_pRigidBody = new btRigidBody(rigidBodyCI); m_pRigidBody->setUserPointer(this); m_pRigidBody->setAngularFactor(0); InitCollisionShapeDebugDraw(Ogre::ColourValue::Green); //todo: is this working? //m_pRigidBody->setCollisionFlags(m_pRigidBody->CF_NO_CONTACT_RESPONSE); //add new rigid body to world btDiscreteDynamicsWorld& dynamicsWorld = GetDynamicsWorld(); dynamicsWorld.addRigidBody(m_pRigidBody, COLLISION_MOB, COLLISION_STRUCTURE|COLLISION_OBJ|COLLISION_MOB); Mob::InstantiateAtom(); }
Physics::Physics(GameObject* attachedGameObject, float mass, btCollisionShape* collider, Ogre::Vector3 opos, bool collides) { //Create rigidbody // std::cout << "attachedGameObject " << (&attachedGameObject != NULL) << std::endl; gameObject = attachedGameObject; mTransform = gameObject->transform; btQuaternion rot = btQuaternion(0,0,0); btVector3 pos = btVector3(opos.x,opos.y,opos.z); btTransform transform = btTransform(rot, pos); btDefaultMotionState* motionState = new btDefaultMotionState(transform); // std::cout << "<2>" << std::endl; btVector3 inertia = btVector3(0, 0, 0); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collider, inertia); //Last argument is inertia mRigidBody = new btRigidBody(rigidBodyCI); mRigidBody->setUserPointer(gameObject); mRigidBody->setCollisionFlags(mRigidBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); mRigidBody->setRestitution(0); mRigidBody->setDamping(0, 0); mRigidBody->setActivationState(DISABLE_DEACTIVATION); // std::cout << "<3>" << std::endl; //Add rigidbody to world float multiplier = 50; mGravity = btVector3(0,multiplier*-980,0); if (!collides) mGravity =btVector3(0,0,0); mRigidBody->setGravity(mGravity); //instance of btDiscreteDynamicsWorld->addRigidBody(mRigidBody) gameObject->game->getPhysicsSimulator()->addObject(mRigidBody, collides); //std::cout << "COLLIDER AT " << pos.x() << ", " << pos.y() << ", " << pos.z() << std::endl; // std::cout << "<4>" << std::endl; enabled = true; }
btRigidBody *physics_system_t::create_rigid_heightmap(class heightmap_t &heightmap) { int ds_width = heightmap.width / 4; int ds_height = heightmap.height / 4; float *data = new float[ds_width * ds_height]; float dy = heightmap.xyz_high.y - heightmap.xyz_low.y; for (int i = 0; i < ds_width * ds_height; i++) { int ds_x = i % ds_width; int ds_y = i / ds_width; int lookup = ds_x * 4 + ds_y * 4 * heightmap.width; data[i] = heightmap.xyz_low.y + dy * heightmap.heights[lookup]; } btHeightfieldTerrainShape *shape = new btHeightfieldTerrainShape(ds_width, ds_height, data, 1.0f, -50, 50, 1, PHY_FLOAT, false); float scale_x = (heightmap.xyz_high.x - heightmap.xyz_low.x) / ds_width; float scale_z = (heightmap.xyz_high.z - heightmap.xyz_low.z) / ds_height; shape->setLocalScaling(btVector3(scale_x, 1.0f, scale_z)); btDefaultMotionState *motionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0))); btVector3 inertia(0, 0, 0); shape->calculateLocalInertia(0.0f, inertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0.0f, motionState, shape, inertia); btRigidBody *rigidBody = new btRigidBody(rigidBodyCI); //delete[] data; return rigidBody; }
void PhysicalBodyConvexHull::updateBody() { if (_vertices) _collShape.reset( new btConvexHullShape((btScalar*)_vertices, _vertexCount, sizeof(Vertex)) );//32); else if (_verticesvec) _collShape.reset( new btConvexHullShape((btScalar*)_verticesvec, _vertexCount, sizeof(glm::vec3)) );//32); /* _collShape = new btConvexHullShape; for (int i = 0; i < _vertexCount; i++) { btVector3 point(btVector3(_vertices[i].Position[0], _vertices[i].Position[1], _vertices[i].Position[2])); dynamic_cast<btConvexHullShape>(_collShape->addPoint(btVector3(0,0,0))); } */ btVector3 inertia(0, 0, 0); _collShape->calculateLocalInertia(_mass, inertia); _motionState.reset( new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), _position)) ); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(_mass, _motionState.get(), _collShape.get(), inertia); _rigidBody.reset( new btRigidBody(rigidBodyCI) ); }
/** * Scene_Container owns this method because I, Dylan, have made an executive decision to make lights bound to * the laws of physics impossible; I see no point for it (except for flashlights maybe). Our lights will be static, * so the initialization of rigid bodies can be the sole responsibility of the Scene_Container, because it knows * about all of its own vertices. * * TODO: Support more than just Convex Hull Shape for collision detection; Triangle Mesh Shape might be more * appropriate for static game objects. **/ void Scene_Container::InitRigidBody(btScalar mass) { // Create the collision shape CreateCollisionShape(); btVector3 localInertia(0, 0, 0); isDynamic = (mass != 0.f); if (isDynamic) collisionShape->calculateLocalInertia(mass, localInertia); btTransform* transform = new btTransform( btQuaternion(this->transform.rotation.x, this->transform.rotation.y, this->transform.rotation.z, this->transform.rotation.w), btVector3(this->transform.position.x, this->transform.position.y, this->transform.position.z)); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( mass, new btDefaultMotionState(*transform), collisionShape, localInertia); rigidBody = new btRigidBody(rigidBodyCI); rigidBody->setUserPointer(this); Physics_Manager::GetInstance()->AddRigidBody(rigidBody); }
void SceneObject_PhysicsBox::OnAdd() { // Rendering m_pModel = new SceneObject_Prop(); GetScene()->Add(m_pModel, true); m_pModel->Create("data/models/crate1.obj"); // Get reference to physics world m_pPhysicsWorld = static_cast<SceneObject_PhysicsWorld*>(GetScene()->GetNamed_SceneObject("physWrld")); m_physicsWorldTracker.Set(m_pPhysicsWorld); assert(m_pPhysicsWorld != NULL); // Physics m_pCollisionShape = new btBoxShape(bt(m_pModel->GetAABB().GetHalfDims())); m_pMotionState = new btDefaultMotionState(btTransform(btQuaternion(1.0f, 0.0f, 0.0f, 0.0f).normalized(), bt(m_aabb.GetCenter()))); const float mass = 1.0f; btVector3 intertia; m_pCollisionShape->calculateLocalInertia(mass, intertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, m_pMotionState, m_pCollisionShape, intertia); m_pRigidBody = new btRigidBody(rigidBodyCI); m_pPhysicsWorld->m_pDynamicsWorld->addRigidBody(m_pRigidBody); m_created = true; }
btRigidBody * GameObject::initializeRigidBody(BODY_SHAPE shape) { _bodyShape = shape; /*if(shape == SHAPE_TRIANGLE_MESH) { _triangleMesh = new btTriangleMesh(); std::vector<glm::vec3> worldVertices = _model->getWorldVertices(); size_t numTriangles = worldVertices.size()/3; for(size_t i = 0; i < numTriangles; ++i) { const btVector3 v1 = glmVecToBullet(worldVertices[i*3]); const btVector3 v2 = glmVecToBullet(worldVertices[i*3+1]); const btVector3 v3 = glmVecToBullet(worldVertices[i*3+2]); _triangleMesh->addTriangle(v1, v2, v3); } _triangleShape = new btBvhTriangleMeshShape(_triangleMesh, false); btVector3 localInertia(0, 0, 0); _triangleShape->calculateLocalInertia(1, localInertia); _motionstate = new btDefaultMotionState(btTransform( btQuaternion(_orientation[0], _orientation[1], _orientation[2], _orientation[3]), btVector3(_position[0], _position[1], _position[2]) )); _rigidBody = new btRigidBody(1, _motionstate, _triangleShape, localInertia); _rigidBody->setContactProcessingThreshold(BT_LARGE_FLOAT); _rigidBody->setCcdMotionThreshold(.5); _rigidBody->setCcdSweptSphereRadius(0); } */ //else if(shape == SHAPE_CUBE) //{ _boxCollisionShape = new btBoxShape(btVector3(_model->_scale[0], _model->_scale[1], _model->_scale[2])); _motionstate = new btDefaultMotionState(btTransform( btQuaternion(_orientation[0], _orientation[1], _orientation[2], _orientation[3]), btVector3(_position[0], _position[1], _position[2]) )); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( 0, // mass, in kg. 0 -> Static object, will never move. _motionstate, _boxCollisionShape, // collision shape of body btVector3(0,0,0) // local inertia ); _rigidBody = new btRigidBody(rigidBodyCI); _rigidBody->setUserPointer((void*)this); //} return _rigidBody; }
btRigidBody *physics_system_t::create_rigid_cube(float radius, float mass) { btCollisionShape *shape = new btBoxShape(btVector3(radius, radius, radius)); btDefaultMotionState *motionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0))); btVector3 inertia(0, 0, 0); shape->calculateLocalInertia(mass, inertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, shape, inertia); btRigidBody *rigidBody = new btRigidBody(rigidBodyCI); return rigidBody; }
void Entity :: setPhysics(float mass){ properties = new PhysProperty(); properties->motionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,-1,0))); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( mass, // mass properties->motionState, // initial position body->shape, // collision shape of body btVector3(0,0,0) // local inertia ); properties->rigidBody = new btRigidBody(rigidBodyCI); }
/** * Processes the properties of the body, shape, mass friction, etc... */ void RigidBody::processBody_() { // Remove the current body from the physics engine selfRemoveBody_(); if(!shape_) { return; } shape_->calculateLocalInertia(mass_, inertia_); float x = getX(); float y = getY(); float z = getZ(); if(body_) { delete(body_); }// else { //btTransform xform; //xform = motionState_.getWorldTransform(); //xform.setRotation(btQuaternion (btVector3(getRotX(), getRotY(), getRotZ()), getRotAngle()*(PI/180))); //xform.setOrigin( //((btPairCachingGhostObject*)body_)->setWorldTransform (xform); //motionState_->setWorldTransform (xform); btTransform xform; motionState_->getWorldTransform(xform); xform.setOrigin(btVector3(getX(), getY(), getZ())); motionState_->setWorldTransform(xform); //} btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( mass_, motionState_, shape_, inertia_ ); rigidBodyCI.m_friction = friction_; rigidBodyCI.m_mass = mass_; body_ = new btRigidBody(rigidBodyCI); setXYZ(x, y, z); selfAddBody_(); /*btRigidBody* rb = dynamic_cast<btRigidBody*>(body_); if(rb) { rb->clearForces(); }*/ stopMovement(); }
void PhysicsObject::initPhysics(btCollisionShape* shape, btDefaultMotionState* state, bool ground ) { m_shape = shape; m_motionState = state; m_fallInertia = btVector3(0, 0, 0); if (!ground) { m_shape->calculateLocalInertia(m_mass, m_fallInertia); } btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( m_mass, state, m_shape, m_fallInertia); m_rigidBody = new btRigidBody(rigidBodyCI); }
MoveableObject::MoveableObject(ObjectType type, btMotionState* pose, btCollisionShape* collision_shape, double weight) { btCollisionShape* shape; btVector3 inertia; shape = collision_shape; if (type == ObjectType::SIMULATED) { shape->calculateLocalInertia(weight, inertia); assert(inertia != btVector3(0, 0, 0)); } btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(weight, pose, shape, inertia); rigid_body = std::make_unique<btRigidBody>(rigidBodyCI); }
//Agregar cuerpo al mundo void Esfera::agregarCuerpo(btDiscreteDynamicsWorld *world){ shape = new btSphereShape(RADIO); motionState = new btDefaultMotionState( btTransform(btQuaternion(rand(),rand(),rand(),1), btVector3(this->x,this->y,this->z)) ); btScalar mass = MASA; btVector3 inertia(0,0,0); shape->calculateLocalInertia(mass,inertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass,motionState,shape,inertia); rigidBody = new btRigidBody(rigidBodyCI); rigidBody->setRestitution(0.9); rigidBody->setDamping(0,0.8); world->addRigidBody(rigidBody); }
CollisionPart create_collision_part(const Mesh &mesh, bool changeUp, bool move) { CollisionPart collision; collision.convexHullShape = std::make_shared<btConvexHullShape>(); for(const poly &p : mesh){ for(const Vertex &v : p.vertexes){ collision.convexHullShape->addPoint(btVector3((float)v.pos.x,(float)v.pos.y,(float)v.pos.z)); } } collision.motionState = std::make_shared<btDefaultMotionState>(); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0,collision.motionState.get(),collision.convexHullShape.get(),btVector3(0,0,0)); collision.rigidBody = std::make_shared<btRigidBody>(rigidBodyCI); collision.changeUp = changeUp; collision.move = move; return collision; }
void CapsuleElement::build(SceneManager* sceneManager, btDiscreteDynamicsWorld* dynamicsWorld) { // create graphics object for render SimpleGraphicsElmBuilder::getInstance()->createCapsule(name + "_physicSCapsule", size.getX()/2, size.getY()); entity = sceneManager->createEntity(name, name + "_physicSCapsule"); node = sceneManager->getRootSceneNode()->createChildSceneNode(name); node->attachObject(entity); // create physics object for simylation motionState = new MyMotionState(btTransform(startOrientation, startPos), node); inertia = btVector3(1, 1, 1); collisionShape = new btCapsuleShape(size.getX()/2,size.getY()); collisionShape->calculateLocalInertia(mass, inertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collisionShape, inertia); rigidBody = new btRigidBody(rigidBodyCI); dynamicsWorld->addRigidBody(rigidBody); }
SceneryEntity::SceneryEntity(const char *name, const char *entityTemplateName) : VisibleEntity(name, entityTemplateName) { SceneryEntityTemplate *entTemplate = static_cast<SceneryEntityTemplate*>(Core::GetInstance().entityTemplateManager.GetTemplate(entityTemplateName)); *(static_cast<SceneryEntityProperties *>(this)) = *(static_cast<SceneryEntityProperties*>(entTemplate)); entity = Core::GetInstance().ogreSceneMgr->createEntity(name, entTemplate->meshName); node = Core::GetInstance().ogreSceneMgr->getRootSceneNode()->createChildSceneNode(name); node->attachObject(entity); if (collidable) { if (boundingVolumeType == BVT_BOX) { collisionShape = new btBoxShape(boxSize); } else if (boundingVolumeType == BVT_SPHERE) { collisionShape = new btSphereShape(radius); } btDefaultMotionState *motionState = new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(0,0,0)) ); btVector3 inertia(0,0,0); collisionShape->calculateLocalInertia(mass, inertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI( mass, motionState, collisionShape, inertia ); body = new btRigidBody(rigidBodyCI); body->setUserPointer(static_cast<void *>(this)); Core::GetInstance().bulWorld->addRigidBody(body); } }
void RigidBody::start() { const Collider *collider = gameObject->get<Collider>(); if (!collider) return; glm::vec3 translation = gameObject->getTranslation(); glm::vec3 rotation = gameObject->getRotation(); btVector3 position(translation.x, translation.y, translation.z); btQuaternion quaternion(rotation.x, rotation.y, rotation.z); btVector3 inertia(0, 0, 0); collider->shape->calculateLocalInertia(mass, inertia); btDefaultMotionState *motionState = new btDefaultMotionState(btTransform(quaternion, position)); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collider->shape, inertia); rigidBody = new btRigidBody(rigidBodyCI); Command::physics.push(new AddRigidBodyCommand(rigidBody)); rigidBodies.emplace(rigidBody, this); }
bool SceneObject_Prop_Physics_Static::Create(const std::string &modelName, const Vec3f &position, const Quaternion &rotation, float restitution, float friction) { assert(!m_created); assert(GetScene() != NULL); Asset* pModelAsset; if(!GetScene()->GetAssetManager_AutoCreate("modelOBJPhy", Model_OBJ_Physics_Static::Asset_Factory)->GetAsset(modelName, pModelAsset)) return false; m_pModel = static_cast<Model_OBJ_Physics_Static*>(pModelAsset); m_pModel->SetRenderer(GetScene()); // Get reference to physics world m_pPhysicsWorld = static_cast<SceneObject_PhysicsWorld*>(GetScene()->GetNamed_SceneObject("physWrld")); assert(m_pPhysicsWorld != NULL); m_physicsWorldTracker.Set(m_pPhysicsWorld); m_pMotionState = new btDefaultMotionState(btTransform(bt(rotation).normalized(), bt(position))); const float mass = 0.0f; btVector3 intertia; m_pModel->GetShape()->calculateLocalInertia(mass, intertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, m_pMotionState, m_pModel->GetShape(), intertia); rigidBodyCI.m_restitution = restitution; rigidBodyCI.m_friction = friction; m_pRigidBody = new btRigidBody(rigidBodyCI); m_pPhysicsWorld->m_pDynamicsWorld->addRigidBody(m_pRigidBody); m_pRigidBody->setUserPointer(this); // Default texture setting: nearest filtering /*for(unsigned int i = 0, size = m_pModel->GetNumMaterials(); i < size; i++) { Model_OBJ::Material* pMat = m_pModel->GetMaterial(i); pMat->m_pDiffuseMap->Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if(pMat->m_pSpecularMap != NULL) { pMat->m_pSpecularMap->Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } if(pMat->m_pNormalMap != NULL) { pMat->m_pNormalMap->Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } }*/ RegenAABB(); m_created = true; return true; }