From b12453e9ca1612ad41c46ab675d87cdb84f082d4 Mon Sep 17 00:00:00 2001 From: hiker Date: Tue, 13 Dec 2016 08:31:08 +1100 Subject: [PATCH] Made Physics a singleton, removing the accessor functions from World. Removes dependency on World for a few files. --- src/graphics/fixed_pipeline_renderer.cpp | 2 +- src/graphics/irr_driver.cpp | 4 ++-- src/graphics/post_processing.cpp | 2 +- src/graphics/shader_based_renderer.cpp | 13 +++++++------ src/items/flyable.cpp | 4 ++-- src/items/plunger.cpp | 5 ++--- src/items/rubber_band.cpp | 4 +--- src/karts/abstract_kart_animation.cpp | 5 ++--- src/karts/controller/spare_tire_ai.cpp | 2 +- src/karts/kart.cpp | 10 +++++----- src/karts/rescue_animation.cpp | 2 +- src/modes/world.cpp | 16 ++++++++-------- src/modes/world.hpp | 5 ----- src/network/rewind_info.cpp | 5 ++--- src/network/rewind_manager.cpp | 2 +- src/physics/physical_object.cpp | 21 ++++++++++----------- src/physics/physics.hpp | 14 +++++++++++--- src/physics/stk_dynamics_world.hpp | 4 ++++ src/physics/triangle_mesh.cpp | 10 +++++----- src/tracks/track.cpp | 2 +- src/utils/debug.cpp | 6 +++--- 21 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/graphics/fixed_pipeline_renderer.cpp b/src/graphics/fixed_pipeline_renderer.cpp index 99d68787c..8aab91030 100644 --- a/src/graphics/fixed_pipeline_renderer.cpp +++ b/src/graphics/fixed_pipeline_renderer.cpp @@ -71,7 +71,7 @@ void FixedPipelineRenderer::render(float dt) // is not set up properly. This is only used for // the bullet debug view. if (UserConfigParams::m_artist_debug_mode) - World::getWorld()->getPhysics()->draw(); + Physics::getInstance()->draw(); } // for igetNumKarts() // Set the viewport back to the full screen for race gui diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 63c0aa900..2396f28d0 100644 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -2078,9 +2078,9 @@ void IrrDriver::update(float dt) GUIEngine::render(dt); } - if (world->getPhysics() != NULL) + if (Physics::getInstance()) { - IrrDebugDrawer* debug_drawer = world->getPhysics()->getDebugDrawer(); + IrrDebugDrawer* debug_drawer = Physics::getInstance()->getDebugDrawer(); if (debug_drawer != NULL && debug_drawer->debugEnabled()) { debug_drawer->beginNextFrame(); diff --git a/src/graphics/post_processing.cpp b/src/graphics/post_processing.cpp index 784751940..0623a0619 100644 --- a/src/graphics/post_processing.cpp +++ b/src/graphics/post_processing.cpp @@ -1436,7 +1436,7 @@ FrameBuffer *PostProcessing::render(scene::ICameraSceneNode * const camnode, glDisable(GL_BLEND); World *world = World::getWorld(); - Physics *physics = world ? world->getPhysics() : NULL; + Physics *physics = Physics::getInstance(); if (isRace && UserConfigParams::m_dof && (physics == NULL || !physics->isDebug())) { diff --git a/src/graphics/shader_based_renderer.cpp b/src/graphics/shader_based_renderer.cpp index a383a7706..f174961ce 100644 --- a/src/graphics/shader_based_renderer.cpp +++ b/src/graphics/shader_based_renderer.cpp @@ -547,15 +547,16 @@ void ShaderBasedRenderer::debugPhysics() // the bullet debug view, since otherwise the camera // is not set up properly. This is only used for // the bullet debug view. - World *world = World::getWorld(); - if (UserConfigParams::m_artist_debug_mode) - world->getPhysics()->draw(); - if (world != NULL && world->getPhysics() != NULL) + if(Physics::getInstance()) { - IrrDebugDrawer* debug_drawer = world->getPhysics()->getDebugDrawer(); + if (UserConfigParams::m_artist_debug_mode) + Physics::getInstance()->draw(); + + IrrDebugDrawer* debug_drawer = Physics::getInstance()->getDebugDrawer(); if (debug_drawer != NULL && debug_drawer->debugEnabled()) { - const std::map >& lines = debug_drawer->getLines(); + const std::map >& lines = + debug_drawer->getLines(); std::map >::const_iterator it; Shaders::ColoredLine *line = Shaders::ColoredLine::getInstance(); diff --git a/src/items/flyable.cpp b/src/items/flyable.cpp index d0195ff17..e0b122844 100644 --- a/src/items/flyable.cpp +++ b/src/items/flyable.cpp @@ -133,7 +133,7 @@ void Flyable::createPhysics(float forw_offset, const Vec3 &velocity, m_shape = shape; createBody(m_mass, trans, m_shape, restitution); m_user_pointer.set(this); - World::getWorld()->getPhysics()->addBody(getBody()); + Physics::getInstance()->addBody(getBody()); m_body->setGravity(gravity); @@ -199,7 +199,7 @@ void Flyable::init(const XMLNode &node, scene::IMesh *model, Flyable::~Flyable() { if(m_shape) delete m_shape; - World::getWorld()->getPhysics()->removeBody(getBody()); + Physics::getInstance()->removeBody(getBody()); } // ~Flyable //----------------------------------------------------------------------------- diff --git a/src/items/plunger.cpp b/src/items/plunger.cpp index 309be7b18..be863f551 100644 --- a/src/items/plunger.cpp +++ b/src/items/plunger.cpp @@ -28,7 +28,6 @@ #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" #include "karts/kart_properties.hpp" -#include "modes/world.hpp" #include "physics/physical_object.hpp" #include "physics/physics.hpp" #include "tracks/track.hpp" @@ -174,7 +173,7 @@ bool Plunger::hit(AbstractKart *kart, PhysicalObject *obj) m_keep_alive = 0; // Make this object invisible. getNode()->setVisible(false); - World::getWorld()->getPhysics()->removeBody(getBody()); + Physics::getInstance()->removeBody(getBody()); } else { @@ -188,7 +187,7 @@ bool Plunger::hit(AbstractKart *kart, PhysicalObject *obj) { node->setVisible(false); } - World::getWorld()->getPhysics()->removeBody(getBody()); + Physics::getInstance()->removeBody(getBody()); if(kart) { diff --git a/src/items/rubber_band.cpp b/src/items/rubber_band.cpp index e9245584b..6fad556cf 100644 --- a/src/items/rubber_band.cpp +++ b/src/items/rubber_band.cpp @@ -29,7 +29,6 @@ #include "karts/abstract_kart.hpp" #include "karts/kart_properties.hpp" #include "karts/max_speed.hpp" -#include "modes/world.hpp" #include "physics/physics.hpp" #include "race/race_manager.hpp" #include "utils/string_utils.hpp" @@ -215,8 +214,7 @@ void RubberBand::checkForHit(const Vec3 &k, const Vec3 &p) m_owner->getBody()->getBroadphaseHandle()->m_collisionFilterGroup = 0; // Do the raycast - World::getWorld()->getPhysics()->getPhysicsWorld()->rayTest(k, p, - ray_callback); + Physics::getInstance()->getPhysicsWorld()->rayTest(k, p, ray_callback); // Reset collision groups m_plunger->getBody()->getBroadphaseHandle()->m_collisionFilterGroup = old_plunger_group; if(m_owner->getBody()->getBroadphaseHandle()) diff --git a/src/karts/abstract_kart_animation.cpp b/src/karts/abstract_kart_animation.cpp index 5a10b9374..54cc9b09c 100644 --- a/src/karts/abstract_kart_animation.cpp +++ b/src/karts/abstract_kart_animation.cpp @@ -22,7 +22,6 @@ #include "karts/abstract_kart.hpp" #include "karts/kart_model.hpp" #include "karts/skidding.hpp" -#include "modes/world.hpp" #include "physics/physics.hpp" AbstractKartAnimation::AbstractKartAnimation(AbstractKart *kart, @@ -48,7 +47,7 @@ AbstractKartAnimation::AbstractKartAnimation(AbstractKart *kart, // Register this animation with the kart (which will free it // later). kart->setKartAnimation(this); - World::getWorld()->getPhysics()->removeKart(m_kart); + Physics::getInstance()->removeKart(m_kart); kart->getSkidding()->reset(); kart->getSlipstream()->reset(); if(kart->isSquashed()) @@ -74,7 +73,7 @@ AbstractKartAnimation::~AbstractKartAnimation() if(m_timer < 0) { m_kart->getBody()->setAngularVelocity(btVector3(0,0,0)); - World::getWorld()->getPhysics()->addKart(m_kart); + Physics::getInstance()->addKart(m_kart); } } // ~AbstractKartAnimation diff --git a/src/karts/controller/spare_tire_ai.cpp b/src/karts/controller/spare_tire_ai.cpp index b68faa386..2f4eb2f9d 100644 --- a/src/karts/controller/spare_tire_ai.cpp +++ b/src/karts/controller/spare_tire_ai.cpp @@ -113,7 +113,7 @@ void SpareTireAI::spawn(float time_to_last) findDefaultPath(); m_timer = time_to_last; - World::getWorld()->getPhysics()->addKart(m_kart); + Physics::getInstance()->addKart(m_kart); m_kart->startEngineSFX(); m_kart->getKartGFX()->reset(); m_kart->getNode()->setVisible(true); diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index 371ebd66c..b4d250c22 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -273,7 +273,7 @@ Kart::~Kart() // Ghost karts don't have a body if(m_body) { - World::getWorld()->getPhysics()->removeKart(this); + Physics::getInstance()->removeKart(this); delete m_vehicle; delete m_vehicle_raycaster; } @@ -310,8 +310,8 @@ void Kart::reset() // don't have one). if(m_body) { - World::getWorld()->getPhysics()->removeKart(this); - World::getWorld()->getPhysics()->addKart(this); + Physics::getInstance()->removeKart(this); + Physics::getInstance()->addKart(this); } m_min_nitro_time = 0.0f; @@ -671,7 +671,7 @@ void Kart::createPhysics() // Create the actual vehicle // ------------------------- m_vehicle_raycaster = - new btKartRaycaster(World::getWorld()->getPhysics()->getPhysicsWorld(), + new btKartRaycaster(Physics::getInstance()->getPhysicsWorld(), stk_config->m_smooth_normals && Track::getCurrentTrack()->smoothNormals()); m_vehicle = new btKart(m_body, m_vehicle_raycaster, this); @@ -1158,7 +1158,7 @@ void Kart::eliminate() { if (!getKartAnimation()) { - World::getWorld()->getPhysics()->removeKart(this); + Physics::getInstance()->removeKart(this); } if (m_stars_effect) { diff --git a/src/karts/rescue_animation.cpp b/src/karts/rescue_animation.cpp index dbec7ff01..06c7d2002 100644 --- a/src/karts/rescue_animation.cpp +++ b/src/karts/rescue_animation.cpp @@ -89,7 +89,7 @@ RescueAnimation::~RescueAnimation() { m_kart->getBody()->setLinearVelocity(btVector3(0,0,0)); m_kart->getBody()->setAngularVelocity(btVector3(0,0,0)); - World::getWorld()->getPhysics()->addKart(m_kart); + Physics::getInstance()->addKart(m_kart); for(unsigned int i=0; iloadScript(script_path, true); // Create the physics - m_physics = new Physics(); + Physics::getInstance(); unsigned int num_karts = race_manager->getNumberOfKarts(); //assert(num_karts > 0); @@ -489,9 +488,10 @@ World::~World() Camera::removeAllCameras(); projectile_manager->cleanup(); - // In case that the track is not found, m_physics is still undefined. - if(m_physics) - delete m_physics; + + // In case that the track is not found, Physics was not instantiated, + // but kill handles this correctly. + Physics::kill(); delete m_script_engine; @@ -654,7 +654,7 @@ void World::resetAllKarts() { // Reset the physics 'remaining' time to 0 so that the number // of timesteps is reproducible if doing a physics-based history run - getPhysics()->getPhysicsWorld()->resetLocalTime(); + Physics::getInstance()->getPhysicsWorld()->resetLocalTime(); // If track checking is requested, check all rescue positions if // they are high enough. @@ -727,7 +727,7 @@ void World::resetAllKarts() (*i)->getBody()->setGravity((*i)->getMaterial()->hasGravity() ? (*i)->getNormal() * -g : Vec3(0, -g, 0)); } - for(int i=0; i<60; i++) m_physics->update(1.f/60.f); + for(int i=0; i<60; i++) Physics::getInstance()->update(1.f/60.f); for ( KartList::iterator i=m_karts.begin(); i!=m_karts.end(); i++) { @@ -1002,7 +1002,7 @@ void World::update(float dt) if (!history->dontDoPhysics()) { - m_physics->update(dt); + Physics::getInstance()->update(dt); } PROFILER_PUSH_CPU_MARKER("World::update (weather)", 0x80, 0x7F, 0x00); diff --git a/src/modes/world.hpp b/src/modes/world.hpp index c3204c5f5..fbdca0382 100644 --- a/src/modes/world.hpp +++ b/src/modes/world.hpp @@ -41,7 +41,6 @@ class AbstractKart; class btRigidBody; class Controller; class PhysicalObject; -class Physics; namespace Scripting { @@ -95,7 +94,6 @@ protected: KartList m_karts; RandomGenerator m_random; - Physics* m_physics; AbstractKart* m_fastest_kart; /** Number of eliminated karts. */ int m_eliminated_karts; @@ -311,9 +309,6 @@ public: unsigned int getCurrentNumPlayers() const { return m_num_players - m_eliminated_players;} // ------------------------------------------------------------------------ - /** Returns a pointer to the physics. */ - Physics *getPhysics() const { return m_physics; } - // ------------------------------------------------------------------------ /** Returns a pointer to the Scripting Engine. */ Scripting::ScriptEngine *getScriptEngine() const { return m_script_engine; } diff --git a/src/network/rewind_info.cpp b/src/network/rewind_info.cpp index 4340f2457..ec9989afb 100644 --- a/src/network/rewind_info.cpp +++ b/src/network/rewind_info.cpp @@ -18,7 +18,6 @@ #include "network/rewind_info.hpp" -#include "modes/world.hpp" #include "physics/physics.hpp" /** Constructor for a state: it only takes the size, and allocates a buffer @@ -42,8 +41,8 @@ RewindInfoState::RewindInfoState(float time, Rewinder *rewinder, BareNetworkString *buffer, bool is_confirmed) : RewindInfoRewinder(time, rewinder, buffer, is_confirmed) { - m_local_physics_time = World::getWorld()->getPhysics()->getPhysicsWorld() - ->getLocalTime(); + m_local_physics_time = Physics::getInstance()->getPhysicsWorld() + ->getLocalTime(); } // RewindInfoState // ============================================================================ diff --git a/src/network/rewind_manager.cpp b/src/network/rewind_manager.cpp index 5e8504770..7d33a764a 100644 --- a/src/network/rewind_manager.cpp +++ b/src/network/rewind_manager.cpp @@ -322,7 +322,7 @@ void RewindManager::rewindTo(float rewind_time) // Now start the rewind with the full state: world->setTime(exact_rewind_time); float local_physics_time = state->getLocalPhysicsTime(); - world->getPhysics()->getPhysicsWorld()->setLocalTime(local_physics_time); + Physics::getInstance()->getPhysicsWorld()->setLocalTime(local_physics_time); // Restore all states from the current time - the full state of a race // will be potentially stored in several state objects. State can be NULL diff --git a/src/physics/physical_object.cpp b/src/physics/physical_object.cpp index 7480563eb..275d063bd 100644 --- a/src/physics/physical_object.cpp +++ b/src/physics/physical_object.cpp @@ -18,18 +18,12 @@ #include "physics/physical_object.hpp" -#include -#include -#include - -using namespace irr; - +#include "config/stk_config.hpp" #include "graphics/material.hpp" #include "graphics/material_manager.hpp" #include "graphics/mesh_tools.hpp" #include "io/file_manager.hpp" #include "io/xml_node.hpp" -#include "modes/world.hpp" #include "physics/physics.hpp" #include "physics/triangle_mesh.hpp" #include "tracks/track.hpp" @@ -40,6 +34,11 @@ using namespace irr; #include #include #include +using namespace irr; + +#include +#include +#include /** Creates a physical Settings object with the given type, radius and mass. */ @@ -180,7 +179,7 @@ PhysicalObject::PhysicalObject(bool is_dynamic, // ---------------------------------------------------------------------------- PhysicalObject::~PhysicalObject() { - World::getWorld()->getPhysics()->removeBody(m_body); + Physics::getInstance()->removeBody(m_body); delete m_body; delete m_motion_state; @@ -540,7 +539,7 @@ void PhysicalObject::init(const PhysicalObject::Settings& settings) m_body->setActivationState(DISABLE_DEACTIVATION); } - World::getWorld()->getPhysics()->addBody(m_body); + Physics::getInstance()->addBody(m_body); m_body_added = true; if(m_triangle_mesh) m_triangle_mesh->setBody(m_body); @@ -672,7 +671,7 @@ void PhysicalObject::removeBody() { if (m_body_added) { - World::getWorld()->getPhysics()->removeBody(m_body); + Physics::getInstance()->removeBody(m_body); m_body_added = false; } } // Remove body @@ -684,7 +683,7 @@ void PhysicalObject::addBody() if (!m_body_added) { m_body_added = true; - World::getWorld()->getPhysics()->addBody(m_body); + Physics::getInstance()->addBody(m_body); } } // Add body diff --git a/src/physics/physics.hpp b/src/physics/physics.hpp index ac0cf77f7..a5d1209b7 100644 --- a/src/physics/physics.hpp +++ b/src/physics/physics.hpp @@ -32,6 +32,7 @@ #include "physics/irr_debug_drawer.hpp" #include "physics/stk_dynamics_world.hpp" #include "physics/user_pointer.hpp" +#include "utils/singleton.hpp" class AbstractKart; class STKDynamicsWorld; @@ -41,6 +42,7 @@ class Vec3; * \ingroup physics */ class Physics : public btSequentialImpulseConstraintSolver + , public AbstractSingleton { private: /** Bullet can report the same collision more than once (up to 4 @@ -53,7 +55,8 @@ private: * overhead (since it will likely use a tree to sort the entries). * Considering that the number of collisions is usually rather small * a simple list and linear search is faster is is being used here. */ - class CollisionPair { + class CollisionPair + { private: /** The user pointer of the objects involved in this collision. */ const UserPointer *m_up[2]; @@ -140,14 +143,19 @@ private: /** Used in physics debugging to draw the physics world. */ IrrDebugDrawer *m_debug_drawer; + btCollisionDispatcher *m_dispatcher; btBroadphaseInterface *m_axis_sweep; btDefaultCollisionConfiguration *m_collision_conf; CollisionList m_all_collisions; + /** Singleton. */ + static Physics *m_physics; + + Physics(); + virtual ~Physics(); + friend class AbstractSingleton; public: - Physics (); - ~Physics (); void init (const Vec3 &min_world, const Vec3 &max_world); void addKart (const AbstractKart *k); void addBody (btRigidBody* b) {m_dynamics_world->addRigidBody(b);} diff --git a/src/physics/stk_dynamics_world.hpp b/src/physics/stk_dynamics_world.hpp index 3e2c4f11a..ada89138a 100644 --- a/src/physics/stk_dynamics_world.hpp +++ b/src/physics/stk_dynamics_world.hpp @@ -21,6 +21,10 @@ #include "btBulletDynamicsCommon.h" +/** A thin wrapper around bullet's btDiscreteDynamicsWorld. Used to + * be able to query and set the 'left over' time from a previous + * time step, which is needed for more precise rewind/replays. + */ class STKDynamicsWorld : public btDiscreteDynamicsWorld { public: diff --git a/src/physics/triangle_mesh.cpp b/src/physics/triangle_mesh.cpp index 36b403d8f..7533c31fa 100644 --- a/src/physics/triangle_mesh.cpp +++ b/src/physics/triangle_mesh.cpp @@ -18,13 +18,13 @@ #include "physics/triangle_mesh.hpp" -#include "btBulletDynamicsCommon.h" - -#include "modes/world.hpp" +#include "config/stk_config.hpp" #include "physics/physics.hpp" #include "utils/constants.hpp" #include "utils/time.hpp" +#include "btBulletDynamicsCommon.h" + #include // ----------------------------------------------------------------------------- @@ -190,7 +190,7 @@ void TriangleMesh::createPhysicalBody(btCollisionObject::CollisionFlags flags, m_collision_shape); info.m_restitution = 0.8f; m_body=new btRigidBody(info); - World::getWorld()->getPhysics()->addBody(m_body); + Physics::getInstance()->addBody(m_body); m_body->setUserPointer(&m_user_pointer); m_body->setCollisionFlags(m_body->getCollisionFlags() | @@ -210,7 +210,7 @@ void TriangleMesh::removeAll() // Don't free the physical body if it was created outside this object. if(m_body && m_free_body) { - World::getWorld()->getPhysics()->removeBody(m_body); + Physics::getInstance()->removeBody(m_body); delete m_body; delete m_motion_state; m_body = NULL; diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index e606d6522..8b866fecd 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -1183,7 +1183,7 @@ bool Track::loadMainTrack(const XMLNode &root) // could be relaxed to fix this, it is not certain how the physics // will handle items that are out of the AABB m_aabb_max.setY(m_aabb_max.getY()+30.0f); - World::getWorld()->getPhysics()->init(m_aabb_min, m_aabb_max); + Physics::getInstance()->init(m_aabb_min, m_aabb_max); ModelDefinitionLoader lodLoader(this); diff --git a/src/utils/debug.cpp b/src/utils/debug.cpp index f9d82f23d..0d7e351d1 100644 --- a/src/utils/debug.cpp +++ b/src/utils/debug.cpp @@ -230,7 +230,7 @@ bool handleContextMenuAction(s32 cmd_id) { World *world = World::getWorld(); - Physics *physics = world ? world->getPhysics() : NULL; + Physics *physics = Physics::getInstance(); switch(cmd_id) { case DEBUG_GRAPHICS_RELOAD_SHADERS: @@ -325,8 +325,8 @@ bool handleContextMenuAction(s32 cmd_id) { irr_driver->resetDebugModes(); - if (!world) return false; - Physics *physics = world->getPhysics(); + Physics *physics = Physics::getInstance(); + if (!physics) return false; physics->setDebugMode(IrrDebugDrawer::DM_NO_KARTS_GRAPHICS); break; }