From 8173ce0fa88d45e5156e248a0e76f53d457f689a Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Mon, 22 Jun 2015 13:48:28 +0200 Subject: [PATCH] heights working --- Weave/Scene/EventBox.cpp | 2 +- Weave/Scene/EventBox.h | 2 +- Weave/Scene/Level.cpp | 42 ++++++++++++++++++++++-------- Weave/Scene/Level.h | 2 +- Weave/Scene/Marvin.cpp | 2 +- Weave/Scene/Scene.cpp | 51 ++++++++++++++++++++++--------------- Weave/Scene/Scene.h | 4 ++- Weave/Scene/SceneObject.cpp | 35 ++++++++++++++++++++++--- Weave/Scene/SceneObject.h | 4 ++- 9 files changed, 105 insertions(+), 39 deletions(-) diff --git a/Weave/Scene/EventBox.cpp b/Weave/Scene/EventBox.cpp index ceeed9c..196072b 100644 --- a/Weave/Scene/EventBox.cpp +++ b/Weave/Scene/EventBox.cpp @@ -32,7 +32,7 @@ void EventBox::draw() const model->drawBBox(modelMat,vec4(.3f,.9f,.9f,1.f)); } -void EventBox::collides(SceneObject* _other) +void EventBox::collides(SceneObject* _other, btPersistentManifold* _contactManifold, float _deltaT) { if ((texture != nullptr && texture->operator std::string() == "model_player_2D.png") || _other->getTexture() != nullptr && (_other->getTexture()->operator std::string() == "model_player_2D.png")) { diff --git a/Weave/Scene/EventBox.h b/Weave/Scene/EventBox.h index dd8fc49..68d0ead 100644 --- a/Weave/Scene/EventBox.h +++ b/Weave/Scene/EventBox.h @@ -18,7 +18,7 @@ public: virtual void update(float); virtual void draw() const; - virtual void collides(SceneObject* other); + virtual void collides(SceneObject* other, btPersistentManifold* contactManifold, float deltaT = 1.f) override; protected: uint eb_type; diff --git a/Weave/Scene/Level.cpp b/Weave/Scene/Level.cpp index e58d2a0..401ad73 100644 --- a/Weave/Scene/Level.cpp +++ b/Weave/Scene/Level.cpp @@ -11,6 +11,16 @@ Level::Level(Shader* _shader, std::string _modelpath, std::string _texturepath) { collide_group = COL_LEVEL; collide_with = COL_MARVIN | COL_ENEMY; + + + btTransform btModelMat; + btModelMat.setFromOpenGLMatrix(value_ptr(modelMat)); + + btDefaultMotionState* MotionState = + new btDefaultMotionState(btModelMat); + btRigidBody::btRigidBodyConstructionInfo RigidBodyCI(0, MotionState, *model, btVector3(0, 0, 0)); + + bt_rigid_body = new btRigidBody(RigidBodyCI); } @@ -18,33 +28,45 @@ Level::~Level() { } -void Level::collides(SceneObject* _other, btPersistentManifold* _contactManifold) +void Level::collides(SceneObject* _other, btPersistentManifold* _contactManifold, float _deltaT) { int numContacts = _contactManifold->getNumContacts(); //For each contact point in that manifold float pdist = _contactManifold->getContactPoint(0).getDistance(); + vec3 normal; for (int j = 0; j < numContacts; j++) { //Get the contact information btManifoldPoint& pt = _contactManifold->getContactPoint(j); btVector3 ptA = pt.getPositionWorldOnA(); btVector3 ptB = pt.getPositionWorldOnB(); + vec3 ptAg(ptA.getX(), ptA.getY(), ptA.getZ()); vec3 ptBg(ptB.getX(), ptB.getY(), ptB.getZ()); - vec3 normal = normalize(ptAg - ptBg); - pdist = pt.getDistance(); + //normal = normalize(ptAg - ptBg); + //pdist = pt.getDistance(); - //btVector3 ptN = pt.m_normalWorldOnB; - //vec3 normal = vec3(ptN.getX(), ptN.getY(), ptN.getZ()); + btVector3 ptN = pt.m_normalWorldOnB; + normal = vec3(ptN.getX(), ptN.getY(), ptN.getZ()); _other->yFloorDist = pdist * normal[1]; - if (normal[1] <= .6f && pdist<0) - _other->move(-pdist * vec3(normal[0], 0.f, normal[2])); - } + //Height + if (_other->yFloorDist < 0 && _other->ySpeed < 0) + { + _other->move(-pdist * vec3(0.f, normal[1], 0.f)); + } + + if (normal[1] >= .6f && _other->ySpeed < 0) + { + _other->ySpeed = 0; + } + - if (_other->yFloorDist <= 0 && _other->ySpeed<0) - _other->ySpeed = 0; + //if (normal[1] <= .6f && pdist<0 && pdist > -1) + // _other->move(-pdist * vec3(normal[0], 0.f, normal[2])); + + } //Message::info((std::string)"Kollision! " + std::to_string(_other->yFloorDist) + " (LVL with " + (std::string)*_other->getModel() + ")"); diff --git a/Weave/Scene/Level.h b/Weave/Scene/Level.h index 8caae54..ffe7690 100644 --- a/Weave/Scene/Level.h +++ b/Weave/Scene/Level.h @@ -12,6 +12,6 @@ public: Level(Shader* shader,std::string modelpath, std::string texturepath); ~Level(); - virtual void collides(SceneObject* other, btPersistentManifold* contactManifold) override; + virtual void collides(SceneObject* other, btPersistentManifold* contactManifold, float deltaT=1.f) override; }; diff --git a/Weave/Scene/Marvin.cpp b/Weave/Scene/Marvin.cpp index ed1f5b6..75f4e8c 100644 --- a/Weave/Scene/Marvin.cpp +++ b/Weave/Scene/Marvin.cpp @@ -33,7 +33,7 @@ void Marvin::update(float deltaT) //Jump - if (Events::getJump() && yFloorDist<=0) + if (Events::getJump() && abs(yFloorDist)<.5) ySpeed += 5.5; int move_x = Events::getMoveX(); diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index 9a34902..b88d247 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -18,16 +18,17 @@ #include + using std::list; using std::set; using std::stack; #define timestep 0.01f -#define SCENE_SIZE 500.0 -#define MAX_OBJECTS 16000 - +//#define SCENE_SIZE 500.0 +//#define MAX_OBJECTS 16000 +#define YFALL_SPEED 9.8f Scene::Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : viewPort(x, y, width, height, fovy, zNear, zFar), @@ -40,13 +41,21 @@ lookat(_lookat) bt_collision_configuration = new btDefaultCollisionConfiguration(); bt_dispatcher = new btCollisionDispatcher(bt_collision_configuration); - btScalar sscene_size = (btScalar)SCENE_SIZE; - btVector3 worldAabbMin(-sscene_size, -sscene_size, -sscene_size); - btVector3 worldAabbMax(sscene_size, sscene_size, sscene_size); + //btScalar sscene_size = (btScalar)SCENE_SIZE; + //btVector3 worldAabbMin(-sscene_size, -sscene_size, -sscene_size); + //btVector3 worldAabbMax(sscene_size, sscene_size, sscene_size); + //bt_broadphase = new bt32BitAxisSweep3(worldAabbMin, worldAabbMax, MAX_OBJECTS, 0, true); + + bt_broadphase = new btDbvtBroadphase(); + + bt_solver = new btSequentialImpulseConstraintSolver; + + bt_dynamics_world = new btDiscreteDynamicsWorld(bt_dispatcher, bt_broadphase, bt_solver, bt_collision_configuration); - bt_broadphase = new bt32BitAxisSweep3(worldAabbMin, worldAabbMax, MAX_OBJECTS, 0, true); + //bt_collision_world = new btCollisionWorld(bt_dispatcher, bt_broadphase, bt_collision_configuration); - bt_collision_world = new btCollisionWorld(bt_dispatcher, bt_broadphase, bt_collision_configuration); + + bt_dynamics_world->setGravity(btVector3(0, YFALL_SPEED, 0)); } @@ -60,7 +69,9 @@ Scene::~Scene() timestamps.empty(); //Bullet - delete bt_collision_world; + //delete bt_collision_world; + delete bt_dynamics_world; + delete bt_solver; delete bt_broadphase; delete bt_dispatcher; delete bt_collision_configuration; @@ -97,21 +108,21 @@ void Scene::update(float deltaT) { (*i)->update(deltaT); } + bt_dynamics_world->stepSimulation(deltaT, 2); + //bt_collision_world->performDiscreteCollisionDetection(); - bt_collision_world->performDiscreteCollisionDetection(); - - int numManifolds = bt_collision_world->getDispatcher()->getNumManifolds(); + int numManifolds = bt_dynamics_world->getDispatcher()->getNumManifolds(); //For each contact manifold for (int i = 0; i < numManifolds; i++) { - btPersistentManifold* contactManifold = bt_collision_world->getDispatcher()->getManifoldByIndexInternal(i); + btPersistentManifold* contactManifold = bt_dynamics_world->getDispatcher()->getManifoldByIndexInternal(i); btCollisionObject* obA = const_cast(contactManifold->getBody0()); btCollisionObject* obB = const_cast(contactManifold->getBody1()); contactManifold->refreshContactPoints(obA->getWorldTransform(), obB->getWorldTransform()); if (contactManifold->getNumContacts()>0) { - ((SceneObject*)obA->getUserPointer())->collides((SceneObject*)obB->getUserPointer(), contactManifold); - ((SceneObject*)obB->getUserPointer())->collides((SceneObject*)obA->getUserPointer(), contactManifold); + ((SceneObject*)obA->getUserPointer())->collides((SceneObject*)obB->getUserPointer(), contactManifold, deltaT); + ((SceneObject*)obB->getUserPointer())->collides((SceneObject*)obA->getUserPointer(), contactManifold, deltaT); } @@ -140,10 +151,10 @@ void Scene::update(float deltaT) lookat->ySpeed = prev_ySpeed; - for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i) - { - //(*i)->update(-max(0.0f, currenttime - deltaT)); - } + //for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i) + //{ + // //(*i)->update(-max(0.0f, currenttime - deltaT)); + //} currenttime = max(0.0f, currenttime - deltaT); @@ -182,7 +193,7 @@ void Scene::addObject(SceneObject* _add) ShaderSet.insert(_add->getShader()); //bt_collision_world->addCollisionObject(*_add); - bt_collision_world->addCollisionObject(*_add,_add->getCollideGroup(),_add->getCollideWith()); + bt_dynamics_world->addCollisionObject(*_add,_add->getCollideGroup(),_add->getCollideWith()); _add->setMainScene(this); } diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index 3a5904e..e6495f9 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -58,5 +58,7 @@ protected: btCollisionConfiguration* bt_collision_configuration; btCollisionDispatcher* bt_dispatcher; btBroadphaseInterface* bt_broadphase; - btCollisionWorld* bt_collision_world; + btSequentialImpulseConstraintSolver* bt_solver; + //btCollisionWorld* bt_collision_world; + btDiscreteDynamicsWorld* bt_dynamics_world; }; diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 3ff9454..e6a32fa 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -66,8 +66,23 @@ bt_collision_object(new btCollisionObject()) //Graphix::getGlError(); //Message::info("Done"); + btTransform btModelMat; + btModelMat.setFromOpenGLMatrix(value_ptr(modelMat)); + + btDefaultMotionState* MotionState = + new btDefaultMotionState(btModelMat); + btScalar mass = 1; + btVector3 fallInertia(0, 0, 0); + model->operator btCollisionShape *()->calculateLocalInertia(mass, fallInertia); + btRigidBody::btRigidBodyConstructionInfo RigidBodyCI(mass, MotionState, *model, fallInertia); + + + bt_rigid_body = new btRigidBody(RigidBodyCI); + bt_collision_object->setCollisionShape(*model); bt_collision_object->setUserPointer(this); + +// bt_rigid_body } SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, Model* _model, string texturepath) : @@ -84,7 +99,8 @@ ignore(false), newModel(false), collide_group(0), collide_with(0), -bt_collision_object(new btCollisionObject()) +bt_collision_object(new btCollisionObject()), +bt_rigid_body(nullptr) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -114,11 +130,18 @@ bt_collision_object(new btCollisionObject()) SceneObject::~SceneObject() { + + if (bt_rigid_body != nullptr) + { + delete bt_rigid_body->getMotionState(); + delete bt_rigid_body; + } + + if (texture!=nullptr) Texture::deleteTexture(*texture); if (newModel) Model::deleteIMetaModel(*model); - } @@ -167,7 +190,7 @@ void SceneObject::draw() const } -void SceneObject::collides(SceneObject* _other, btPersistentManifold* _contactManifold) +void SceneObject::collides(SceneObject* _other, btPersistentManifold* _contactManifold, float _deltaT) { int numContacts = _contactManifold->getNumContacts(); //For each contact point in that manifold @@ -269,6 +292,12 @@ SceneObject::operator btCollisionObject *() const return bt_collision_object; } +SceneObject::operator btRigidBody *() const +{ + return bt_rigid_body; +} + + short SceneObject::getCollideGroup() const { return collide_group; diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 8642b1a..ddf6476 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -36,7 +36,7 @@ public: virtual void update(float); virtual void draw() const; - virtual void collides(SceneObject* other, btPersistentManifold* contactManifold); + virtual void collides(SceneObject* other, btPersistentManifold* contactManifold, float deltaT=1.f); virtual vec3 getPosition() const; @@ -67,6 +67,7 @@ public: bool isEnemy(); operator btCollisionObject*() const; + operator btRigidBody*() const; short getCollideGroup() const; short getCollideWith() const; @@ -94,6 +95,7 @@ protected: //Bullet btCollisionObject* bt_collision_object; + btRigidBody* bt_rigid_body; }; -- 2.47.3