Create a rigid body into the physics world * @param transform Transformation from body local-space to world-space * @return A pointer to the body that has been created in the world */
| 399 | * @return A pointer to the body that has been created in the world |
| 400 | */ |
| 401 | RigidBody* PhysicsWorld::createRigidBody(const Transform& transform) { |
| 402 | |
| 403 | // Create a new entity for the body |
| 404 | Entity entity = mEntityManager.createEntity(); |
| 405 | |
| 406 | // Check that the transform is valid |
| 407 | if (!transform.isValid()) { |
| 408 | RP3D_LOG(mConfig.worldName, Logger::Level::Error, Logger::Category::Body, |
| 409 | "Error when creating a rigid body: the init transform is not valid", __FILE__, __LINE__); |
| 410 | } |
| 411 | assert(transform.isValid()); |
| 412 | |
| 413 | mTransformComponents.addComponent(entity, false, TransformComponents::TransformComponent(transform)); |
| 414 | |
| 415 | // Create the rigid body |
| 416 | RigidBody* rigidBody = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, |
| 417 | sizeof(RigidBody))) RigidBody(*this, entity); |
| 418 | assert(rigidBody != nullptr); |
| 419 | |
| 420 | BodyComponents::BodyComponent bodyComponent(rigidBody); |
| 421 | mBodyComponents.addComponent(entity, false, bodyComponent); |
| 422 | |
| 423 | RigidBodyComponents::RigidBodyComponent rigidBodyComponent(rigidBody, BodyType::DYNAMIC, transform.getPosition()); |
| 424 | mRigidBodyComponents.addComponent(entity, false, rigidBodyComponent); |
| 425 | |
| 426 | // Compute the inverse mass |
| 427 | mRigidBodyComponents.setMassInverse(entity, decimal(1.0) / mRigidBodyComponents.getMass(entity)); |
| 428 | |
| 429 | // Add the rigid body to the physics world |
| 430 | mRigidBodies.add(rigidBody); |
| 431 | |
| 432 | #ifdef IS_RP3D_PROFILING_ENABLED |
| 433 | |
| 434 | rigidBody->setProfiler(mProfiler); |
| 435 | #endif |
| 436 | |
| 437 | RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, |
| 438 | "Body " + std::to_string(entity.id) + ": New collision body created", __FILE__, __LINE__); |
| 439 | |
| 440 | // Return the pointer to the rigid body |
| 441 | return rigidBody; |
| 442 | } |
| 443 | |
| 444 | // Destroy a rigid body and all the joints which it belongs |
| 445 | /** |