Kaynağa Gözat

Refactoring physics

Panagiotis Christopoulos Charitos 14 yıl önce
ebeveyn
işleme
78d55e29e1

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
build/debug/Makefile


+ 6 - 6
src/Main.cpp

@@ -22,12 +22,12 @@
 #include "LightRsrc.h"
 #include "Parser.h"
 #include "ParticleEmitter.h"
-#include "PhyCharacter.h"
+#include "PhysCharacter.h"
 #include "Renderer.h"
 #include "RendererInitializer.h"
 #include "MainRenderer.h"
-#include "PhyCharacter.h"
-#include "RigidBody.h"
+#include "PhysCharacter.h"
+#include "PhysRigidBody.h"
 #include "ScriptingEngine.h"
 #include "StdinListener.h"
 #include "ModelNode.h"
@@ -54,7 +54,7 @@ SkinNode* imp;
 PointLight* point_lights[10];
 SpotLight* spot_lights[2];
 ParticleEmitter* partEmitter;
-PhyCharacter* character;
+Phys::Character* character;
 
 Ui::Painter* painter;
 
@@ -82,12 +82,12 @@ void initPhysics()
 	groundTransform.setIdentity();
 	groundTransform.setOrigin(Vec3(0,-50, 0));
 
-	RigidBody::Initializer init;
+	Phys::RigidBody::Initializer init;
 	init.mass = 0.0;
 	init.shape = groundShape;
 	init.startTrf = groundTransform;
 
-	new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
+	new Phys::RigidBody(SceneSingleton::getInstance().getPhysMasterContainer(), init);
 
 
 	/*{

+ 31 - 11
src/Physics/PhyCharacter.cpp → src/Physics/PhysCharacter.cpp

@@ -3,22 +3,39 @@
 #include <btBulletDynamicsCommon.h>
 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
 #include <BulletDynamics/Character/btKinematicCharacterController.h>
-#include "PhyCharacter.h"
-#include "Physics.h"
-#include "MotionState.h"
-#include "RigidBody.h"
+#include "PhysCharacter.h"
+#include "PhysMasterContainer.h"
+#include "PhysMotionState.h"
+#include "PhysRigidBody.h"
+
+
+namespace Phys {
 
 
 //======================================================================================================================
 // Contructor                                                                                                          =
 //======================================================================================================================
-PhyCharacter::PhyCharacter(Physics& physics_, const Initializer& init, Object* parent):
+inline Character::Initializer::Initializer():
+	characterHeight(2.0),
+	characterWidth(0.75),
+	stepHeight(1.0),
+	jumpSpeed(10.0),
+	maxJumpHeight(0.0),
+	sceneNode(NULL),
+	startTrf(Transform::getIdentity())
+{}
+
+
+//======================================================================================================================
+// Contructor                                                                                                          =
+//======================================================================================================================
+Character::Character(MasterContainer& masterContainer_, const Initializer& init, Object* parent):
 	Object(parent),
-	physics(physics_)
+	masterContainer(masterContainer_)
 {
 	ghostObject = new btPairCachingGhostObject();
 
-	motionState = new MotionState(init.startTrf, init.sceneNode, this);
+	motionState = new MotionState(init.startTrf, init.sceneNode);
 
 	btAxisSweep3* sweepBp = dynamic_cast<btAxisSweep3*>(physics.broadphase);
 	ASSERT(sweepBp != NULL);
@@ -50,7 +67,7 @@ PhyCharacter::PhyCharacter(Physics& physics_, const Initializer& init, Object* p
 //======================================================================================================================
 // Destructor                                                                                                          =
 //======================================================================================================================
-PhyCharacter::~PhyCharacter()
+Character::~Character()
 {
 	physics.characters.erase(std::find(physics.characters.begin(), physics.characters.end(), this));
 	physics.dynamicsWorld->removeAction(character);
@@ -66,7 +83,7 @@ PhyCharacter::~PhyCharacter()
 //======================================================================================================================
 // rotate                                                                                                              =
 //======================================================================================================================
-void PhyCharacter::rotate(float angle)
+void Character::rotate(float angle)
 {
 	btMatrix3x3 rot = ghostObject->getWorldTransform().getBasis();
 	rot *= btMatrix3x3(btQuaternion(btVector3(0, 1, 0), angle));
@@ -77,7 +94,7 @@ void PhyCharacter::rotate(float angle)
 //======================================================================================================================
 // moveForward                                                                                                         =
 //======================================================================================================================
-void PhyCharacter::moveForward(float distance)
+void Character::moveForward(float distance)
 {
 	btVector3 forward = -ghostObject->getWorldTransform().getBasis().getColumn(2);
 	character->setWalkDirection(forward * distance);
@@ -87,7 +104,10 @@ void PhyCharacter::moveForward(float distance)
 //======================================================================================================================
 // jump                                                                                                                =
 //======================================================================================================================
-void PhyCharacter::jump()
+void Character::jump()
 {
 	character->jump();
 }
+
+
+} // end namespace

+ 18 - 25
src/Physics/PhyCharacter.h → src/Physics/PhysCharacter.h

@@ -1,31 +1,32 @@
-#ifndef PHY_CHARACTER_H
-#define PHY_CHARACTER_H
+#ifndef PHYS_CHARACTER_H
+#define PHYS_CHARACTER_H
 
-#include "Physics.h"
+#include "PhysMasterContainer.h"
 #include "Math.h"
 #include "Object.h"
 
 
-class Physics;
 class btPairCachingGhostObject;
 class btConvexShape;
 class btKinematicCharacterController;
 class btGhostPairCallback;
-class MotionState;
 class SceneNode;
+namespace Phys {
+class MasterContainer;
+class MotionState;
+}
+
+
+namespace Phys {
 
 
-/**
- * Its basically a wrapper around bullet character
- */
-class PhyCharacter: public Object
+/// Its basically a wrapper around bullet character
+class Character
 {
-	friend class Physics;
+	friend class MasterContainer;
 
 	public:
-		/**
-		 * Initializer class
-		 */
+		/// Initializer class
 		struct Initializer
 		{
 			float characterHeight;
@@ -39,14 +40,14 @@ class PhyCharacter: public Object
 			Initializer();
 		};
 
-		PhyCharacter(Physics& physics_, const Initializer& init, Object* parent = NULL);
-		~PhyCharacter();
+		Character(MasterContainer& masterContainer_, const Initializer& init);
+		~Character();
 		void rotate(float angle);
 		void moveForward(float distance);
 		void jump();
 
 	private:
-		Physics& physics; ///< Know your father
+		MasterContainer& masterContainer; ///< Know your father
 		btPairCachingGhostObject* ghostObject;
 		btConvexShape* convexShape;
 		btKinematicCharacterController* character;
@@ -55,15 +56,7 @@ class PhyCharacter: public Object
 };
 
 
-inline PhyCharacter::Initializer::Initializer():
-	characterHeight(2.0),
-	characterWidth(0.75),
-	stepHeight(1.0),
-	jumpSpeed(10.0),
-	maxJumpHeight(0.0),
-	sceneNode(NULL),
-	startTrf(Transform::getIdentity())
-{}
+} // end namespace
 
 
 #endif

+ 15 - 13
src/Physics/Physics.cpp → src/Physics/PhysMasterContainer.cpp

@@ -1,15 +1,17 @@
 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
-#include "Physics.h"
-#include "PhyCharacter.h"
-#include "MotionState.h"
+#include "PhysMasterContainer.h"
+#include "PhysCharacter.h"
+#include "PhysMotionState.h"
+
+
+namespace Phys {
 
 
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Physics::Physics():
-	defaultContactProcessingThreshold(BT_LARGE_FLOAT),
-	time(0.0)
+MasterContainer::MasterContainer():
+	defaultContactProcessingThreshold(BT_LARGE_FLOAT)
 {
 	collisionConfiguration = new btDefaultCollisionConfiguration();
 	dispatcher = new	btCollisionDispatcher(collisionConfiguration);
@@ -18,21 +20,18 @@ Physics::Physics():
 	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, sol, collisionConfiguration);
 	dynamicsWorld->setGravity(btVector3(0,-10, 0));
 
-	debugDrawer = new PhyDbgDrawer;
+	/*debugDrawer = new PhyDbgDrawer;
 	dynamicsWorld->setDebugDrawer(debugDrawer);
-	dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
+	dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);*/
 }
 
 
 //======================================================================================================================
 // update                                                                                                              =
 //======================================================================================================================
-void Physics::update(float crntTime)
+void MasterContainer::update(uint prevUpdateTime, uint crntTime)
 {
-	float dt = crntTime - time;
-	time = crntTime;
-
-	dynamicsWorld->stepSimulation(dt);
+	dynamicsWorld->stepSimulation((crntTime - prevUpdateTime) * 1000.0);
 
 	// updateNonRigidBodiesMotionStates
 	for(uint i=0; i<characters.size(); i++)
@@ -40,3 +39,6 @@ void Physics::update(float crntTime)
 		characters[i]->motionState->setWorldTransform(characters[i]->ghostObject->getWorldTransform());
 	}
 }
+
+
+} // end namespace

+ 20 - 24
src/Physics/Physics.h → src/Physics/PhysMasterContainer.h

@@ -1,29 +1,30 @@
-#ifndef PHYSICS_H
-#define PHYSICS_H
+#ifndef PHYS_MASTER_CONTAINER_H
+#define PHYS_MASTER_CONTAINER_H
 
 #include <btBulletCollisionCommon.h>
 #include <btBulletDynamicsCommon.h>
 #include "BtAndAnkiConvertors.h"
-#include "PhyDbgDrawer.h"
+#include "PhyDbgDrawer.h" ///< @todo Remove this crap from here. Its Renderer's stuff
 #include "Vec.h"
 
 
+namespace Phys {
 class PhyCharacter;
 class RigidBody;
+}
+
+
+namespace Phys {
 
 
-/**
- * The master container for all physics related stuff.
- */
-class Physics
+/// The master container for all physics related stuff.
+class MasterContainer
 {
 	friend class PhyCharacter; ///< For registering and unregistering
 	friend class RigidBody;  ///< For registering and unregistering
 
 	public:
-		/**
-		 * Collision groups
-		 */
+		/// Collision groups
 		enum CollisionGroup
 		{
 			CG_NOTHING = 0,
@@ -33,16 +34,15 @@ class Physics
 		};
 
 	public:
-		Physics();
-		void update(float crntTime);
-		void debugDraw();
+		MasterContainer();
 
-		/**
-		 * @name Accessors
-		 */
-		/**@{*/
+		/// @name Accessors
+		/// @{
 		btDiscreteDynamicsWorld& getWorld() {return *dynamicsWorld;}
-		/**@}*/
+		/// @}
+
+		/// Time as always in ms
+		void update(uint prevUpdateTime, uint crntTime);
 
 	private:
 		btDiscreteDynamicsWorld* dynamicsWorld;
@@ -50,16 +50,12 @@ class Physics
 		btCollisionDispatcher* dispatcher;
 		btBroadphaseInterface* broadphase;
 		btSequentialImpulseConstraintSolver* sol;
-		PhyDbgDrawer* debugDrawer;
+		PhyDbgDrawer* debugDrawer; ///< @todo Remove this crap from here. Its Renderer's stuff
 		float defaultContactProcessingThreshold;
 		Vec<PhyCharacter*> characters;
-		float time; ///< Time of prev update
 };
 
 
-inline void Physics::debugDraw()
-{
-	dynamicsWorld->debugDrawWorld();
-}
+} // end namespace
 
 #endif

+ 11 - 11
src/Physics/MotionState.h → src/Physics/PhysMotionState.h

@@ -1,16 +1,18 @@
-#ifndef MOTIONSTATE_H
-#define MOTIONSTATE_H
+#ifndef PHYS_MOTION_STATE_H
+#define PHYS_MOTION_STATE_H
 
 #include <LinearMath/btMotionState.h>
 #include "SceneNode.h"
-#include "Object.h"
+
+
+namespace Phys {
 
 
 /// A custom motion state
-class MotionState: public btMotionState, public Object
+class MotionState: public btMotionState
 {
 	public:
-		MotionState(const Transform& initialTransform, SceneNode* node_, Object* parent);
+		MotionState(const Transform& initialTransform, SceneNode* node_);
 		~MotionState() {}
 
 		/// @name Bullet implementation of virtuals
@@ -26,12 +28,7 @@ class MotionState: public btMotionState, public Object
 };
 
 
-//======================================================================================================================
-// Inlines                                                                                                             =
-//======================================================================================================================
-
-inline MotionState::MotionState(const Transform& initialTransform, SceneNode* node_, Object* parent):
-	Object(parent),
+inline MotionState::MotionState(const Transform& initialTransform, SceneNode* node_):
 	worldTransform(toBt(initialTransform)),
 	node(node_)
 {}
@@ -63,4 +60,7 @@ inline void MotionState::setWorldTransform(const btTransform& worldTrans)
 }
 
 
+} // end namespace
+
+
 #endif

+ 2 - 5
src/Physics/Ragdoll.h → src/Physics/PhysRagdoll.h

@@ -1,12 +1,9 @@
-#ifndef _RAGDOLL_H_
+/*#ifndef _RAGDOLL_H_
 #define _RAGDOLL_H_
 
 #include "PhyCommon.h"
 
 
-/**
- *
- */
 class Ragdoll
 {
 	public:
@@ -60,4 +57,4 @@ class Ragdoll
 };
 
 
-#endif
+#endif */

+ 37 - 11
src/Physics/RigidBody.cpp → src/Physics/PhysRigidBody.cpp

@@ -1,16 +1,31 @@
-#include "RigidBody.h"
-#include "Physics.h"
+#include "PhysRigidBody.h"
+#include "PhysMasterContainer.h"
 #include "Scene.h"
-#include "MotionState.h"
+#include "PhysMotionState.h"
+
+
+namespace Phys {
 
 
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-RigidBody::RigidBody(Physics& physics_, const Initializer& init, Object* parent):
+RigidBody::Initializer::Initializer():
+	mass(0.0),
+	startTrf(Transform::getIdentity()),
+	shape(NULL),
+	sceneNode(NULL),
+	group(-1),
+	mask(-1)
+{}
+
+
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+RigidBody::RigidBody(MasterContainer& masterContainer_, const Initializer& init):
   btRigidBody(btRigidBody::btRigidBodyConstructionInfo(0.0, NULL, NULL, btVector3(0.0, 0.0, 0.0))), // dummy init
-  Object(parent),
-  physics(physics_)
+  masterContainer(masterContainer_)
 {
 	ASSERT(init.shape != NULL && init.shape->getShapeType() != INVALID_SHAPE_PROXYTYPE);
 
@@ -18,13 +33,17 @@ RigidBody::RigidBody(Physics& physics_, const Initializer& init, Object* parent)
 
 	btVector3 localInertia;
 	if(isDynamic)
+	{
 		init.shape->calculateLocalInertia(init.mass, localInertia);
+	}
 	else
+	{
 		localInertia = btVector3(0.0, 0.0, 0.0);
+	}
 
-	motionState = new MotionState(init.startTrf, init.sceneNode, this);
+	motionState.reset(new MotionState(init.startTrf, init.sceneNode));
 
-	btRigidBody::btRigidBodyConstructionInfo cInfo(init.mass, motionState, init.shape, localInertia);
+	btRigidBody::btRigidBodyConstructionInfo cInfo(init.mass, motionState.get(), init.shape, localInertia);
 
 	setupRigidBody(cInfo);
 
@@ -33,10 +52,14 @@ RigidBody::RigidBody(Physics& physics_, const Initializer& init, Object* parent)
 	forceActivationState(ISLAND_SLEEPING);
 
 	// register
-	if(init.mask==-1 || init.group==-1)
-		physics.dynamicsWorld->addRigidBody(this);
+	if(init.mask == -1 || init.group == -1)
+	{
+		masterContainer.dynamicsWorld->addRigidBody(this);
+	}
 	else
-		physics.dynamicsWorld->addRigidBody(this, init.group, init.mask);
+	{
+		masterContainer.dynamicsWorld->addRigidBody(this, init.group, init.mask);
+	}
 }
 
 
@@ -47,3 +70,6 @@ RigidBody::~RigidBody()
 {
 	physics.dynamicsWorld->removeRigidBody(this);
 }
+
+
+} // end namespace

+ 52 - 0
src/Physics/PhysRigidBody.h

@@ -0,0 +1,52 @@
+#ifndef PHYS_RIGID_BODY_H
+#define PHYS_RIGID_BODY_H
+
+#include <boost/scoped_ptr.hpp>
+#include <btBulletDynamicsCommon.h>
+#include <btBulletCollisionCommon.h>
+#include "Math.h"
+
+
+class SceneNode;
+namespace Phys {
+class MotionState;
+class MasterContainer;
+}
+
+
+namespace Phys {
+
+
+/// Wrapper for rigid body
+class RigidBody: public btRigidBody
+{
+	public:
+		/// Initializer class
+		struct Initializer
+		{
+			float mass;
+			Transform startTrf;
+			btCollisionShape* shape;
+			SceneNode* sceneNode;
+			int group;
+			int mask;
+
+			Initializer();
+		};
+
+		/// Init and register
+		RigidBody(MasterContainer& masterContainer, const Initializer& init);
+
+		/// Unregister
+		~RigidBody();
+
+	private:
+		MasterContainer& masterContainer; ///< Know your father
+		boost::scoped_ptr<MotionState> motionState; ///< Keep it here for garbage collection
+};
+
+
+} // end namespace
+
+
+#endif

+ 0 - 62
src/Physics/RigidBody.h

@@ -1,62 +0,0 @@
-#ifndef MY_RIGIDBODY_H // Watch the naming cause Bullet uses the same
-#define MY_RIGIDBODY_H
-
-#include <memory>
-#include <btBulletDynamicsCommon.h>
-#include <btBulletCollisionCommon.h>
-#include "Math.h"
-#include "Object.h"
-
-
-class SceneNode;
-class MotionState;
-class Physics;
-
-
-/**
- * Wrapper for rigid body
- */
-class RigidBody: public btRigidBody, public Object
-{
-	public:
-		/**
-		 * Initializer class
-		 */
-		struct Initializer
-		{
-			float mass;
-			Transform startTrf;
-			btCollisionShape* shape;
-			SceneNode* sceneNode;
-		  int group;
-		  int mask;
-
-		  Initializer();
-		};
-
-		/**
-		 * Init and register
-		 */
-		RigidBody(Physics& physics, const Initializer& init, Object* parent = NULL);
-
-		/**
-		 * Unregister
-		 */
-		~RigidBody();
-
-	private:
-		Physics& physics; ///< Know your father
-		MotionState* motionState; ///< Keep it here as well for garbage collection
-};
-
-
-inline RigidBody::Initializer::Initializer():
-	mass(0.0),
-	startTrf(Transform::getIdentity()),
-	shape(NULL),
-	sceneNode(NULL),
-	group(-1),
-	mask(-1)
-{}
-
-#endif

+ 2 - 2
src/Scene/ParticleEmitter.cpp

@@ -1,7 +1,7 @@
 #include <btBulletCollisionCommon.h>
 #include <btBulletDynamicsCommon.h>
 #include "ParticleEmitter.h"
-#include "RigidBody.h"
+#include "PhysRigidBody.h"
 #include "MainRenderer.h"
 #include "App.h"
 #include "Scene.h"
@@ -58,7 +58,7 @@ void ParticleEmitter::init(const char* filename)
 		init.sceneNode = particle;
 		init.group = Physics::CG_PARTICLE;
 		init.mask = Physics::CG_ALL ^ Physics::CG_PARTICLE;
-		RigidBody* body = new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
+		Phys::RigidBody* body = new Phys::RigidBody(SceneSingleton::getInstance().getPhysics(), init);
 
 		body->forceActivationState(DISABLE_SIMULATION);
 

+ 3 - 3
src/Scene/ParticleEmitter.h

@@ -2,15 +2,15 @@
 #define PARTICLEEMITTER_H
 
 #include <boost/ptr_container/ptr_vector.hpp>
-#include <memory>
+#include <boost/scoped_ptr.hpp>
 #include <btBulletCollisionCommon.h>
 #include "SceneNode.h"
 #include "GhostNode.h"
 #include "ParticleEmitterProps.h"
 #include "RsrcPtr.h"
+#include "PhysRigidBody.h"
 
 
-class RigidBody;
 class btCollisionShape;
 
 
@@ -23,7 +23,7 @@ class ParticleEmitter: public SceneNode, public ParticleEmitterPropsStruct
 		{
 			public:
 				float timeOfDeath; ///< Life of death. If < 0.0 then dead. In seconds
-				std::auto_ptr<RigidBody> body;
+				boost::scoped_ptr<Phys::RigidBody> body;
 
 				Particle();
 				void render();

+ 1 - 1
src/Scene/Scene.cpp

@@ -21,7 +21,7 @@ Scene::Scene()
 	ambientCol = Vec3(0.1, 0.05, 0.05) * 4;
 	//sunPos = Vec3(0.0, 1.0, -1.0) * 50.0;
 
-	physics.reset(new Physics);
+	physMasterContainer.reset(new Phys::MasterContainer);
 
 	visibilityTester.reset(new VisibilityTester(*this));
 }

+ 4 - 4
src/Scene/Scene.h

@@ -2,7 +2,7 @@
 #define SCENE_H
 
 #include <boost/scoped_ptr.hpp>
-#include "Physics.h"
+#include "PhysMasterContainer.h"
 #include "Assert.h"
 #include "Accessors.h"
 #include "Singleton.h"
@@ -47,8 +47,8 @@ class Scene
 		/// @name Accessors
 		/// @{
 		GETTER_SETTER(Vec3, ambientCol, getAmbientCol, setAmbientCol)
-		Physics& getPhysics() {return *physics;}
-		const Physics& getPhysics() const {return *physics;}
+		Phys::MasterContainer& getPhysMasterContainer() {return *physMasterContainer;}
+		const Phys::MasterContainer& getPhysMasterContainer() const {return *physMasterContainer;}
 		const VisibilityTester& getVisibilityTester() const {return *visibilityTester;}
 
 		GETTER_RW(Types<SceneNode>::Container, nodes, getAllNodes)
@@ -73,7 +73,7 @@ class Scene
 		/// @}
 
 		Vec3 ambientCol; ///< The global ambient color
-		boost::scoped_ptr<Physics> physics; ///< Connection with Bullet wrapper
+		boost::scoped_ptr<Phys::MasterContainer> physMasterContainer; ///< Connection with Bullet wrapper
 		boost::scoped_ptr<VisibilityTester> visibilityTester;
 
 		/// Adds a node in a container

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor