From 1032161366da1b2153de8d804465061e6bcc4fce Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Sun, 17 Oct 2010 21:25:27 +0000
Subject: moved bullet objets to core::Entity, moved docking functions to
 game.cc and removed entity->dock(), enabled depth testing for bullet debug
 draw

---
 src/core/entity.cc | 131 +++++++++++++++++++++++++----------------------------
 src/core/entity.h  |  37 +++++++--------
 2 files changed, 78 insertions(+), 90 deletions(-)

(limited to 'src/core')

diff --git a/src/core/entity.cc b/src/core/entity.cc
index 8a7976f..2f8b8a5 100644
--- a/src/core/entity.cc
+++ b/src/core/entity.cc
@@ -144,7 +144,10 @@ Entity::Entity() :
 	entity_moduletypeid = 0;
 
 	entity_model = 0;
+
+	entity_body = 0;
 	entity_body = 0;
+	entity_motionstate = 0;
 	entity_collision_shape = 0;
 	
 	entity_speed = 0.0f;
@@ -180,7 +183,10 @@ Entity::Entity(std::istream & is)
 	entity_visible = true;
 
 	entity_model = 0;
+
 	entity_body = 0;
+	entity_body = 0;
+	entity_motionstate = 0;
 	entity_collision_shape = 0;
 	
 	entity_speed = 0.0f;
@@ -219,11 +225,19 @@ Entity::~Entity()
 	if (entity_zone)
 		entity_zone->remove(this);
 	
+	if (entity_motionstate)
+		delete entity_motionstate;
+	
 	if (entity_collision_shape)
 		delete entity_collision_shape;
 	
 	if (entity_body)
 		delete entity_body;
+	
+	if (entity_body_info)
+		delete entity_body_info;
+	
+	
 }
 
 void Entity::die()
@@ -244,34 +258,6 @@ void Entity::clear_updates()
 	}
 }
 
-// reset physics state
-void Entity::reset()
-{
-	if (!entity_body) {
-		if (entity_collision_shape ) {
-			delete entity_collision_shape ;
-			entity_collision_shape  = 0;
-		}
-		
-		if (model()) {
-			entity_collision_shape  = new btBoxShape(to_btVector3(model()->maxbbox()));
-		} else {
-			entity_collision_shape  = new btSphereShape(radius());
-		}
-
-		btVector3 inertia(0, 0, 0);
-		entity_body = new btRigidBody(0.0f, 0, entity_collision_shape, inertia);
-	}
-
-	btTransform t;
-	t.setIdentity();
-	t.setOrigin(to_btVector3(location()));
-	t.setBasis(to_btMatrix3x3(axis()));
-	
-	entity_body->setWorldTransform(t);
-	set_dirty();
-}
-
 void Entity::set_info(Info *info)
 {
 	entity_info = info;
@@ -467,10 +453,6 @@ void Entity::receive_server_update(std::istream &is)
 {
 }
 
-void Entity::dock(core::Entity *entity)
-{
-}
-
 void Entity::frame(float seconds)
 {
 }
@@ -498,27 +480,63 @@ void Entity::remove_menu(std::string const &label)
 	}
 }
 
+void Entity::reset()
+{
+	// location and orientation
+	btTransform t;
+	t.setIdentity();
+	t.setOrigin(to_btVector3(location()));
+	t.setBasis(to_btMatrix3x3(axis()));
+	
+	// construct physics body if necessary
+	if (!entity_body) {
+		// create collision shape
+		if (model()) {
+			entity_collision_shape  = new btBoxShape(to_btVector3(model()->maxbbox()));
+		} else {
+			entity_collision_shape  = new btSphereShape(radius());
+		}
+		btVector3 inertia(0, 0, 0);
+		if (entity_mass)
+			entity_collision_shape->calculateLocalInertia(entity_mass, inertia);
+		
+		// create motion state
+		entity_motionstate = new btDefaultMotionState(t);
+		
+		// create physics body
+		entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, entity_motionstate, entity_collision_shape, inertia);
+		
+		entity_body = new btRigidBody(*entity_body_info);
+		//entity_body->setActivationState(ISLAND_SLEEPING);
+		
+		if (zone())
+			zone()->physics()->addRigidBody(entity_body);		
+	}
+
+	// transfer entity location to motion state
+	entity_body->setWorldTransform(t);
+	if (zone())
+		zone()->physics()->synchronizeSingleMotionState(entity_body);
+
+	set_dirty();
+}
+
 /* ---- class EntityDynamic ---------------------------------------- */
 
 EntityDynamic::EntityDynamic() : Entity()
 {
 	entity_state = Normal;
 	entity_timer = 0;
-	
-	entity_motionstate = new btDefaultMotionState();
 }
 
 EntityDynamic::EntityDynamic(std::istream & is) : Entity(is)
 {
 	entity_state = Normal;
 	entity_timer = 0;
-	entity_motionstate = 0;
 }
 
 EntityDynamic::~EntityDynamic()
 {
-	if (entity_motionstate)
-		delete entity_motionstate;
 }
 
 void EntityDynamic::set_state(int state)
@@ -529,39 +547,15 @@ void EntityDynamic::set_state(int state)
 	}
 }
 
-// reset physics state
 void EntityDynamic::reset()
 {
-	assert(entity_motionstate);
-		
-	// construct physics body if necessary
-	if (!entity_body) {
-		if (entity_collision_shape ) {
-			delete entity_collision_shape ;
-			entity_collision_shape  = 0;
-		}
-		
-		if (model()) {
-			entity_collision_shape  = new btBoxShape(to_btVector3(model()->maxbbox()));
-		} else {
-			entity_collision_shape  = new btSphereShape(radius());
-		}
-		
-		btVector3 inertia(0, 0, 0);
-		entity_collision_shape->calculateLocalInertia(entity_mass, inertia);
-		entity_body = new btRigidBody(entity_mass, entity_motionstate, entity_collision_shape, inertia);
+	Entity::reset();
+	
+	if ((entity_state == Docked) || (entity_state == Destroyed) || (!visible())){
+		entity_body->activate(false);
+	} else {
+		entity_body->activate(true);
 	}
-
-	// transfer entity location to motion state
-	btTransform t;
-	t.setIdentity();
-	t.setOrigin(to_btVector3(location()));
-	t.setBasis(to_btMatrix3x3(axis()));
-	entity_body->setWorldTransform(t);
-	if (zone())
-		zone()->physics()->synchronizeSingleMotionState(entity_body);
-
-	set_dirty();
 }
 
 void EntityDynamic::frame(float seconds)
@@ -571,8 +565,7 @@ void EntityDynamic::frame(float seconds)
 
 	// transfer bullet state to entity state
 	// FIXME disable physics when docked
-	if (entity_body && entity_motionstate) {
-		
+	if (entity_body) {
 		// this makes sure an update is sent if speed goes to 0 in the next step
 		if (entity_speed > 0) {
 			set_dirty();
diff --git a/src/core/entity.h b/src/core/entity.h
index 2719040..e51dac3 100644
--- a/src/core/entity.h
+++ b/src/core/entity.h
@@ -166,11 +166,16 @@ public:
 		return entity_body;
 	}
 	
-	/// collision shape
+	/// physics collision shape
 	inline btCollisionShape *collision_shape() {
 		return entity_collision_shape;
 	}
 	
+	/// physics motion info
+	inline btMotionState *motionstate() {
+		return entity_motionstate;
+	}
+		
 	/// indicates a server-side entity
 	inline const bool serverside() const {
 		return entity_serverside;
@@ -277,10 +282,6 @@ public:
 	
 	/* ---- actors ---------------------------------------------------- */
 
-	/// called when the entity received a docking request
-	// FIXME move docking functions to game
-	virtual void dock(Entity *entity);
-
 	/// set flags
 	inline void set_flag(Flags flag) {
 		entity_flags |= flag;
@@ -419,8 +420,10 @@ public:
 	float			entity_servertimestamp;
 
 protected:
-	btRigidBody		*entity_body;
-	btCollisionShape	*entity_collision_shape;
+	btRigidBody					*entity_body;
+	btRigidBody::btRigidBodyConstructionInfo	*entity_body_info;
+	btCollisionShape				*entity_collision_shape;
+	btMotionState					*entity_motionstate;
 
 private:
 	unsigned int		entity_id;
@@ -489,11 +492,7 @@ public:
 		return entity_timer;
 	}
 
-	inline btMotionState *motionstate() {
-		return entity_motionstate;
-	}
-	
-	/*----- mutators -------------------------------------------------- */
+/*----- mutators -------------------------------------------------- */
 
 	/// mass of the entity
 	inline void set_mass(const float mass) {
@@ -505,11 +504,6 @@ public:
 		entity_speed = speed;
 	}
 
-	/**
-	 * @brief reset the physics state
-	 */
-	virtual void reset();
-	
 	/*----- serializers ----------------------------------------------- */
 
 	/// serialize the entity to a stream
@@ -542,13 +536,14 @@ public:
 	 */
 	virtual void frame(float seconds);
 
+	/**
+	 * @brief reset the physics state
+	 */
+	virtual void reset();
+
 protected:
 	float			entity_timer;
 	int			entity_state;
-	
-	btMotionState		*entity_motionstate;
-	
-
 };
 
 /// an entity that can be controlled by a player
-- 
cgit v1.2.3