void PhysXPhysics::AddShape(Actor* pActor, PxGeometry* geometry, float density, const std::string& physicsMaterial, bool gravityEnabled, float linearDamping, float angularDamping, const std::string& bodyType) { BE_ASSERT(pActor); ActorId actorId = pActor->GetId(); BE_ASSERTf(m_actorRigidBodyMap.find(actorId) == m_actorRigidBodyMap.end(), "Actor with more than one rigidbody"); Mat4x4 transform = Mat4x4::g_Identity; TransformComponent* pTransformComponent = pActor->GetComponent<TransformComponent>(TransformComponent::g_Name); if (pTransformComponent) { transform = pTransformComponent->GetTransform(); } else { //Doesnt work without transform BE_ERROR("Actor %s PhysicsComponent requires Shape to have Transform Component: %d", actorId); return; } PhysicsMaterialData material(LookupMaterialData(physicsMaterial)); PxMaterial* mat = m_pPhysicsSdk->createMaterial(material.m_friction, material.m_friction, material.m_restitution); Vec3 translation, scale; Quaternion rotation; bool ok = transform.Decompose(translation, rotation, scale); PxQuat pxRot; PxVec3 pxLoc; Vec3ToPxVec(translation, &pxLoc); QuaternionToPxQuat(rotation, &pxRot); PxTransform t(pxLoc, pxRot); if (bodyType == "Dynamic") { PxRigidDynamic* body = PxCreateDynamic(*m_pPhysicsSdk, t, *geometry, *mat, density); body->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, !gravityEnabled); PxRigidBodyExt::updateMassAndInertia(*body, density); body->setLinearDamping(linearDamping); body->setAngularDamping(angularDamping); m_pScene->addActor(*body); m_actorRigidBodyMap[actorId] = body; m_rigidBodyActorMap[body] = actorId; } else { BE_ERROR("[Physics] BodyType not supported: %s", bodyType.c_str()); return; } }
void BulletPhysics::AddShape(StrongGameObjectPtr pGameObject, btCollisionShape* shape, float mass, const std::string& physicsMaterial) { CB_ASSERT(pGameObject); // make sure this object is only added once GameObjectId id = pGameObject->GetId(); CB_ASSERT(m_ObjectIdToRigidBody.find(id) == m_ObjectIdToRigidBody.end() && "GameObjects can only have one physics body"); // look up material MaterialData material(LookupMaterialData(physicsMaterial)); // local inertia defines how the objects mass is distributed btVector3 localInertia(0.0f, 0.0f, 0.0f); if (mass > 0.0f) { shape->calculateLocalInertia(mass, localInertia); } // get the objects transform Mat4x4 transform = Mat4x4::Identity; shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(pGameObject->GetComponent<TransformComponent>(TransformComponent::g_Name)); CB_ASSERT(pTransformComponent); if (pTransformComponent) { transform = pTransformComponent->GetTransform(); } else { // Physics can't work on an object that doesn't have a TransformComponent return; } ObjectMotionState* motionState = CB_NEW ObjectMotionState(transform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState, shape, localInertia); // set up material properties rbInfo.m_restitution = material.m_Restitution; rbInfo.m_friction = material.m_Friction; // add body to physics world btRigidBody* body = new btRigidBody(rbInfo); m_DynamicsWorld->addRigidBody(body); // update the maps m_ObjectIdToRigidBody[id] = body; m_RigidBodyToObjectId[body] = id; }