----------------------------------- RigidBodySystem::OnComponentAdded Register transform components in the physics world when they are added to the ECS
| 22 | // Register transform components in the physics world when they are added to the ECS |
| 23 | // |
| 24 | void RigidBodySystem::OnComponentAdded(EcsController& controller, RigidBodyComponent& component, T_EntityId const entity) |
| 25 | { |
| 26 | TransformComponent const& transf = controller.GetComponent<TransformComponent>(entity); |
| 27 | |
| 28 | //rigidbody is dynamic if and only if mass is non zero, otherwise static |
| 29 | ET_ASSERT((!component.m_IsDynamic) || (component.m_Mass > 0.f)); |
| 30 | |
| 31 | // initial transform matches transform component |
| 32 | btTransform startTransform; |
| 33 | startTransform.setOrigin(ToBtVec3(transf.GetPosition())); |
| 34 | startTransform.setRotation(ToBtQuat(transf.GetRotation())); |
| 35 | |
| 36 | // initial inertia |
| 37 | btVector3 localInertia(0, 0, 0); |
| 38 | if (component.m_IsDynamic) |
| 39 | { |
| 40 | component.m_CollisionShape->calculateLocalInertia(component.m_Mass, localInertia); |
| 41 | } |
| 42 | |
| 43 | // create rigid body |
| 44 | component.m_Body = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(component.m_Mass, |
| 45 | new btDefaultMotionState(startTransform), |
| 46 | component.m_CollisionShape, |
| 47 | localInertia)); |
| 48 | |
| 49 | // add to world |
| 50 | component.m_Body->setUserIndex(-1); |
| 51 | UnifiedScene::Instance().GetPhysicsWorld().GetWorld()->addRigidBody(component.m_Body); |
| 52 | } |
| 53 | |
| 54 | //------------------------------------- |
| 55 | // RigidBodySystem::OnComponentRemoved |