each scene has its own physics simulation

This commit is contained in:
Lendemor 2017-08-31 21:35:52 +02:00
parent c70453ff65
commit e5a9ac9da0
8 changed files with 48 additions and 28 deletions

View File

@ -19,7 +19,7 @@
Engine::Engine() :
m_window(nullptr),
m_input(nullptr),
m_world(nullptr),
// m_world(nullptr),
m_physicsDebugNode(nullptr),
m_togglePhysicsDebugAction(NO_ACTION),
m_toggleShellAction(NO_ACTION),
@ -44,8 +44,8 @@ Engine::~Engine()
delete m_window;
delete m_input;
}
if(m_world != NULL)
delete m_world;
// if(m_world != NULL)
// delete m_world;
}
void Engine::createWindow(std::string title,
@ -70,15 +70,15 @@ void Engine::createWindow(std::string title,
m_loadingThread = LoadingThread::init();
}
void Engine::initPhysics()
/*void Engine::initPhysics()
{
btDefaultCollisionConfiguration *collisionConfiguration = new btDefaultCollisionConfiguration();
btBroadphaseInterface *broadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver();
m_world = new btDiscreteDynamicsWorld(dispatcher, broadPhase, solver, collisionConfiguration);
m_world->setGravity(btVector3(0, -9.81f, 0));
}
getScene()->setPhysics(new btDiscreteDynamicsWorld(dispatcher, broadPhase, solver, collisionConfiguration));
getScene()->getPhysics()->setGravity(btVector3(0, -9.81f, 0));
}*/
void Engine::update()
{
@ -118,13 +118,13 @@ void Engine::update()
}
// update Physics
if(m_world != nullptr)
if(getScene()->getPhysics() != nullptr)
{
m_world->stepSimulation(1000.f*(float)getDeltaTime());
getScene()->getPhysics()->stepSimulation(1000.f*(float)getDeltaTime());
if(m_physicsDebugNode != nullptr)
{
m_physicsDebugNode->clearBuffers();
m_world->debugDrawWorld();
getScene()->getPhysics()->debugDrawWorld();
getScene()->registerMeshType(m_physicsDebugNode->getFlags());
}
}
@ -190,20 +190,20 @@ void Engine::setScene(std::string scene)
void Engine::enablePhysicsDebug()
{
if(m_world != nullptr && m_physicsDebugNode == nullptr)
if(getScene()->getPhysics() != nullptr && m_physicsDebugNode == nullptr)
{
m_physicsDebugNode = new PhysicsDebugNode();
getScene()->addToIndex(m_physicsDebugNode);
m_world->setDebugDrawer(m_physicsDebugNode);
m_world->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
getScene()->getPhysics()->setDebugDrawer(m_physicsDebugNode);
getScene()->getPhysics()->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
}
}
void Engine::disablePhysicsDebug()
{
if(m_world != nullptr && m_physicsDebugNode != nullptr)
if(getScene()->getPhysics() != nullptr && m_physicsDebugNode != nullptr)
{
m_world->setDebugDrawer(nullptr);
getScene()->getPhysics()->setDebugDrawer(nullptr);
getScene()->removeFromIndex(m_physicsDebugNode);
delete m_physicsDebugNode;
m_physicsDebugNode = nullptr;

View File

@ -50,7 +50,7 @@ public:
Input* getInput() const {return m_input;}
sf::Window* getWindow() const {return m_window;}
SparrowRenderer* getRenderer() const {return m_renderer;}
btDiscreteDynamicsWorld* getPhysics() const {return m_world;}
// btDiscreteDynamicsWorld* getPhysics() const {return m_world;}
SparrowShell* getShell() const {return m_sparrowshell;}
PhysicsDebugNode* getPhysicsDebug() const {return m_physicsDebugNode;}
Editor* getEditor() const {return m_editor;}
@ -77,7 +77,7 @@ private:
// SceneTree* m_scene;
std::string m_current_scene;
SparrowShell* m_sparrowshell;
btDiscreteDynamicsWorld* m_world;
// btDiscreteDynamicsWorld* m_world;
PhysicsDebugNode *m_physicsDebugNode;
SparrowRenderer* m_renderer;
Editor* m_editor;

View File

@ -22,7 +22,7 @@ void GibGeneratorNode::createGib(GraphicalNode* graphicalPart,
btRigidBody *body = new btRigidBody(mass, motionState, physicsShape, localInertia);
body->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
getEngine().getPhysics()->addRigidBody(body);
getEngine().getScene()->getPhysics()->addRigidBody(body);
m_gibs.push_back(new Gib(body, graphicalPart, getEngine().getTime()+lifeSpan));
addChild(graphicalPart);
@ -37,7 +37,7 @@ void GibGeneratorNode::update()
Gib *g = *it;
if(g->expiration < getEngine().getTime())
{
getEngine().getPhysics()->removeCollisionObject(g->body);
getEngine().getScene()->getPhysics()->removeCollisionObject(g->body);
removeChild(g->graphics);
it = m_gibs.erase(it);
delete g->graphics;

View File

@ -221,8 +221,8 @@ void PlayerCharacterNode::update()
end_back.setY(end_back.y() - (LEGS_HEIGHT));
btCollisionWorld::ClosestRayResultCallback RayCallback_back(start_back, end_back);
getEngine().getPhysics()->rayTest(start_front, end_front, RayCallback_front);
getEngine().getPhysics()->rayTest(start_back, end_back, RayCallback_back);
getEngine().getScene()->getPhysics()->rayTest(start_front, end_front, RayCallback_front);
getEngine().getScene()->getPhysics()->rayTest(start_back, end_back, RayCallback_back);
PhysicsDebugNode* pdnode= getEngine().getPhysicsDebug();
if (pdnode != nullptr){

View File

@ -10,6 +10,8 @@
#include "scene/scenenode.h"
#include "scene/cameranode.h"
#include <btBulletDynamicsCommon.h>
SceneTree::SceneTree(const Engine &engine) :
Scene(),
m_engine(engine),
@ -24,7 +26,8 @@ SceneTree::SceneTree(const Engine &engine) :
SceneTree::~SceneTree()
{
if(m_world != NULL)
delete m_world;
}
void SceneTree::setMainCamera(CameraNode *camNode)
@ -62,6 +65,17 @@ void SceneTree::update()
m_root.update();
}
void SceneTree::initPhysics()
{
btDefaultCollisionConfiguration *collisionConfiguration = new btDefaultCollisionConfiguration();
btBroadphaseInterface *broadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver();
m_world = new btDiscreteDynamicsWorld(dispatcher, broadPhase, solver, collisionConfiguration);
m_world->setGravity(btVector3(0, -9.81f, 0));
}
void SceneTree::registerMeshType(unsigned int meshType)
{
auto ret = m_meshTypes.emplace(meshType);

View File

@ -33,6 +33,8 @@ public:
Camera* getCamera() const { return m_camera; }
ContainerNode* getRootObject(){return &m_root;}
btDiscreteDynamicsWorld* getPhysics() const {return m_world;}
void initPhysics();
void registerMeshType(unsigned int meshType);
void registerLightType(unsigned int lightType);
void addToIndex(SceneNode* node);
@ -49,6 +51,7 @@ private:
const Engine &m_engine;
ContainerNode m_root;
btDiscreteDynamicsWorld* m_world;
std::vector<Light*> m_lights;
std::vector<GeometryNode*> m_geometries;
std::unordered_set<unsigned int> m_meshTypes;

View File

@ -216,10 +216,12 @@ public:
scene->getRootObject()->addChild(ambientLight);
scene->initPhysics();
if(m_config->scene == "sponza")
{
sun->initShadowMap(4096);
generateSponza(scene, m_engine->getPhysics());
generateSponza(scene, m_engine->getScene()->getPhysics());
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f))));
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f))));
m_player->setPosition(0.f, 2.f, 0.f);
@ -228,7 +230,7 @@ public:
else if(m_config->scene == "terrain")
{
sun->initShadowMap(4096);
generateTerrain(scene, m_engine->getPhysics());
generateTerrain(scene, m_engine->getScene()->getPhysics());
m_player->setPosition(0.f, 15.f, 0.f);
sun->setShadowView(glm::vec3(130, 130, 70));
}
@ -243,7 +245,7 @@ public:
m->initGL();
MeshNode *node = new MeshNode(m);
sandboxContainer->addChild(node);
m_engine->getPhysics()->addRigidBody(node->buildStaticCollider());
scene->getPhysics()->addRigidBody(node->buildStaticCollider());
}
m_player->setPosition(0.f, 1.4f, 0.f);
sun->setShadowView(glm::vec3(80));
@ -268,7 +270,7 @@ public:
m_engine->setScene(m_demo->getScene());
m_engine->getInput()->setCurrentContext("default");
m_engine->getInput()->setMouseGrabbed(true);
m_engine->getPhysics()->addRigidBody(m_demo->getPlayer()->getRigidbody());
m_engine->getScene()->getPhysics()->addRigidBody(m_demo->getPlayer()->getRigidbody());
m_engine->toggleMouseVisibility();
}
};
@ -290,6 +292,7 @@ public:
glm::vec2 pos = glm::vec2(size.x,size.y)/glm::vec2(2,2) - m_button_demo->getDimension()/glm::vec2(2,2);
m_button_demo->setPosition(pos);
m_button_demo->setVisible(true);
scene->initPhysics();
}
void setLeftClickAction(int action){
@ -357,7 +360,7 @@ int main(){
// engine.toggleMouseVisibility();
// setting up SparrowEngine
engine.initPhysics();
// engine.initPhysics();
// SceneTree *scene = engine.createScene();
// engine.setScene(scene);

View File

@ -80,7 +80,7 @@ void ScenePicker::pick()
PhysicsDebugNode* pdnode= getEngine().getPhysicsDebug();
if (pdnode != nullptr)
pdnode->drawLine(start,end,btVector3(1,0,1));
getEngine().getPhysics()->rayTest(start,end,RayCallback);
getEngine().getScene()->getPhysics()->rayTest(start,end,RayCallback);
m_pickSucceeded = RayCallback.hasHit();
if(m_pickSucceeded)