Panagiotis Christopoulos Charitos 15 gadi atpakaļ
vecāks
revīzija
ff11408839

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 361 - 418
build/debug/Makefile


+ 1 - 1
src/Misc/TestHeader.cpp

@@ -1 +1 @@
-#include "DebugDrawer.h"
+#include "RigidBody.h"

+ 6 - 9
src/Physics/MotionState.h

@@ -4,15 +4,16 @@
 #include "Common.h"
 #include "Common.h"
 #include "PhyCommon.h"
 #include "PhyCommon.h"
 #include "SceneNode.h"
 #include "SceneNode.h"
+#include "Object.h"
 
 
 
 
 /**
 /**
- *
+ * A custom motion state
  */
  */
-class MotionState: public btMotionState
+class MotionState: public btMotionState, public Object
 {
 {
 	public:
 	public:
-		MotionState(const btTransform& initialTransform, SceneNode& node_);
+		MotionState(const btTransform& initialTransform, SceneNode& node_, Object* parent = NULL);
 
 
 		~MotionState() {}
 		~MotionState() {}
 
 
@@ -32,7 +33,8 @@ class MotionState: public btMotionState
 // Inlines                                                                                                             =
 // Inlines                                                                                                             =
 //======================================================================================================================
 //======================================================================================================================
 
 
-inline MotionState::MotionState(const btTransform& initialTransform, SceneNode& node_):
+inline MotionState::MotionState(const btTransform& initialTransform, SceneNode& node_, Object* parent):
+	Object(parent),
 	worldTransform(initialTransform),
 	worldTransform(initialTransform),
 	node(node_)
 	node(node_)
 {}
 {}
@@ -57,11 +59,6 @@ inline void MotionState::setWorldTransform(const btTransform& worldTrans)
 
 
 	node.setLocalTransform(Transform(toAnki(worldTrans)));
 	node.setLocalTransform(Transform(toAnki(worldTrans)));
 	node.getLocalTransform().setScale(originalScale);
 	node.getLocalTransform().setScale(originalScale);
-
-	/*btQuaternion rot = worldTrans.getRotation();
-	node.rotationLspace = Mat3(Quat(toAnki(rot)));
-	btVector3 pos = worldTrans.getOrigin();
-	node.translationLspace = Vec3(toAnki(pos));*/
 }
 }
 
 
 
 

+ 41 - 3
src/Physics/Physics.cpp

@@ -20,6 +20,7 @@ Physics::Physics():
 	dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
 	dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
 }
 }
 
 
+
 //======================================================================================================================
 //======================================================================================================================
 // createNewRigidBody                                                                                                  =
 // createNewRigidBody                                                                                                  =
 //======================================================================================================================
 //======================================================================================================================
@@ -35,12 +36,14 @@ btRigidBody* Physics::createNewRigidBody(float mass, const Transform& startTrans
 	if(isDynamic)
 	if(isDynamic)
 		shape->calculateLocalInertia(mass,localInertia);
 		shape->calculateLocalInertia(mass,localInertia);
 
 
+	btRigidBody::btRigidBodyConstructionInfo cInfo(mass, NULL, shape, localInertia);
+
+	btRigidBody* body = new btRigidBody(cInfo);
 
 
-	MotionState* myMotionState = new MotionState(toBt(startTransform), *node);
+	MotionState* myMotionState = new MotionState(toBt(startTransform), *node/*, body*/);
 
 
-	btRigidBody::btRigidBodyConstructionInfo cInfo(mass, myMotionState, shape, localInertia);
+	body->setMotionState(myMotionState);
 
 
-	btRigidBody* body = new btRigidBody(cInfo);
 	body->setContactProcessingThreshold(defaultContactProcessingThreshold);
 	body->setContactProcessingThreshold(defaultContactProcessingThreshold);
 
 
 	if(mask==-1 || group==-1)
 	if(mask==-1 || group==-1)
@@ -51,3 +54,38 @@ btRigidBody* Physics::createNewRigidBody(float mass, const Transform& startTrans
 	return body;
 	return body;
 }
 }
 
 
+
+//======================================================================================================================
+// deleteRigidBody                                                                                                     =
+//======================================================================================================================
+void Physics::deleteRigidBody(btRigidBody* body)
+{
+	/*// find the body
+	btRigidBody* body1 = NULL;
+	btCollisionObject* obj;
+	int i;
+	for(i=0; i<dynamicsWorld->getNumCollisionObjects(); i++)
+	{
+		obj = dynamicsWorld->getCollisionObjectArray()[i];
+		body1 = btRigidBody::upcast(obj);
+
+		if(body1 == body)
+			break;
+	}
+
+	// check
+	if(body1 == NULL)
+	{
+		ERROR("Cannot find body 0x" << hex << body);
+		return;
+	}
+
+	// remove it from world
+	dynamicsWorld->removeCollisionObject(obj);
+
+	// delete motion state
+	if (body && body->getMotionState())
+	{
+		delete body->getMotionState();
+	}*/
+}

+ 18 - 12
src/Physics/Physics.h

@@ -1,5 +1,5 @@
-#ifndef _PHYWORLD_H_
-#define _PHYWORLD_H_
+#ifndef PHYSICS_H
+#define PHYSICS_H
 
 
 #include "Common.h"
 #include "Common.h"
 #include "PhyCommon.h"
 #include "PhyCommon.h"
@@ -12,20 +12,11 @@
  */
  */
 class Physics
 class Physics
 {
 {
-	PROPERTY_R(btDiscreteDynamicsWorld*, dynamicsWorld, getDynamicsWorld)
-	PROPERTY_R(float, defaultContactProcessingThreshold, getDefaultContactProcessingThreshold)
-
 	public:
 	public:
-		btDefaultCollisionConfiguration* collisionConfiguration;
-		btCollisionDispatcher* dispatcher;
-		btDbvtBroadphase* broadphase;
-		btSequentialImpulseConstraintSolver* sol;
-		DebugDrawer* debugDrawer;
-
 		/**
 		/**
 		 * Collision groups
 		 * Collision groups
 		 */
 		 */
-		enum
+		enum CollisionGroup
 		{
 		{
 			CG_NOTHING = 0,
 			CG_NOTHING = 0,
 			CG_MAP = 1,
 			CG_MAP = 1,
@@ -33,6 +24,16 @@ class Physics
 			CG_ALL = CG_MAP | CG_PARTICLE
 			CG_ALL = CG_MAP | CG_PARTICLE
 		};
 		};
 
 
+	PROPERTY_R(btDiscreteDynamicsWorld*, dynamicsWorld, getDynamicsWorld)
+	PROPERTY_R(float, defaultContactProcessingThreshold, getDefaultContactProcessingThreshold)
+
+	public:
+		btDefaultCollisionConfiguration* collisionConfiguration;
+		btCollisionDispatcher* dispatcher;
+		btDbvtBroadphase* broadphase;
+		btSequentialImpulseConstraintSolver* sol;
+		DebugDrawer* debugDrawer;
+
 		Physics();
 		Physics();
 
 
 		/**
 		/**
@@ -48,6 +49,11 @@ class Physics
 		 */
 		 */
 		btRigidBody* createNewRigidBody(float mass, const Transform& startTransform, btCollisionShape* shape,
 		btRigidBody* createNewRigidBody(float mass, const Transform& startTransform, btCollisionShape* shape,
 		                                SceneNode* node, int group = -1, int mask = -1);
 		                                SceneNode* node, int group = -1, int mask = -1);
+
+		/**
+		 * Removes the body from the world, deletes its motion state and deletes it
+		 */
+		void deleteRigidBody(btRigidBody* body);
 };
 };
 
 
 #endif
 #endif

+ 35 - 0
src/Physics/RigidBody.cpp

@@ -0,0 +1,35 @@
+#include "RigidBody.h"
+#include "App.h"
+#include "Physics.h"
+#include "Scene.h"
+
+
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+RigidBody::RigidBody(float mass, const Transform& startTransform, btCollisionShape* shape, SceneNode* node,
+                     int group, int mask, Object* parent):
+  btRigidBody(btRigidBody::btRigidBodyConstructionInfo(0.0, NULL, NULL, btVector3(0.0, 0.0, 0.0))),
+	Object(parent)
+{
+	DEBUG_ERR(shape==NULL || shape->getShapeType()==INVALID_SHAPE_PROXYTYPE);
+
+	bool isDynamic = (mass != 0.0);
+
+	btVector3 localInertia(0.0, 0.0, 0.0);
+	if(isDynamic)
+		shape->calculateLocalInertia(mass,localInertia);
+
+	MotionState* myMotionState = new MotionState(toBt(startTransform), *node, this);
+
+	btRigidBody::btRigidBodyConstructionInfo cInfo(mass, myMotionState, shape, localInertia);
+
+	setupRigidBody(cInfo);
+
+	setContactProcessingThreshold(defaultContactProcessingThreshold);
+
+	if(mask==-1 || group==-1)
+		app->getScene()->getPhysics()->getDynamicsWorld()->addRigidBody(this);
+	else
+		app->getScene()->getPhysics()->getDynamicsWorld()->addRigidBody(this, group, mask);
+}

+ 36 - 0
src/Physics/RigidBody.h

@@ -0,0 +1,36 @@
+#ifndef MY_RIGIDBODY_H // Watch the naming cause Bullet uses the same
+#define MY_RIGIDBODY_H
+
+#include <btBulletDynamicsCommon.h>
+#include <btBulletCollisionCommon.h>
+#include "Common.h"
+#include "Object.h"
+#include "Math.h"
+
+
+class SceneNode;
+
+
+/**
+ * Wrapper for rigid body
+ */
+class RigidBody: public btRigidBody, public Object
+{
+	public:
+		/**
+		 *
+		 * @param mass
+		 * @param startTransform
+		 * @param shape
+		 * @param node
+		 * @param group
+		 * @param mask
+		 * @param parent
+		 * @return
+		 */
+		RigidBody(float mass, const Transform& startTransform, btCollisionShape* shape, SceneNode* node,
+		          int group = -1, int mask = -1, Object* parent = NULL);
+};
+
+
+#endif

+ 10 - 13
src/Scene/ParticleEmitter.cpp

@@ -38,24 +38,21 @@ void ParticleEmitter::init(const char* filename)
 
 
 	// create the particles
 	// create the particles
 	btCollisionShape* colShape = new btSphereShape(0.5);
 	btCollisionShape* colShape = new btSphereShape(0.5);
-	btTransform startTransform;
+	Transform startTransform;
 	startTransform.setIdentity();
 	startTransform.setIdentity();
 
 
-	particles.resize(maxNumOfParticles);
 	for(uint i = 0; i < maxNumOfParticles; i++)
 	for(uint i = 0; i < maxNumOfParticles; i++)
 	{
 	{
-		particles[i] = new Particle;
-		float mass = particleMass + randFloat(particleMassMargin) * 2.0 - particleMassMargin;
+		particles.push_back(new Particle);
+		Particle* particle = &particles.back();
+		float mass = particleMass + Util::randFloat(particleMassMargin) * 2.0 - particleMassMargin;
 
 
-		/*btVector3 origin = startTransform.getOrigin();
-		 origin.setX( origin.getX() + 0.6*2.0);
-		 startTransform.setOrigin(origin);*/
-
-		btRigidBody* body = physics->createNewRigidBody(mass, startTransform, colShape, Physics::CG_PARTICLE, Physics::CG_ALL
-		    ^ Physics::CG_PARTICLE);
+		btRigidBody* body = app->getScene()->getPhysics()->createNewRigidBody(mass, startTransform, colShape, particle,
+		                                                                      Physics::CG_PARTICLE,
+		                                                                      Physics::CG_ALL ^ Physics::CG_PARTICLE);
 		body->forceActivationState(DISABLE_SIMULATION);
 		body->forceActivationState(DISABLE_SIMULATION);
-		//body->setActivationState(ISLAND_SLEEPING);
-		particles[i]->body = body;
+
+		particle->body = body;
 	}
 	}
 }
 }
 
 
@@ -78,7 +75,7 @@ void ParticleEmitter::update()
 	float crntTime = app->getTicks() / 1000.0;
 	float crntTime = app->getTicks() / 1000.0;
 
 
 	// Opt: We dont have to make extra calculations if the ParticleEmitter's rotation is the identity
 	// Opt: We dont have to make extra calculations if the ParticleEmitter's rotation is the identity
-	bool identRot = (worldTransform.getRotation() == Mat3::getIdentity()) ? true : false;
+	bool identRot = worldTransform.getRotation() == Mat3::getIdentity();
 
 
 	// deactivate the dead particles
 	// deactivate the dead particles
 	for(uint i=0; i<particles.size(); i++)
 	for(uint i=0; i<particles.size(); i++)

+ 39 - 12
src/Scene/SceneNode.h

@@ -1,5 +1,5 @@
-#ifndef _NODE_H_
-#define _NODE_H_
+#ifndef SCENE_NODE_H
+#define SCENE_NODE_H
 
 
 #include <memory>
 #include <memory>
 #include "Common.h"
 #include "Common.h"
@@ -12,11 +12,13 @@ class Material;
 class Controller;
 class Controller;
 
 
 
 
-/// Scene node
+/**
+ * The backbone of scene
+ */
 class SceneNode
 class SceneNode
 {
 {
 	public:
 	public:
-		enum NodeType
+		enum Type
 		{
 		{
 			NT_GHOST,
 			NT_GHOST,
 			NT_LIGHT,
 			NT_LIGHT,
@@ -33,25 +35,32 @@ class SceneNode
 	public:
 	public:
 		SceneNode* parent;
 		SceneNode* parent;
 		Vec<SceneNode*> childs;
 		Vec<SceneNode*> childs;
-		NodeType type;
+		Type type;
 		bvolume_t* bvolumeLspace;
 		bvolume_t* bvolumeLspace;
 		bvolume_t* bvolumeWspace;
 		bvolume_t* bvolumeWspace;
 		bool isCompound;
 		bool isCompound;
 		
 		
-		SceneNode(NodeType type_);
-		SceneNode(NodeType type_, SceneNode* parent);
+		SceneNode(Type type_);
+		SceneNode(Type type_, SceneNode* parent);
 		virtual ~SceneNode();
 		virtual ~SceneNode();
 		virtual void render() = 0;
 		virtual void render() = 0;
 		virtual void init(const char*) = 0; ///< init using a script
 		virtual void init(const char*) = 0; ///< init using a script
 		virtual void deinit() = 0;
 		virtual void deinit() = 0;
 		virtual void updateWorldStuff() { updateWorldTransform(); } ///< This update happens only when the object gets moved. Override it if you want more
 		virtual void updateWorldStuff() { updateWorldTransform(); } ///< This update happens only when the object gets moved. Override it if you want more
 		void updateWorldTransform();
 		void updateWorldTransform();
-		void rotateLocalX(float angDegrees) { localTransform.getRotation().rotateXAxis(angDegrees); }
-		void rotateLocalY(float angDegrees) { localTransform.getRotation().rotateYAxis(angDegrees); }
-		void rotateLocalZ(float angDegrees) { localTransform.getRotation().rotateZAxis(angDegrees); }
+
+		/**
+		 * @name Local transform
+		 */
+		/**@{*/
+		void rotateLocalX(float angDegrees);
+		void rotateLocalY(float angDegrees);
+		void rotateLocalZ(float angDegrees);
 		void moveLocalX(float distance);
 		void moveLocalX(float distance);
 		void moveLocalY(float distance);
 		void moveLocalY(float distance);
 		void moveLocalZ(float distance);
 		void moveLocalZ(float distance);
+		/**@}*/
+
 		void addChild(SceneNode* node);
 		void addChild(SceneNode* node);
 		void removeChild(SceneNode* node);
 		void removeChild(SceneNode* node);
 
 
@@ -60,14 +69,14 @@ class SceneNode
 };
 };
 
 
 
 
-inline SceneNode::SceneNode(NodeType type_):
+inline SceneNode::SceneNode(Type type_):
 	type(type_)
 	type(type_)
 {
 {
 	commonConstructorCode();
 	commonConstructorCode();
 }
 }
 
 
 
 
-inline SceneNode::SceneNode(NodeType type_, SceneNode* parent):
+inline SceneNode::SceneNode(Type type_, SceneNode* parent):
 	type(type_)
 	type(type_)
 {
 {
 	commonConstructorCode();
 	commonConstructorCode();
@@ -75,4 +84,22 @@ inline SceneNode::SceneNode(NodeType type_, SceneNode* parent):
 }
 }
 
 
 
 
+inline void SceneNode::rotateLocalX(float angDegrees)
+{
+	localTransform.getRotation().rotateXAxis(angDegrees);
+}
+
+
+inline void SceneNode::rotateLocalY(float angDegrees)
+{
+	localTransform.getRotation().rotateYAxis(angDegrees);
+}
+
+
+inline void SceneNode::rotateLocalZ(float angDegrees)
+{
+	localTransform.getRotation().rotateZAxis(angDegrees);
+}
+
+
 #endif
 #endif

+ 75 - 0
src/Util/Object.h

@@ -0,0 +1,75 @@
+#ifndef OBJECT_H
+#define OBJECT_H
+
+#include "Common.h"
+#include "Vec.h"
+
+
+/**
+ * A class for automatic garbage collection. Cause we -the programmers- get bored when it comes to deallocation
+ */
+class Object
+{
+	public:
+		Object(Object* parent = NULL);
+		virtual ~Object();
+
+		/**
+		 * @name Accessors
+		 */
+		/**@{*/
+		const Object* getParent() const;
+		const Vec<Object*> getChilds() const; ///< Get the childs Vec
+		/**@}*/
+
+	private:
+		Object* parent;
+		Vec<Object*> childs;
+
+		void addChild(Object* child);
+};
+
+
+//======================================================================================================================
+// Inlines                                                                                                             =
+//======================================================================================================================
+
+inline Object::Object(Object* parent)
+{
+	if(parent)
+		parent->addChild(this);
+}
+
+
+inline Object::~Object()
+{
+	for(Vec<Object*>::iterator it=childs.begin(); it!=childs.end(); it++)
+	{
+		delete *it;
+	}
+}
+
+
+inline const Object* Object::getParent() const
+{
+	return parent;
+}
+
+
+inline const Vec<Object*> Object::getChilds() const
+{
+	return childs;
+}
+
+
+inline void Object::addChild(Object* child)
+{
+	DEBUG_ERR(child == NULL);
+	DEBUG_ERR(child->parent != NULL); // Child already has parent
+
+	child->parent = this;
+	childs.push_back(child);
+}
+
+
+#endif

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels