瀏覽代碼

Particle emitter

Panagiotis Christopoulos Charitos 16 年之前
父節點
當前提交
5b6e19159e
共有 8 個文件被更改,包括 76 次插入45 次删除
  1. 0 1
      TODO
  2. 4 15
      src/Main.cpp
  3. 17 5
      src/Physics/PhyWorld.h
  4. 34 1
      src/Scene/ParticleEmitter.cpp
  5. 10 5
      src/Scene/ParticleEmitter.h
  6. 2 0
      src/Scene/Scene.cpp
  7. 1 1
      src/Scene/Scene.h
  8. 8 17
      src/Util/Common.h

+ 0 - 1
TODO

@@ -7,4 +7,3 @@
 *Ask in the bullet forum:
 	*How to make floating particles like smoke. But first try with one body and manualy setting the gravity
 	*What the btCollisionObject::setActivationState takes as parameter?
-*In eclipse add to Project Properties -> C/C++ General -> Paths and Symbols -> Include the bullet src dir so it can color the structs and classes

+ 4 - 15
src/Main.cpp

@@ -57,11 +57,6 @@ class floor_t: public Camera
 
 
 // Physics
-btDefaultCollisionConfiguration* collisionConfiguration;
-btCollisionDispatcher* dispatcher;
-btDbvtBroadphase* broadphase;
-btSequentialImpulseConstraintSolver* sol;
-btDiscreteDynamicsWorld* dynamicsWorld;
 BulletDebuger debugDrawer;
 
 #define ARRAY_SIZE_X 5
@@ -78,13 +73,7 @@ BulletDebuger debugDrawer;
 
 void initPhysics()
 {
-	collisionConfiguration = new btDefaultCollisionConfiguration();
-	dispatcher = new	btCollisionDispatcher(collisionConfiguration);
-	broadphase = new btDbvtBroadphase();
-	sol = new btSequentialImpulseConstraintSolver;
-	dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, broadphase, sol, collisionConfiguration );
-
-	dynamicsWorld->setGravity(btVector3(0,-10,0));
+	btDiscreteDynamicsWorld* dynamicsWorld = app->scene->getPhyWorld()->getDynamicsWorld();
 
 	btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
 
@@ -153,7 +142,7 @@ void initPhysics()
 					btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
 					btRigidBody* body = new btRigidBody(rbInfo);
 
-
+					if( i=2 ) body->setActivationState(ISLAND_SLEEPING);
 
 					//body->setActivationState(ISLAND_SLEEPING);
 
@@ -323,8 +312,8 @@ int main( int /*argc*/, char* /*argv*/[] )
 		app->scene->updateAllWorldStuff();
 
 
-		dynamicsWorld->stepSimulation( app->timerTick );
-		dynamicsWorld->debugDrawWorld();
+		app->scene->getPhyWorld()->getDynamicsWorld()->stepSimulation( app->timerTick );
+		app->scene->getPhyWorld()->getDynamicsWorld()->debugDrawWorld();
 
 		R::render( *app->activeCam );
 

+ 17 - 5
src/Physics/PhyWorld.h

@@ -9,6 +9,14 @@
  */
 class PhyWorld
 {
+	PROPERTY_R( btDiscreteDynamicsWorld*, dynamicsWorld, getDynamicsWorld )
+
+	private:
+		btDefaultCollisionConfiguration* collisionConfiguration;
+		btCollisionDispatcher* dispatcher;
+		btDbvtBroadphase* broadphase;
+		btSequentialImpulseConstraintSolver* sol;
+
 	public:
 		/**
 		 * Collision groups
@@ -21,11 +29,15 @@ class PhyWorld
 		};
 
 
-		btDefaultCollisionConfiguration* collisionConfiguration;
-		btCollisionDispatcher* dispatcher;
-		btDbvtBroadphase* broadphase;
-		btSequentialImpulseConstraintSolver* sol;
-		btDiscreteDynamicsWorld* dynamicsWorld;
+		PhyWorld()
+		{
+			collisionConfiguration = new btDefaultCollisionConfiguration();
+			dispatcher = new	btCollisionDispatcher(collisionConfiguration);
+			broadphase = new btDbvtBroadphase();
+			sol = new btSequentialImpulseConstraintSolver;
+			dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, broadphase, sol, collisionConfiguration );
+			dynamicsWorld->setGravity(btVector3(0,-10,0));
+		}
 };
 
 #endif

+ 34 - 1
src/Scene/ParticleEmitter.cpp

@@ -10,6 +10,8 @@
 //=====================================================================================================================================
 void ParticleEmitter::Particle::render()
 {
+	if( lifeTillDeath < 0 ) return;
+
 	glPushMatrix();
 	R::multMatrix( transformationWspace );
 
@@ -50,7 +52,38 @@ void ParticleEmitter::init( const char* filename )
 		MotionState* mState = new MotionState( btTransform::getIdentity(), particles[i] );
 		btRigidBody::btRigidBodyConstructionInfo rbInfo( mass, mState, colShape, localInertia );
 		btRigidBody* body = new btRigidBody( rbInfo );
+		particles[i]->body = body;
 		body->setActivationState(ISLAND_SLEEPING);
-		app->scene->getPhyWorld()->dynamicsWorld->addRigidBody( body, PhyWorld::CG_PARTICLE, PhyWorld::CG_MAP );
+		app->scene->getPhyWorld()->getDynamicsWorld()->addRigidBody( body, PhyWorld::CG_PARTICLE, PhyWorld::CG_MAP );
+	}
+}
+
+
+//=====================================================================================================================================
+// update                                                                                                                             =
+//=====================================================================================================================================
+void ParticleEmitter::update()
+{
+	uint crntTime = app->getTicks();
+
+	// deactivate the dead particles
+	for( Vec<Particle*>::iterator it=particles.begin(); it!=particles.end(); ++it )
+	{
+		Particle& part = **it;
+
+		part.lifeTillDeath -= crntTime-timeOfPrevUpdate;
+		if( part.lifeTillDeath < 1 )
+		{
+			part.body->setActivationState( ISLAND_SLEEPING );
+		}
+	}
+
+	if( (crntTime - timeOfPrevEmittion) > emittionPeriod )
+	{
+		timeOfPrevEmittion = crntTime;
+
+		//for(  )
 	}
+
+	timeOfPrevUpdate = crntTime;
 }

+ 10 - 5
src/Scene/ParticleEmitter.h

@@ -4,6 +4,7 @@
 #include "Common.h"
 #include "Node.h"
 #include "MeshNode.h"
+#include "PhyCommon.h"
 
 
 /**
@@ -17,6 +18,7 @@ class ParticleEmitter: public Node
 		{
 			public:
 				int lifeTillDeath; ///< Life till death. If 0 then dead. In ms
+				btRigidBody* body;
 
 				Particle() {}
 				void render();
@@ -24,19 +26,21 @@ class ParticleEmitter: public Node
 		};
 
 		// the properties
-		int maxParticleLife;
-		int minParticleLife;
+		uint maxParticleLife;
+		uint minParticleLife;
 		Euler maxAngle;
 		Euler minAngle;
 		float minParticleMass;
 		float maxParticleMass;
-		int maxNumOfParticles; ///< The size of the particles vector
-		int emittionPeriod; ///< How often the emitter emits new particles. In ms
+		uint maxNumOfParticles; ///< The size of the particles vector
+		uint emittionPeriod; ///< How often the emitter emits new particles. In ms
+		uint particlesPerEmittion; ///< How many particles are emitted every emittion
 
 
 		// the changeable vars
 		Vec<Particle*> particles;
-		int life;
+		uint timeOfPrevUpdate;
+		uint timeOfPrevEmittion;
 
 		// funcs
 		ParticleEmitter(): Node( NT_PARTICLE_EMITTER ) {}
@@ -44,6 +48,7 @@ class ParticleEmitter: public Node
 		void renderDepth() {}
 		void init( const char* filename );
 		void deinit() {}
+		void update();
 };
 
 #endif

+ 2 - 0
src/Scene/Scene.cpp

@@ -15,6 +15,8 @@ Scene::Scene()
 {
 	ambientCol = Vec3( 0.1, 0.05, 0.05 )*1;
 	sunPos = Vec3( 0.0, 1.0, -1.0 ) * 50.0;
+
+	phyWorld = new PhyWorld;
 }
 
 

+ 1 - 1
src/Scene/Scene.h

@@ -21,7 +21,7 @@ class Scene
 {
 	PROPERTY_RW( Vec3, ambientCol, setAmbientCol, getAmbientCol )
 	PROPERTY_RW( Vec3, sunPos, setSunPos, getSunPos )
-	PTR_PROPERTY_RW( PhyWorld*, phyWorld, setPhyWorld, getPhyWorld )
+	PROPERTY_RW( PhyWorld*, phyWorld, setPhyWorld, getPhyWorld )
 
 	private:
 		template<typename ContainerType, typename Type> void putBackNode( ContainerType& container, Type* x )

+ 8 - 17
src/Util/Common.h

@@ -103,34 +103,25 @@ extern string getFunctionFromPrettyFunction( const char* pretty_function );
 #define FOREACH( x ) for( int i=0; i<x; i++ )
 
 
-/// useful property macros
+/// useful property macros. It concatenates and creates a unique type so it can accept pointers
 #define PROPERTY_RW( __Type__, __varName__, __setFunc__, __getFunc__ ) \
 	private: \
-		__Type__ __varName__; \
+		typedef __Type__ __Dummy__##__varName__; \
+		__Dummy__##__varName__ __varName__; \
 	public: \
-		void __setFunc__( const __Type__& __x__ ) { \
+		void __setFunc__( const __Dummy__##__varName__& __x__ ) { \
 			__varName__ = __x__; \
 		} \
-		const __Type__& __getFunc__() const { \
+		const __Dummy__##__varName__& __getFunc__() const { \
 			return __varName__; \
 		}
 
 #define PROPERTY_R( __Type__, __varName__, __getFunc__ ) \
 	private: \
-		__Type__ __varName__; \
+		typedef __Type__ __Dummy__##__varName__; \
+		__Dummy__##__varName__ __varName__; \
 	public: \
-		const __Type__& __getFunc__() const { \
-			return __varName__; \
-		}
-
-#define PTR_PROPERTY_RW( __Type__, __varName__, __setFunc__, __getFunc__ ) \
-	private: \
-		__Type__ __varName__; \
-	public: \
-		void __setFunc__( __Type__ __x__ ) { \
-			__varName__ = __x__; \
-		} \
-		const __Type__ __getFunc__() const { \
+		const __Dummy__##__varName__& __getFunc__() const { \
 			return __varName__; \
 		}