diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-10-18 21:34:55 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-10-18 21:34:55 -0400 |
| commit | 33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904 (patch) | |
| tree | 18d24b74e48d8b7e9cb656272df7a48b28d3b6c8 /src/shared/phys | |
| parent | 76b4fddee6106b60dbc6da6d7bcef61b42a3c310 (diff) | |
| download | brokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.tar.gz brokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.tar.bz2 brokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.zip | |
Started refactoring
* Finished a basic server
* Changed from ZMQ to nanomsg (basically the same though)
* Moved some repeated code from the client/server directories into
"shared"
* Edited makefile to reflect new dependencies
Diffstat (limited to 'src/shared/phys')
| -rw-r--r-- | src/shared/phys/physcommon.cpp | 84 | ||||
| -rw-r--r-- | src/shared/phys/physcommon.hpp | 4 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp new file mode 100644 index 0000000..c15274d --- /dev/null +++ b/src/shared/phys/physcommon.cpp @@ -0,0 +1,84 @@ +#include <chrono> +#include <list> +#include <btBulletDynamicsCommon.h> + +#include "physcommon.hpp" + +using namespace std::chrono; + +btDiscreteDynamicsWorld* World; +std::list<btRigidBody*> Objects; + +// Removes all objects from the world +void ClearObjects(btDiscreteDynamicsWorld* wr, std::list<btRigidBody*> objs, void(*f)(btRigidBody*)) { + + for(std::list<btRigidBody *>::iterator Iterator = objs.begin(); Iterator != objs.end(); ++Iterator) { + btRigidBody *Object = *Iterator; + + if(f){ + (*f)(Object); + } + + // Remove the object from the world + wr->removeRigidBody(Object); + delete Object; + } + objs.clear(); +} + +btBroadphaseInterface* BroadPhase; +btDefaultCollisionConfiguration* CollisionConfiguration; +btCollisionDispatcher* Dispatcher; +btSequentialImpulseConstraintSolver* Solver; + +void phys_genesis(){ + BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000)); + printf("Broadphase\n"); + CollisionConfiguration = new btDefaultCollisionConfiguration(); + printf("Collision config\n"); + Dispatcher = new btCollisionDispatcher(CollisionConfiguration); + printf("Dispatcher\n"); + Solver = new btSequentialImpulseConstraintSolver(); + printf("Solver\n"); + World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration); + printf("Physics world init ok.\n"); +} + +void phys_shutdown(void(*f)(btRigidBody*)){ + ClearObjects(World,Objects,f); + printf("cleared objects\n"); + delete BroadPhase; + printf("deleted broadphase\n"); + delete CollisionConfiguration; + printf("deleted collision config\n"); + delete Dispatcher; + printf("Deleted dispatcher\n"); + delete Solver; + printf("deleted solver\n"); + + delete World; //Muah ha ha + printf("deleted world\n"); + +} + + +// Runs the physics simulation. +// - TDeltaTime tells the simulation how much time has passed since the last frame so the simulation can run independently of the frame rate. Optionally pass in an argument that will be called on every rigidbody in the world +void UpdatePhysics(double TDeltaTime, void(*f)(btRigidBody*)) { + World->stepSimulation(TDeltaTime * 0.02f, 60); + if(f){ + // Relay the object's orientation to irrlicht + for(std::list<btRigidBody *>::iterator it = Objects.begin(); it != Objects.end(); ++it) { + (*f)(*it); + } + } +} + +high_resolution_clock::time_point t1 = high_resolution_clock::now(); +void gameloop_phys(void(*f)(btRigidBody*)){ + high_resolution_clock::time_point now = high_resolution_clock::now(); + duration<double> delta = now-t1; + double steps = delta.count() * 100; + UpdatePhysics(steps,f); + t1 = now; +} diff --git a/src/shared/phys/physcommon.hpp b/src/shared/phys/physcommon.hpp new file mode 100644 index 0000000..e4660ab --- /dev/null +++ b/src/shared/phys/physcommon.hpp @@ -0,0 +1,4 @@ + +void gameloop_phys(void(*f)(btRigidBody*)); +void phys_genesis(); +void phys_shutdown(void(*f)(btRigidBody*)); |
