Browse Source

Working on the physics character

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
c26f75b083
4 changed files with 693 additions and 225 deletions
  1. 608 208
      build/debug/Makefile
  2. 41 0
      src/Physics/PhyCharacter.cpp
  3. 42 17
      src/Physics/PhyCharacter.h
  4. 2 0
      src/Physics/Physics.h

File diff suppressed because it is too large
+ 608 - 208
build/debug/Makefile


+ 41 - 0
src/Physics/PhyCharacter.cpp

@@ -0,0 +1,41 @@
+#include <btBulletCollisionCommon.h>
+#include <btBulletDynamicsCommon.h>
+#include <BulletCollision/CollisionDispatch/btGhostObject.h>
+#include <BulletDynamics/Character/btKinematicCharacterController.h>
+#include "PhyCharacter.h"
+#include "Physics.h"
+
+
+//======================================================================================================================
+// Contructor                                                                                                          =
+//======================================================================================================================
+PhyCharacter::PhyCharacter(Physics& physics, const Initializer& init)
+{
+	ghostObject = new btPairCachingGhostObject();
+
+	btAxisSweep3* sweepBp = dynamic_cast<btAxisSweep3*>(physics.broadphase);
+	DEBUG_ERR(sweepBp == NULL);
+
+	ghostPairCallback = new btGhostPairCallback();
+	sweepBp->getOverlappingPairCache()->setInternalGhostPairCallback(ghostPairCallback);
+	//collisionShape = new btCapsuleShape(init.characterWidth, init.characterHeight);
+	convexShape = new btCylinderShape(btVector3(init.characterWidth, init.characterHeight, init.characterWidth));
+
+	ghostObject->setCollisionShape(convexShape);
+	//ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
+	btTransform trf;
+	trf.setIdentity();
+	trf.setOrigin(btVector3(0.0, 40.0, 0.0));
+	ghostObject->setWorldTransform(trf);
+
+	character = new btKinematicCharacterController(ghostObject, convexShape, init.stepHeight);
+
+	character->setJumpSpeed(init.jumpSpeed);
+	character->setMaxJumpHeight(init.maxJumpHeight);
+
+	// register
+	physics.dynamicsWorld->addCollisionObject(ghostObject, btBroadphaseProxy::CharacterFilter,
+																						btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
+
+	physics.dynamicsWorld->addAction(character);
+}

+ 42 - 17
src/Physics/PhyCharacter.h

@@ -1,32 +1,57 @@
-#ifndef _PHYCHARACTER_H_
-#define _PHYCHARACTER_H_
+#ifndef PHYCHARACTER_H
+#define PHYCHARACTER_H
 
 #include "Common.h"
-#include "PhyCommon.h"
+#include "Physics.h"
+
+
+class Physics;
+class btPairCachingGhostObject;
+class btConvexShape;
+class btKinematicCharacterController;
+class btGhostPairCallback;
 
 
 /**
- * @todo write docs
+ * Its basically a wrapper around bullet character
  */
 class PhyCharacter
 {
 	public:
-		btPairCachingGhostObject* ghostObject;
-		btCapsuleShape* capsule;
-		btKinematicCharacterController* character;
-
-		PhyCharacter(Physics* world, float charHeight, float charWidth, float stepHeight, float maxJumpHeight)
+		/**
+		 * Initializer class
+		 */
+		struct Initializer
 		{
-			ghostObject = new btPairCachingGhostObject();
-			world->broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
-			capsule = new btCapsuleShape(charWidth, charHeight);
-			ghostObject->setCollisionShape(capsule);
-			ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
-			character = new btKinematicCharacterController(ghostObject, capsule, stepHeight);
-			character->setMaxJumpHeight(maxJumpHeight);
-		}
+			float characterHeight;
+			float characterWidth;
+			float stepHeight;
+			float jumpSpeed;
+			float maxJumpHeight;
 
+			Initializer();
+		};
 
+		PhyCharacter(Physics& physics, const Initializer& init);
+		~PhyCharacter();
+		void rotate(float angle);
+		void moveForward(float distance);
+		void jump();
+
+	private:
+		btPairCachingGhostObject* ghostObject;
+		btConvexShape* convexShape;
+		btKinematicCharacterController* character;
+		btGhostPairCallback* ghostPairCallback;
 };
 
+
+inline PhyCharacter::Initializer::Initializer():
+	characterHeight(2.0),
+	characterWidth(0.75),
+	stepHeight(1.0),
+	jumpSpeed(10.0),
+	maxJumpHeight(.0)
+{}
+
 #endif

+ 2 - 0
src/Physics/Physics.h

@@ -12,6 +12,8 @@
  */
 class Physics
 {
+	friend class PhyCharacter;
+
 	public:
 		/**
 		 * Collision groups

Some files were not shown because too many files changed in this diff