diff --git a/include/reactphysics3d/body/CollisionBody.h b/include/reactphysics3d/body/CollisionBody.h index f01c896f..c7a14826 100644 --- a/include/reactphysics3d/body/CollisionBody.h +++ b/include/reactphysics3d/body/CollisionBody.h @@ -62,12 +62,6 @@ class CollisionBody { /// Reference to the world the body belongs to PhysicsWorld& mWorld; -#ifdef IS_LOGGING_ACTIVE - - /// Logger - Logger* mLogger; -#endif - #ifdef IS_PROFILING_ACTIVE /// Pointer to the profiler @@ -163,12 +157,6 @@ class CollisionBody { /// Return the body local-space coordinates of a vector given in the world-space coordinates Vector3 getLocalVector(const Vector3& worldVector) const; -#ifdef IS_LOGGING_ACTIVE - - /// Set the logger - void setLogger(Logger* logger); -#endif - #ifdef IS_PROFILING_ACTIVE /// Set the profiler @@ -202,15 +190,6 @@ inline Entity CollisionBody::getEntity() const { return mEntity; } -#ifdef IS_LOGGING_ACTIVE - -// Set the logger -inline void CollisionBody::setLogger(Logger* logger) { - mLogger = logger; -} - -#endif - #ifdef IS_PROFILING_ACTIVE // Set the profiler diff --git a/include/reactphysics3d/collision/Collider.h b/include/reactphysics3d/collision/Collider.h index b370c645..92d50d1d 100644 --- a/include/reactphysics3d/collision/Collider.h +++ b/include/reactphysics3d/collision/Collider.h @@ -72,12 +72,6 @@ class Collider { #endif -#ifdef IS_LOGGING_ACTIVE - - /// Logger - Logger* mLogger; -#endif - // -------------------- Methods -------------------- // /// Notify the collider that the size of the collision shape has been @@ -173,12 +167,6 @@ class Collider { #endif -#ifdef IS_LOGGING_ACTIVE - - /// Set the logger - void setLogger(Logger* logger); -#endif - // -------------------- Friendship -------------------- // friend class OverlappingPair; @@ -245,32 +233,6 @@ inline Material& Collider::getMaterial() { return mMaterial; } -// Set a new material for this rigid body -/** - * @param material The material you want to set to the body - */ -inline void Collider::setMaterial(const Material& material) { - - mMaterial = material; - -#ifdef IS_LOGGING_ACTIVE - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, - "Collider " + std::to_string(mEntity.id) + ": Set Material" + mMaterial.to_string(), __FILE__, __LINE__); -#endif - -} - -#ifdef IS_LOGGING_ACTIVE - -// Set the logger -inline void Collider::setLogger(Logger* logger) { - - mLogger = logger; -} - -#endif - } #endif diff --git a/include/reactphysics3d/engine/PhysicsCommon.h b/include/reactphysics3d/engine/PhysicsCommon.h index dc5917e6..54de743f 100644 --- a/include/reactphysics3d/engine/PhysicsCommon.h +++ b/include/reactphysics3d/engine/PhysicsCommon.h @@ -83,21 +83,31 @@ class PhysicsCommon { /// Set of triangle meshes Set mTriangleMeshes; -#ifdef IS_LOGGING_ACTIVE + /// Pointer to the current logger + static Logger* mLogger; - /// Set of loggers - Set mLoggers; - -#endif - - /// Set of loggers + /// Set of profilers Set mProfilers; + /// Set of default loggers + Set mDefaultLoggers; + // -------------------- Methods -------------------- // /// Destroy and release everything that has been allocated void release(); +// If profiling is enabled +#ifdef IS_PROFILING_ACTIVE + + /// Create and return a new profiler + Profiler* createProfiler(); + + /// Destroy a profiler + void destroyProfiler(Profiler* profiler); + +#endif + public : // -------------------- Methods -------------------- // @@ -115,8 +125,7 @@ class PhysicsCommon { // the method parameters with the "@param" keyword for Doxygen /// Create and return an instance of PhysicsWorld - PhysicsWorld* createPhysicsWorld(const PhysicsWorld::WorldSettings& worldSettings = PhysicsWorld::WorldSettings(), - Logger* logger = nullptr, Profiler* profiler = nullptr); + PhysicsWorld* createPhysicsWorld(const PhysicsWorld::WorldSettings& worldSettings = PhysicsWorld::WorldSettings()); /// Destroy an instance of PhysicsWorld void destroyPhysicsWorld(PhysicsWorld* world); @@ -172,30 +181,33 @@ class PhysicsCommon { /// Destroy a triangle mesh void destroyTriangleMesh(TriangleMesh* triangleMesh); -// If logging is enabled -#ifdef IS_LOGGING_ACTIVE - /// Create and return a new default logger DefaultLogger* createDefaultLogger(); /// Destroy a default logger void destroyDefaultLogger(DefaultLogger* logger); -#endif + /// Return the current logger + static Logger* getLogger(); -// If profiling is enabled -#ifdef IS_PROFILING_ACTIVE - - /// Create and return a new profiler - Profiler* createProfiler(); - - /// Destroy a profiler - void destroyProfiler(Profiler* profiler); - -#endif + /// Set the logger + static void setLogger(Logger* logger); }; +// Return the current logger +inline Logger* PhysicsCommon::getLogger() { + return mLogger; +} + +// Set the logger +inline void PhysicsCommon::setLogger(Logger* logger) { + mLogger = logger; +} + +// Use this macro to log something +#define RP3D_LOG(physicsWorldName, level, category, message, filename, lineNumber) if (reactphysics3d::PhysicsCommon::getLogger() != nullptr) PhysicsCommon::getLogger()->log(level, physicsWorldName, category, message, filename, lineNumber) + } #endif diff --git a/include/reactphysics3d/engine/PhysicsWorld.h b/include/reactphysics3d/engine/PhysicsWorld.h index 6bafe402..2a894ae4 100644 --- a/include/reactphysics3d/engine/PhysicsWorld.h +++ b/include/reactphysics3d/engine/PhysicsWorld.h @@ -235,12 +235,6 @@ class PhysicsWorld { Profiler* mProfiler; #endif -#ifdef IS_LOGGING_ACTIVE - - /// Logger - Logger* mLogger; -#endif - /// Total number of worlds static uint mNbWorlds; @@ -287,8 +281,7 @@ class PhysicsWorld { // -------------------- Methods -------------------- // /// Constructor - PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings = WorldSettings(), - Logger* logger = nullptr, Profiler* profiler = nullptr); + PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings = WorldSettings(), Profiler* profiler = nullptr); /// Notify the world if a body is disabled (slepping or inactive) or not void setBodyDisabled(Entity entity, bool isDisabled); @@ -412,7 +405,7 @@ class PhysicsWorld { bool isGravityEnabled() const; /// Enable/Disable the gravity - void setIsGratityEnabled(bool isGravityEnabled); + void setIsGravityEnabled(bool isGravityEnabled); /// Return true if the sleeping technique is enabled bool isSleepingEnabled() const; @@ -473,13 +466,6 @@ class PhysicsWorld { /// Return a reference to the profiler Profiler* getProfiler(); -#endif - -#ifdef IS_LOGGING_ACTIVE - - /// Return a reference to the logger - Logger* getLogger(); - #endif // -------------------- Friendship -------------------- // @@ -610,18 +596,6 @@ inline Profiler* PhysicsWorld::getProfiler() { #endif -#ifdef IS_LOGGING_ACTIVE - -// Return a pointer to the logger -/** - * @return A pointer to the logger - */ -inline Logger* PhysicsWorld::getLogger() { - return mLogger; -} - -#endif - // Get the number of iterations for the velocity constraint solver /** * @return The number of iterations of the velocity constraint solver @@ -630,18 +604,6 @@ inline uint PhysicsWorld::getNbIterationsVelocitySolver() const { return mNbVelocitySolverIterations; } -// Set the number of iterations for the velocity constraint solver -/** - * @param nbIterations Number of iterations for the velocity solver - */ -inline void PhysicsWorld::setNbIterationsVelocitySolver(uint nbIterations) { - - mNbVelocitySolverIterations = nbIterations; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: Set nb iterations velocity solver to " + std::to_string(nbIterations), __FILE__, __LINE__); -} - // Get the number of iterations for the position constraint solver /** * @return The number of iterations of the position constraint solver @@ -650,18 +612,6 @@ inline uint PhysicsWorld::getNbIterationsPositionSolver() const { return mNbPositionSolverIterations; } -// Set the number of iterations for the position constraint solver -/** - * @param nbIterations Number of iterations for the position solver - */ -inline void PhysicsWorld::setNbIterationsPositionSolver(uint nbIterations) { - - mNbPositionSolverIterations = nbIterations; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: Set nb iterations position solver to " + std::to_string(nbIterations), __FILE__, __LINE__); -} - // Set the position correction technique used for contacts /** * @param technique Technique used for the position correction (Baumgarte or Split Impulses) @@ -698,18 +648,6 @@ inline Vector3 PhysicsWorld::getGravity() const { return mConfig.gravity; } -// Set the gravity vector of the world -/** - * @param gravity The gravity vector (in meter per seconds squared) - */ -inline void PhysicsWorld::setGravity(Vector3& gravity) { - - mConfig.gravity = gravity; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: Set gravity vector to " + gravity.to_string(), __FILE__, __LINE__); -} - // Return if the gravity is enaled /** * @return True if the gravity is enabled in the world @@ -718,18 +656,6 @@ inline bool PhysicsWorld::isGravityEnabled() const { return mIsGravityEnabled; } -// Enable/Disable the gravity -/** - * @param isGravityEnabled True if you want to enable the gravity in the world - * and false otherwise - */ -inline void PhysicsWorld::setIsGratityEnabled(bool isGravityEnabled) { - mIsGravityEnabled = isGravityEnabled; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: isGravityEnabled= " + (isGravityEnabled ? std::string("true") : std::string("false")), __FILE__, __LINE__); -} - // Return true if the sleeping technique is enabled /** * @return True if the sleeping technique is enabled and false otherwise @@ -746,21 +672,6 @@ inline decimal PhysicsWorld::getSleepLinearVelocity() const { return mSleepLinearVelocity; } -// Set the sleep linear velocity. -/// When the velocity of a body becomes smaller than the sleep linear/angular -/// velocity for a given amount of time, the body starts sleeping and does not need -/// to be simulated anymore. -/** - * @param sleepLinearVelocity The sleep linear velocity (in meters per second) - */ -inline void PhysicsWorld::setSleepLinearVelocity(decimal sleepLinearVelocity) { - assert(sleepLinearVelocity >= decimal(0.0)); - mSleepLinearVelocity = sleepLinearVelocity; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: sleepLinearVelocity= " + std::to_string(sleepLinearVelocity), __FILE__, __LINE__); -} - // Return the current sleep angular velocity /** * @return The sleep angular velocity (in radian per second) @@ -769,21 +680,6 @@ inline decimal PhysicsWorld::getSleepAngularVelocity() const { return mSleepAngularVelocity; } -// Set the sleep angular velocity. -/// When the velocity of a body becomes smaller than the sleep linear/angular -/// velocity for a given amount of time, the body starts sleeping and does not need -/// to be simulated anymore. -/** - * @param sleepAngularVelocity The sleep angular velocity (in radian per second) - */ -inline void PhysicsWorld::setSleepAngularVelocity(decimal sleepAngularVelocity) { - assert(sleepAngularVelocity >= decimal(0.0)); - mSleepAngularVelocity = sleepAngularVelocity; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: sleepAngularVelocity= " + std::to_string(sleepAngularVelocity), __FILE__, __LINE__); -} - // Return the time a body is required to stay still before sleeping /** * @return Time a body is required to stay still before sleeping (in seconds) @@ -792,18 +688,6 @@ inline decimal PhysicsWorld::getTimeBeforeSleep() const { return mTimeBeforeSleep; } -// Set the time a body is required to stay still before sleeping -/** - * @param timeBeforeSleep Time a body is required to stay still before sleeping (in seconds) - */ -inline void PhysicsWorld::setTimeBeforeSleep(decimal timeBeforeSleep) { - assert(timeBeforeSleep >= decimal(0.0)); - mTimeBeforeSleep = timeBeforeSleep; - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, - "Physics World: timeBeforeSleep= " + std::to_string(timeBeforeSleep), __FILE__, __LINE__); -} - // Set an event listener object to receive events callbacks. /// If you use "nullptr" as an argument, the events callbacks will be disabled. /** diff --git a/include/reactphysics3d/utils/DefaultLogger.h b/include/reactphysics3d/utils/DefaultLogger.h index 106ef2c6..2e30267a 100644 --- a/include/reactphysics3d/utils/DefaultLogger.h +++ b/include/reactphysics3d/utils/DefaultLogger.h @@ -26,9 +26,6 @@ #ifndef REACTPHYSICS3D_DEFAULT_LOGGER_H #define REACTPHYSICS3D_DEFAULT_LOGGER_H -// If logging is enabled -#ifdef IS_LOGGING_ACTIVE - // Libraries #include #include @@ -81,7 +78,7 @@ class DefaultLogger : public Logger { } /// Format a log message - virtual std::string format(const time_t& time, const std::string& message, Level level, Category category, + virtual std::string format(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) = 0; }; @@ -116,13 +113,16 @@ class DefaultLogger : public Logger { } /// Format a log message - virtual std::string format(const time_t& time, const std::string& message, + virtual std::string format(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) override { std::stringstream ss; // Time ss << std::put_time(std::localtime(&time), "%X") << " "; + // World + ss << "World:" << physicsWorldName << std::endl; + // Level ss << getLevelName(level) << " "; @@ -204,8 +204,12 @@ class DefaultLogger : public Logger { "} " ".time { " "margin-right: 20px; " - "width: 10%; " + "width: 5%; " "} " + ".world-name { " + "margin-right: 20px; " + "width: 5%; " + "}" ".level { " "margin-right: 20px; " "width: 10%; " @@ -266,7 +270,7 @@ class DefaultLogger : public Logger { } /// Format a log message - virtual std::string format(const time_t& time, const std::string& message, Level level, + virtual std::string format(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) override { std::stringstream ss; @@ -278,6 +282,11 @@ class DefaultLogger : public Logger { ss << std::put_time(std::localtime(&time), "%X"); ss << ""; + // Message + ss << "
"; + ss << physicsWorldName; + ss << "
"; + // Level ss << "
"; ss << getLevelName(level); @@ -329,7 +338,7 @@ class DefaultLogger : public Logger { } /// Write a message into the output stream - virtual void write(const time_t& time, const std::string& message, Level level, Category category, const char* filename, int lineNumber) = 0; + virtual void write(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) = 0; /// Return the size in bytes of the type virtual size_t getSizeBytes() const=0; @@ -373,11 +382,11 @@ class DefaultLogger : public Logger { } /// Write a message into the output stream - virtual void write(const time_t& time, const std::string& message, Level level, Category category, + virtual void write(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) override { if (static_cast(level) <= static_cast(maxLevelFlag)) { - mFileStream << formatter->format(time, message, level, category, filename, lineNumber) << std::endl << std::flush; + mFileStream << formatter->format(time, physicsWorldName, message, level, category, filename, lineNumber) << std::endl << std::flush; } } @@ -413,11 +422,11 @@ class DefaultLogger : public Logger { } /// Write a message into the output stream - virtual void write(const time_t& time, const std::string& message, Level level, Category category, + virtual void write(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) override { if (static_cast(level) <= static_cast(maxLevelFlag)) { - mOutputStream << formatter->format(time, message, level, category, filename, lineNumber) << std::endl << std::flush; + mOutputStream << formatter->format(time, physicsWorldName, message, level, category, filename, lineNumber) << std::endl << std::flush; } } @@ -469,7 +478,7 @@ class DefaultLogger : public Logger { void removeAllDestinations(); /// Log something - virtual void log(Level level, Category category, const std::string& message, const char* filename, int lineNumber) override; + virtual void log(Level level, const std::string& physicsWorldName, Category category, const std::string& message, const char* filename, int lineNumber) override; // ---------- Friendship ---------- // @@ -491,5 +500,3 @@ namespace std { } #endif - -#endif diff --git a/include/reactphysics3d/utils/Logger.h b/include/reactphysics3d/utils/Logger.h index fa6517bc..db94a84a 100644 --- a/include/reactphysics3d/utils/Logger.h +++ b/include/reactphysics3d/utils/Logger.h @@ -26,9 +26,6 @@ #ifndef REACTPHYSICS3D_LOGGER_H #define REACTPHYSICS3D_LOGGER_H -// If logging is enabled -#ifdef IS_LOGGING_ACTIVE - // Libraries #include #include @@ -91,7 +88,7 @@ class Logger { virtual ~Logger() = default; /// Log something - virtual void log(Level level, Category category, const std::string& message, const char* filename, int lineNumber)=0; + virtual void log(Level level, const std::string& physicsWorldName, Category category, const std::string& message, const char* filename, int lineNumber)=0; // ---------- Friendship ---------- // @@ -100,15 +97,4 @@ class Logger { } -// Use this macro to log something -#define RP3D_LOG(logger, level, category, message, filename, lineNumber) logger->log(level, category, message, filename, lineNumber) - -// If the logging is not enabled -#else - -// Empty macro in case logs are not enabled -#define RP3D_LOG(logger, level, category, message, filename, lineNumber) - -#endif - #endif diff --git a/include/reactphysics3d/utils/Profiler.h b/include/reactphysics3d/utils/Profiler.h index 85ec9698..631cc7e6 100644 --- a/include/reactphysics3d/utils/Profiler.h +++ b/include/reactphysics3d/utils/Profiler.h @@ -310,16 +310,17 @@ class Profiler { /// Destroy the profiler (release the memory) void destroy(); + + public : + + // -------------------- Methods -------------------- // + /// Constructor Profiler(); /// Destructor ~Profiler(); - public : - - // -------------------- Methods -------------------- // - /// Method called when we want to start profiling a block of code. void startProfilingBlock(const char *name); diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index cd6a7b8a..fedb9f7b 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -25,6 +25,7 @@ // Libraries #include +#include #include #include #include @@ -42,10 +43,6 @@ using namespace reactphysics3d; CollisionBody::CollisionBody(PhysicsWorld& world, Entity entity) : mEntity(entity), mWorld(world) { -#ifdef IS_LOGGING_ACTIVE - mLogger = nullptr; -#endif - #ifdef IS_PROFILING_ACTIVE mProfiler = nullptr; #endif @@ -100,13 +97,6 @@ Collider* CollisionBody::addCollider(CollisionShape* collisionShape, const Trans // Set the profiler collider->setProfiler(mProfiler); -#endif - -#ifdef IS_LOGGING_ACTIVE - - // Set the logger - collider->setLogger(mLogger); - #endif // Compute the world-space AABB of the new collision shape @@ -116,10 +106,10 @@ Collider* CollisionBody::addCollider(CollisionShape* collisionShape, const Trans // Notify the collision detection about this new collision shape mWorld.mCollisionDetection.addCollider(collider, aabb); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Collider " + std::to_string(collider->getBroadPhaseId()) + " added to body", __FILE__, __LINE__); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, "Collider " + std::to_string(collider->getBroadPhaseId()) + ": collisionShape=" + collider->getCollisionShape()->to_string(), __FILE__, __LINE__); @@ -170,7 +160,7 @@ Collider* CollisionBody::getCollider(uint colliderIndex) { */ void CollisionBody::removeCollider(Collider* collider) { - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Collider " + std::to_string(collider->getBroadPhaseId()) + " removed from body", __FILE__, __LINE__); // Remove the collider from the broad-phase @@ -281,7 +271,7 @@ void CollisionBody::setIsActive(bool isActive) { } } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set isActive=" + (isActive ? "true" : "false"), __FILE__, __LINE__); } @@ -398,7 +388,7 @@ void CollisionBody::setTransform(const Transform& transform) { // Update the broad-phase state of the body updateBroadPhaseState(0); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set transform=" + transform.to_string(), __FILE__, __LINE__); } diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index e0f43338..2c242c48 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -25,6 +25,7 @@ // Libraries #include +#include #include #include #include @@ -114,7 +115,7 @@ void RigidBody::setType(BodyType type) { mWorld.mRigidBodyComponents.setExternalForce(mEntity, Vector3::zero()); mWorld.mRigidBodyComponents.setExternalTorque(mEntity, Vector3::zero()); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set type=" + (type == BodyType::STATIC ? "Static" : (type == BodyType::DYNAMIC ? "Dynamic" : "Kinematic")), __FILE__, __LINE__); } @@ -212,7 +213,7 @@ void RigidBody::setLocalInertiaTensor(const Vector3& inertiaTensorLocal) { inertiaTensorLocal.z != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.z : 0); mWorld.mRigidBodyComponents.setInverseInertiaTensorLocal(mEntity, inverseInertiaTensorLocal); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set inertiaTensorLocal=" + inertiaTensorLocal.to_string(), __FILE__, __LINE__); } @@ -275,7 +276,7 @@ void RigidBody::setLocalCenterOfMass(const Vector3& centerOfMass) { linearVelocity += angularVelocity.cross(centerOfMassWorld - oldCenterOfMass); mWorld.mRigidBodyComponents.setLinearVelocity(mEntity, linearVelocity); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set centerOfMassLocal=" + centerOfMass.to_string(), __FILE__, __LINE__); } @@ -308,7 +309,7 @@ void RigidBody::updateLocalCenterOfMassFromColliders() { linearVelocity += angularVelocity.cross(centerOfMassWorld - oldCenterOfMassWorld); mWorld.mRigidBodyComponents.setLinearVelocity(mEntity, linearVelocity); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set centerOfMassLocal=" + centerOfMassLocal.to_string(), __FILE__, __LINE__); } @@ -415,7 +416,7 @@ void RigidBody::updateLocalInertiaTensorFromColliders() { inertiaTensorLocal.z != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.z : 0); mWorld.mRigidBodyComponents.setInverseInertiaTensorLocal(mEntity, inverseInertiaTensorLocal); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set inertiaTensorLocal=" + inertiaTensorLocal.to_string(), __FILE__, __LINE__); } @@ -451,7 +452,7 @@ void RigidBody::updateMassFromColliders() { mWorld.mRigidBodyComponents.setMassInverse(mEntity, decimal(0.0)); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set mass=" + std::to_string(totalMass), __FILE__, __LINE__); } @@ -481,7 +482,7 @@ void RigidBody::updateMassPropertiesFromColliders() { linearVelocity += angularVelocity.cross(centerOfMassWorld - oldCenterOfMassWorld); mWorld.mRigidBodyComponents.setLinearVelocity(mEntity, linearVelocity); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set centerOfMassLocal=" + centerOfMassLocal.to_string(), __FILE__, __LINE__); // Compute the mass and local-space inertia tensor @@ -497,7 +498,7 @@ void RigidBody::updateMassPropertiesFromColliders() { inertiaTensorLocal.z != decimal(0.0) ? decimal(1.0) / inertiaTensorLocal.z : 0); mWorld.mRigidBodyComponents.setInverseInertiaTensorLocal(mEntity, inverseInertiaTensorLocal); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set inertiaTensorLocal=" + inertiaTensorLocal.to_string(), __FILE__, __LINE__); // Set the mass @@ -511,7 +512,7 @@ void RigidBody::updateMassPropertiesFromColliders() { mWorld.mRigidBodyComponents.setMassInverse(mEntity, decimal(0.0)); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set mass=" + std::to_string(totalMass), __FILE__, __LINE__); } @@ -534,12 +535,12 @@ void RigidBody::setMass(decimal mass) { if (mWorld.mRigidBodyComponents.getMass(mEntity) < decimal(0.0)) { - RP3D_LOG(mLogger, Logger::Level::Error, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Error, Logger::Category::Body, "Error when setting mass of body " + std::to_string(mEntity.id) + ": mass cannot be negative", __FILE__, __LINE__); } } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set mass=" + std::to_string(mass), __FILE__, __LINE__); } @@ -586,13 +587,6 @@ Collider* RigidBody::addCollider(CollisionShape* collisionShape, const Transform // Set the profiler collider->setProfiler(mProfiler); -#endif - -#ifdef IS_LOGGING_ACTIVE - - // Set the logger - collider->setLogger(mLogger); - #endif // Compute the world-space AABB of the new collision shape @@ -602,10 +596,10 @@ Collider* RigidBody::addCollider(CollisionShape* collisionShape, const Transform // Notify the collision detection about this new collision shape mWorld.mCollisionDetection.addCollider(collider, aabb); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Collider " + std::to_string(collider->getBroadPhaseId()) + " added to body", __FILE__, __LINE__); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, "Collider " + std::to_string(collider->getBroadPhaseId()) + ": collisionShape=" + collider->getCollisionShape()->to_string(), __FILE__, __LINE__); @@ -631,7 +625,7 @@ void RigidBody::removeCollider(Collider* collider) { void RigidBody::enableGravity(bool isEnabled) { mWorld.mRigidBodyComponents.setIsGravityEnabled(mEntity, isEnabled); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set isGravityEnabled=" + (isEnabled ? "true" : "false"), __FILE__, __LINE__); } @@ -648,12 +642,12 @@ void RigidBody::setLinearDamping(decimal linearDamping) { mWorld.mRigidBodyComponents.setLinearDamping(mEntity, linearDamping); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set linearDamping=" + std::to_string(linearDamping), __FILE__, __LINE__); } else { - RP3D_LOG(mLogger, Logger::Level::Error, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Error, Logger::Category::Body, "Error when setting the linear damping of body " + std::to_string(mEntity.id) + ": linear damping cannot be negative", __FILE__, __LINE__); } } @@ -670,11 +664,11 @@ void RigidBody::setAngularDamping(decimal angularDamping) { mWorld.mRigidBodyComponents.setAngularDamping(mEntity, angularDamping); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set angularDamping=" + std::to_string(angularDamping), __FILE__, __LINE__); } else { - RP3D_LOG(mLogger, Logger::Level::Error, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Error, Logger::Category::Body, "Error when setting the angular damping of body " + std::to_string(mEntity.id) + ": angular damping cannot be negative", __FILE__, __LINE__); } } @@ -696,7 +690,7 @@ void RigidBody::setLinearVelocity(const Vector3& linearVelocity) { setIsSleeping(false); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set linearVelocity=" + linearVelocity.to_string(), __FILE__, __LINE__); } @@ -717,7 +711,7 @@ void RigidBody::setAngularVelocity(const Vector3& angularVelocity) { setIsSleeping(false); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set angularVelocity=" + angularVelocity.to_string(), __FILE__, __LINE__); } @@ -832,7 +826,7 @@ void RigidBody::setIsSleeping(bool isSleeping) { mWorld.mRigidBodyComponents.setExternalTorque(mEntity, Vector3::zero()); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set isSleeping=" + (isSleeping ? "true" : "false"), __FILE__, __LINE__); } @@ -876,7 +870,7 @@ void RigidBody::setIsAllowedToSleep(bool isAllowedToSleep) { if (!isAllowedToSleep) setIsSleeping(false); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(mEntity.id) + ": Set isAllowedToSleep=" + (isAllowedToSleep ? "true" : "false"), __FILE__, __LINE__); } diff --git a/src/collision/Collider.cpp b/src/collision/Collider.cpp index a1832af8..68ed01de 100644 --- a/src/collision/Collider.cpp +++ b/src/collision/Collider.cpp @@ -29,6 +29,7 @@ #include #include #include +#include using namespace reactphysics3d; @@ -78,7 +79,7 @@ void Collider::setCollisionCategoryBits(unsigned short collisionCategoryBits) { int broadPhaseId = mBody->mWorld.mCollidersComponents.getBroadPhaseId(mEntity); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, + RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, "Collider " + std::to_string(broadPhaseId) + ": Set collisionCategoryBits=" + std::to_string(collisionCategoryBits), __FILE__, __LINE__); } @@ -97,7 +98,7 @@ void Collider::setCollideWithMaskBits(unsigned short collideWithMaskBits) { int broadPhaseId = mBody->mWorld.mCollidersComponents.getBroadPhaseId(mEntity); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, + RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, "Collider" + std::to_string(broadPhaseId) + ": Set collideWithMaskBits=" + std::to_string(collideWithMaskBits), __FILE__, __LINE__); } @@ -118,7 +119,7 @@ void Collider::setLocalToBodyTransform(const Transform& transform) { mBody->mWorld.mCollisionDetection.updateCollider(mEntity, 0); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Collider, + RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, "Collider " + std::to_string(getBroadPhaseId()) + ": Set localToBodyTransform=" + transform.to_string(), __FILE__, __LINE__); } @@ -215,6 +216,18 @@ void Collider::setHasCollisionShapeChangedSize(bool hasCollisionShapeChangedSize mBody->mWorld.mCollidersComponents.setHasCollisionShapeChangedSize(mEntity, hasCollisionShapeChangedSize); } +// Set a new material for this rigid body +/** + * @param material The material you want to set to the body + */ +void Collider::setMaterial(const Material& material) { + + mMaterial = material; + + RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, + "Collider " + std::to_string(mEntity.id) + ": Set Material" + mMaterial.to_string(), __FILE__, __LINE__); +} + // Return the local to world transform /** * @return The transformation that transforms the local-space of the collision diff --git a/src/engine/PhysicsCommon.cpp b/src/engine/PhysicsCommon.cpp index 0b029551..6f08486c 100644 --- a/src/engine/PhysicsCommon.cpp +++ b/src/engine/PhysicsCommon.cpp @@ -28,6 +28,9 @@ using namespace reactphysics3d; +// Static variables +Logger* PhysicsCommon::mLogger = nullptr; + /// Constructor PhysicsCommon::PhysicsCommon(MemoryAllocator* baseMemoryAllocator) : mMemoryManager(baseMemoryAllocator), @@ -36,10 +39,7 @@ PhysicsCommon::PhysicsCommon(MemoryAllocator* baseMemoryAllocator) mConvexMeshShapes(mMemoryManager.getHeapAllocator()), mConcaveMeshShapes(mMemoryManager.getHeapAllocator()), mHeightFieldShapes(mMemoryManager.getHeapAllocator()), mPolyhedronMeshes(mMemoryManager.getHeapAllocator()), mTriangleMeshes(mMemoryManager.getHeapAllocator()), -#ifdef IS_LOGGING_ACTIVE - mLoggers(mMemoryManager.getHeapAllocator()), -#endif - mProfilers(mMemoryManager.getHeapAllocator()) { + mProfilers(mMemoryManager.getHeapAllocator()), mDefaultLoggers(mMemoryManager.getHeapAllocator()) { } @@ -98,16 +98,11 @@ void PhysicsCommon::release() { destroyTriangleMesh(*it); } -// If logging is enabled -#ifdef IS_LOGGING_ACTIVE - - // Destroy the loggers - for (auto it = mLoggers.begin(); it != mLoggers.end(); ++it) { + // Destroy the default loggers + for (auto it = mDefaultLoggers.begin(); it != mDefaultLoggers.end(); ++it) { destroyDefaultLogger(*it); } -#endif - // If profiling is enabled #ifdef IS_PROFILING_ACTIVE @@ -121,39 +116,20 @@ void PhysicsCommon::release() { } // Create and return an instance of PhysicsWorld -PhysicsWorld* PhysicsCommon::createPhysicsWorld(const PhysicsWorld::WorldSettings& worldSettings, Logger* logger, Profiler* profiler) { +PhysicsWorld* PhysicsCommon::createPhysicsWorld(const PhysicsWorld::WorldSettings& worldSettings) { + + Profiler* profiler = nullptr; #ifdef IS_PROFILING_ACTIVE - // If the user has not provided its own profiler, we create one - if (profiler == nullptr) { + profiler = createProfiler(); - profiler = createProfiler(); - - // Add a destination file for the profiling data - profiler->addFileDestination("rp3d_profiling_" + worldSettings.worldName + ".txt", Profiler::Format::Text); - } + // Add a destination file for the profiling data + profiler->addFileDestination("rp3d_profiling_" + worldSettings.worldName + ".txt", Profiler::Format::Text); #endif -#ifdef IS_LOGGING_ACTIVE - - // If the user has not provided its own logger, we create one - if (logger == nullptr) { - - DefaultLogger* defaultLogger = createDefaultLogger(); - - // Add a log destination file - uint logLevel = static_cast(Logger::Level::Information) | static_cast(Logger::Level::Warning) | - static_cast(Logger::Level::Error); - defaultLogger->addFileDestination("rp3d_log_" + worldSettings.worldName + ".html", logLevel, DefaultLogger::Format::HTML); - - logger = defaultLogger; - } - -#endif - - PhysicsWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Heap, sizeof(PhysicsWorld))) PhysicsWorld(mMemoryManager, worldSettings, logger, profiler); + PhysicsWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Heap, sizeof(PhysicsWorld))) PhysicsWorld(mMemoryManager, worldSettings, profiler); mPhysicsWorlds.add(world); @@ -374,15 +350,12 @@ void PhysicsCommon::destroyTriangleMesh(TriangleMesh* triangleMesh) { mTriangleMeshes.remove(triangleMesh); } -// If logging is enabled -#ifdef IS_LOGGING_ACTIVE - // Create and return a new logger DefaultLogger* PhysicsCommon::createDefaultLogger() { DefaultLogger* logger = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(DefaultLogger))) DefaultLogger(mMemoryManager.getHeapAllocator()); - mLoggers.add(logger); + mDefaultLoggers.add(logger); return logger; } @@ -396,11 +369,9 @@ void PhysicsCommon::destroyDefaultLogger(DefaultLogger* logger) { // Release allocated memory mMemoryManager.release(MemoryManager::AllocationType::Pool, logger, sizeof(DefaultLogger)); - mLoggers.remove(logger); + mDefaultLoggers.remove(logger); } -#endif - // If profiling is enabled #ifdef IS_PROFILING_ACTIVE diff --git a/src/engine/PhysicsWorld.cpp b/src/engine/PhysicsWorld.cpp index ef94ea4f..8480f640 100644 --- a/src/engine/PhysicsWorld.cpp +++ b/src/engine/PhysicsWorld.cpp @@ -24,6 +24,7 @@ ********************************************************************************/ // Libraries +#include #include #include #include @@ -47,10 +48,9 @@ uint PhysicsWorld::mNbWorlds = 0; /** * @param gravity Gravity vector in the world (in meters per second squared) * @param worldSettings The settings of the world - * @param logger Pointer to the logger * @param profiler Pointer to the profiler */ -PhysicsWorld::PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings, Logger* logger, Profiler* profiler) +PhysicsWorld::PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings, Profiler* profiler) : mMemoryManager(memoryManager), mConfig(worldSettings), mEntityManager(mMemoryManager.getHeapAllocator()), mDebugRenderer(mMemoryManager.getHeapAllocator()), mCollisionBodyComponents(mMemoryManager.getHeapAllocator()), mRigidBodyComponents(mMemoryManager.getHeapAllocator()), mTransformComponents(mMemoryManager.getHeapAllocator()), mCollidersComponents(mMemoryManager.getHeapAllocator()), @@ -96,23 +96,16 @@ PhysicsWorld::PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& wo mDynamicsSystem.setProfiler(mProfiler); mCollisionDetection.setProfiler(mProfiler); -#endif - -#ifdef IS_LOGGING_ACTIVE - - assert(logger != nullptr); - mLogger = logger; - #endif mNbWorlds++; - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: Physics world " + mName + " has been created", __FILE__, __LINE__); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: Initial world settings: " + worldSettings.to_string(), __FILE__, __LINE__); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: Physics world " + mName + " has been created", __FILE__, __LINE__); } @@ -120,7 +113,7 @@ PhysicsWorld::PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& wo // Destructor PhysicsWorld::~PhysicsWorld() { - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: Physics world " + mName + " has been destroyed", __FILE__, __LINE__); // Destroy all the collision bodies that have not been removed @@ -152,7 +145,7 @@ PhysicsWorld::~PhysicsWorld() { assert(mJointsComponents.getNbComponents() == 0); assert(mRigidBodies.size() == 0); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: Physics world " + mName + " has been destroyed", __FILE__, __LINE__); } @@ -188,11 +181,7 @@ CollisionBody* PhysicsWorld::createCollisionBody(const Transform& transform) { #endif -#ifdef IS_LOGGING_ACTIVE - collisionBody->setLogger(mLogger); -#endif - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(entity.id) + ": New collision body created", __FILE__, __LINE__); // Return the pointer to the rigid body @@ -205,7 +194,7 @@ CollisionBody* PhysicsWorld::createCollisionBody(const Transform& transform) { */ void PhysicsWorld::destroyCollisionBody(CollisionBody* collisionBody) { - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(collisionBody->getEntity().id) + ": collision body destroyed", __FILE__, __LINE__); // Remove all the collision shapes of the body @@ -477,11 +466,7 @@ RigidBody* PhysicsWorld::createRigidBody(const Transform& transform) { rigidBody->setProfiler(mProfiler); #endif -#ifdef IS_LOGGING_ACTIVE - rigidBody->setLogger(mLogger); -#endif - - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(entity.id) + ": New collision body created", __FILE__, __LINE__); // Return the pointer to the rigid body @@ -494,7 +479,7 @@ RigidBody* PhysicsWorld::createRigidBody(const Transform& transform) { */ void PhysicsWorld::destroyRigidBody(RigidBody* rigidBody) { - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(rigidBody->getEntity().id) + ": rigid body destroyed", __FILE__, __LINE__); // Remove all the collision shapes of the body @@ -634,9 +619,9 @@ Joint* PhysicsWorld::createJoint(const JointInfo& jointInfo) { mCollisionDetection.addNoCollisionPair(jointInfo.body1->getEntity(), jointInfo.body2->getEntity()); } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Joint, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Joint, "Joint " + std::to_string(newJoint->getEntity().id) + ": New joint created", __FILE__, __LINE__); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Joint, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Joint, "Joint " + std::to_string(newJoint->getEntity().id) + ": " + newJoint->to_string(), __FILE__, __LINE__); // Add the joint into the joint list of the bodies involved in the joint @@ -654,7 +639,7 @@ void PhysicsWorld::destroyJoint(Joint* joint) { assert(joint != nullptr); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Joint, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Joint, "Joint " + std::to_string(joint->getEntity().id) + ": joint destroyed", __FILE__, __LINE__); // If the collision between the two bodies of the constraint was disabled @@ -702,17 +687,30 @@ void PhysicsWorld::destroyJoint(Joint* joint) { mMemoryManager.release(MemoryManager::AllocationType::Pool, joint, nbBytes); } + +// Set the number of iterations for the velocity constraint solver +/** + * @param nbIterations Number of iterations for the velocity solver + */ +void PhysicsWorld::setNbIterationsVelocitySolver(uint nbIterations) { + + mNbVelocitySolverIterations = nbIterations; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: Set nb iterations velocity solver to " + std::to_string(nbIterations), __FILE__, __LINE__); +} + // Add the joint to the list of joints of the two bodies involved in the joint void PhysicsWorld::addJointToBodies(Entity body1, Entity body2, Entity joint) { mRigidBodyComponents.addJointToBody(body1, joint); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(body1.id) + ": Joint " + std::to_string(joint.id) + " added to body", __FILE__, __LINE__); mRigidBodyComponents.addJointToBody(body2, joint); - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::Body, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::Body, "Body " + std::to_string(body2.id) + ": Joint " + std::to_string(joint.id) + " added to body", __FILE__, __LINE__); } @@ -939,6 +937,84 @@ void PhysicsWorld::enableSleeping(bool isSleepingEnabled) { } } - RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::World, + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, "Physics World: isSleepingEnabled=" + (isSleepingEnabled ? std::string("true") : std::string("false")) , __FILE__, __LINE__); } + +// Set the number of iterations for the position constraint solver +/** + * @param nbIterations Number of iterations for the position solver + */ +void PhysicsWorld::setNbIterationsPositionSolver(uint nbIterations) { + + mNbPositionSolverIterations = nbIterations; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: Set nb iterations position solver to " + std::to_string(nbIterations), __FILE__, __LINE__); +} + +// Set the gravity vector of the world +/** + * @param gravity The gravity vector (in meter per seconds squared) + */ +void PhysicsWorld::setGravity(Vector3& gravity) { + + mConfig.gravity = gravity; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: Set gravity vector to " + gravity.to_string(), __FILE__, __LINE__); +} + +// Set the sleep linear velocity. +/// When the velocity of a body becomes smaller than the sleep linear/angular +/// velocity for a given amount of time, the body starts sleeping and does not need +/// to be simulated anymore. +/** + * @param sleepLinearVelocity The sleep linear velocity (in meters per second) + */ +void PhysicsWorld::setSleepLinearVelocity(decimal sleepLinearVelocity) { + assert(sleepLinearVelocity >= decimal(0.0)); + mSleepLinearVelocity = sleepLinearVelocity; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: sleepLinearVelocity= " + std::to_string(sleepLinearVelocity), __FILE__, __LINE__); +} + +// Set the sleep angular velocity. +/// When the velocity of a body becomes smaller than the sleep linear/angular +/// velocity for a given amount of time, the body starts sleeping and does not need +/// to be simulated anymore. +/** + * @param sleepAngularVelocity The sleep angular velocity (in radian per second) + */ +void PhysicsWorld::setSleepAngularVelocity(decimal sleepAngularVelocity) { + assert(sleepAngularVelocity >= decimal(0.0)); + mSleepAngularVelocity = sleepAngularVelocity; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: sleepAngularVelocity= " + std::to_string(sleepAngularVelocity), __FILE__, __LINE__); +} + +// Set the time a body is required to stay still before sleeping +/** + * @param timeBeforeSleep Time a body is required to stay still before sleeping (in seconds) + */ +void PhysicsWorld::setTimeBeforeSleep(decimal timeBeforeSleep) { + assert(timeBeforeSleep >= decimal(0.0)); + mTimeBeforeSleep = timeBeforeSleep; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: timeBeforeSleep= " + std::to_string(timeBeforeSleep), __FILE__, __LINE__); +} + +// Enable/Disable the gravity +/** + * @param isGravityEnabled True if you want to enable the gravity in the world + * and false otherwise + */ +void PhysicsWorld::setIsGravityEnabled(bool isGravityEnabled) { + mIsGravityEnabled = isGravityEnabled; + + RP3D_LOG(mConfig.worldName, Logger::Level::Information, Logger::Category::World, + "Physics World: isGravityEnabled= " + (isGravityEnabled ? std::string("true") : std::string("false")), __FILE__, __LINE__); +} diff --git a/src/utils/DefaultLogger.cpp b/src/utils/DefaultLogger.cpp index 8957cf61..92806b6c 100644 --- a/src/utils/DefaultLogger.cpp +++ b/src/utils/DefaultLogger.cpp @@ -23,8 +23,6 @@ * * ********************************************************************************/ -#ifdef IS_LOGGING_ACTIVE - // Libraries #include #include @@ -94,7 +92,7 @@ void DefaultLogger::removeAllDestinations() { } // Log something -void DefaultLogger::log(Level level, Category category, const std::string& message, const char* filename, int lineNumber) { +void DefaultLogger::log(Level level, const std::string& physicsWorldName, Category category, const std::string& message, const char* filename, int lineNumber) { // Get current time auto now = std::chrono::system_clock::now(); @@ -105,10 +103,8 @@ void DefaultLogger::log(Level level, Category category, const std::string& messa // For each destination for (auto it = mDestinations.begin(); it != mDestinations.end(); ++it) { - (*it)->write(time, message, level, category, filename, lineNumber); + (*it)->write(time, physicsWorldName, message, level, category, filename, lineNumber); } mMutex.unlock(); } - -#endif diff --git a/test/tests/collision/TestDynamicAABBTree.h b/test/tests/collision/TestDynamicAABBTree.h index dc121d2d..8150f165 100755 --- a/test/tests/collision/TestDynamicAABBTree.h +++ b/test/tests/collision/TestDynamicAABBTree.h @@ -109,8 +109,18 @@ class TestDynamicAABBTree : public Test { TestDynamicAABBTree(const std::string& name): Test(name) { #ifdef IS_PROFILING_ACTIVE - mProfiler = mPhysicsCommon.createProfiler(); + mProfiler = new Profiler(); #endif + + } + + /// Constructor + ~TestDynamicAABBTree() { + +#ifdef IS_PROFILING_ACTIVE + delete mProfiler; +#endif + } bool isOverlapping(int nodeId, const List& overlappingNodes) const { diff --git a/testbed/scenes/collisiondetection/CollisionDetectionScene.cpp b/testbed/scenes/collisiondetection/CollisionDetectionScene.cpp index 6b9e4e18..06b809f1 100644 --- a/testbed/scenes/collisiondetection/CollisionDetectionScene.cpp +++ b/testbed/scenes/collisiondetection/CollisionDetectionScene.cpp @@ -52,6 +52,13 @@ CollisionDetectionScene::CollisionDetectionScene(const std::string& name, Engine rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation mPhysicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); diff --git a/testbed/scenes/collisionshapes/CollisionShapesScene.cpp b/testbed/scenes/collisionshapes/CollisionShapesScene.cpp index d4aac4b3..0cc97c8e 100644 --- a/testbed/scenes/collisionshapes/CollisionShapesScene.cpp +++ b/testbed/scenes/collisionshapes/CollisionShapesScene.cpp @@ -48,6 +48,13 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name, EngineSettin rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); physicsWorld->setEventListener(this); diff --git a/testbed/scenes/concavemesh/ConcaveMeshScene.cpp b/testbed/scenes/concavemesh/ConcaveMeshScene.cpp index 7139de6f..e08ef540 100644 --- a/testbed/scenes/concavemesh/ConcaveMeshScene.cpp +++ b/testbed/scenes/concavemesh/ConcaveMeshScene.cpp @@ -48,6 +48,13 @@ ConcaveMeshScene::ConcaveMeshScene(const std::string& name, EngineSettings& sett rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); physicsWorld->setEventListener(this); diff --git a/testbed/scenes/cubes/CubesScene.cpp b/testbed/scenes/cubes/CubesScene.cpp index 228aff4f..7c45e15b 100644 --- a/testbed/scenes/cubes/CubesScene.cpp +++ b/testbed/scenes/cubes/CubesScene.cpp @@ -48,6 +48,13 @@ CubesScene::CubesScene(const std::string& name, EngineSettings& settings) rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); physicsWorld->setEventListener(this); diff --git a/testbed/scenes/cubestack/CubeStackScene.cpp b/testbed/scenes/cubestack/CubeStackScene.cpp index c03fa1c7..b45f4977 100644 --- a/testbed/scenes/cubestack/CubeStackScene.cpp +++ b/testbed/scenes/cubestack/CubeStackScene.cpp @@ -46,6 +46,13 @@ CubeStackScene::CubeStackScene(const std::string& name, EngineSettings& settings rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); physicsWorld->setEventListener(this); diff --git a/testbed/scenes/heightfield/HeightFieldScene.cpp b/testbed/scenes/heightfield/HeightFieldScene.cpp index 6c763a4a..53ac20c7 100644 --- a/testbed/scenes/heightfield/HeightFieldScene.cpp +++ b/testbed/scenes/heightfield/HeightFieldScene.cpp @@ -47,6 +47,13 @@ HeightFieldScene::HeightFieldScene(const std::string& name, EngineSettings& sett rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); diff --git a/testbed/scenes/joints/JointsScene.cpp b/testbed/scenes/joints/JointsScene.cpp index e6bbd067..00b1e1ca 100644 --- a/testbed/scenes/joints/JointsScene.cpp +++ b/testbed/scenes/joints/JointsScene.cpp @@ -47,6 +47,13 @@ JointsScene::JointsScene(const std::string& name, EngineSettings& settings) rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); physicsWorld->setEventListener(this); diff --git a/testbed/scenes/pile/PileScene.cpp b/testbed/scenes/pile/PileScene.cpp index da5bfd26..d5d6a938 100644 --- a/testbed/scenes/pile/PileScene.cpp +++ b/testbed/scenes/pile/PileScene.cpp @@ -48,6 +48,13 @@ PileScene::PileScene(const std::string& name, EngineSettings& settings) rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation mPhysicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); diff --git a/testbed/scenes/raycast/RaycastScene.cpp b/testbed/scenes/raycast/RaycastScene.cpp index 65024669..338d4ce3 100644 --- a/testbed/scenes/raycast/RaycastScene.cpp +++ b/testbed/scenes/raycast/RaycastScene.cpp @@ -45,6 +45,13 @@ RaycastScene::RaycastScene(const std::string& name, EngineSettings& settings) rp3d::PhysicsWorld::WorldSettings worldSettings; worldSettings.worldName = name; + // Logger + rp3d::DefaultLogger* defaultLogger = mPhysicsCommon.createDefaultLogger(); + uint logLevel = static_cast(rp3d::Logger::Level::Information) | static_cast(rp3d::Logger::Level::Warning) | + static_cast(rp3d::Logger::Level::Error); + defaultLogger->addFileDestination("rp3d_log_" + name + ".html", logLevel, rp3d::DefaultLogger::Format::HTML); + mPhysicsCommon.setLogger(defaultLogger); + // Create the physics world for the physics simulation mPhysicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings); diff --git a/testbed/src/SceneDemo.cpp b/testbed/src/SceneDemo.cpp index 5e825a16..1464322c 100644 --- a/testbed/src/SceneDemo.cpp +++ b/testbed/src/SceneDemo.cpp @@ -618,7 +618,7 @@ void SceneDemo::updateEngineSettings() { if (mIsPhysicsWorldSimulated) { // Update the physics engine parameters - mPhysicsWorld->setIsGratityEnabled(mEngineSettings.isGravityEnabled); + mPhysicsWorld->setIsGravityEnabled(mEngineSettings.isGravityEnabled); rp3d::Vector3 gravity(mEngineSettings.gravity.x, mEngineSettings.gravity.y, mEngineSettings.gravity.z); mPhysicsWorld->setGravity(gravity);