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"))
{
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;
{
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);
}
{
}
-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() + ")");
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;
};
//Jump
- if (Events::getJump() && yFloorDist<=0)
+ if (Events::getJump() && abs(yFloorDist)<.5)
ySpeed += 5.5;
int move_x = Events::getMoveX();
#include <btBulletDynamicsCommon.h>
+
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),
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));
}
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;
{
(*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<btCollisionObject*>(contactManifold->getBody0());
btCollisionObject* obB = const_cast<btCollisionObject*>(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);
}
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);
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);
}
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;
};
//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) :
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();
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);
-
}
}
-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
return bt_collision_object;
}
+SceneObject::operator btRigidBody *() const
+{
+ return bt_rigid_body;
+}
+
+
short SceneObject::getCollideGroup() const
{
return collide_group;
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;
bool isEnemy();
operator btCollisionObject*() const;
+ operator btRigidBody*() const;
short getCollideGroup() const;
short getCollideWith() const;
//Bullet
btCollisionObject* bt_collision_object;
+ btRigidBody* bt_rigid_body;
};