Exemplo n.º 1
0
void ModelPhysics::insert(ModelInstance & modelInst,
                          f32 mass,
                          f32 friction,
                          vec3 linearFactor,
                          vec3 angularFactor,
                          u32 group,
                          const ivec4 & mask03,
                          const ivec4 & mask47)
{
    if (mBodies.find(modelInst.model().uid()) == mBodies.end())
    {
        ASSERT(modelInst.mHasBody == false);

        vec3 halfExtents = modelInst.model().gmdl().halfExtents();
        auto colShapeIt = mCollisionShapes.find(halfExtents);
        btVector3 btExtents(halfExtents.x, halfExtents.y, halfExtents.z);

        btCollisionShape * pCollisionShape = nullptr;
        if (colShapeIt != mCollisionShapes.end())
            pCollisionShape = colShapeIt->second.get();
        else
        {
            auto empRes = mCollisionShapes.emplace(std::piecewise_construct,
                                                   std::forward_as_tuple(halfExtents),
                                                   std::forward_as_tuple(GNEW(kMEM_Physics, btBoxShape, btExtents)));
            pCollisionShape = empRes.first->second.get();
        }

        ModelMotionState * pMotionState = GNEW(kMEM_Physics, ModelMotionState, modelInst);
        btRigidBody::btRigidBodyConstructionInfo constrInfo(mass, pMotionState, pCollisionShape);
        constrInfo.m_friction = friction;

//        constrInfo.m_angularDamping = 0.1;
//        constrInfo.m_localInertia = btExtents;

        ModelBody * pBody = GNEW(kMEM_Physics, ModelBody, pMotionState, group, constrInfo);
        mBodies.emplace(modelInst.model().uid(), pBody);

        pBody->setLinearFactor(btVector3(linearFactor.x, linearFactor.y, linearFactor.z));
        pBody->setAngularFactor(btVector3(angularFactor.x, angularFactor.y, angularFactor.z));

        if (group == 0)
        {
            mpDynamicsWorld->addRigidBody(pBody);
        }
        else
        {
            u16 groupMask = maskFromHash(group);
            u16 mask = buildMask(mask03, mask47);
            mpDynamicsWorld->addRigidBody(pBody, maskFromHash(group), buildMask(mask03, mask47));
        }
        pBody->setGravity(btVector3(0,0,0));
    }
    else
    {
        LOG_ERROR("ModelBody for %u already created", modelInst.model().uid());
    }
}
Exemplo n.º 2
0
		btCollisionShape * PhysicsAssetExporter::createBoxShape(float * extents)
		{
			btVector3 btExtents(extents[0], extents[1], extents[2]);
			return new btBoxShape(btExtents);
		}