SparrowEngine/src/scene/gibgeneratornode.cpp
2018-01-30 17:44:34 +01:00

53 lines
1.6 KiB
C++

#include "gibgeneratornode.h"
#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
#include "scenetree.h"
#include "scenenode.h"
void GibGeneratorNode::createGib(SceneNode* graphicalPart,
btCollisionShape *physicsShape,
float mass,
const glm::vec3 &pos,
const glm::vec3 &velocity,
unsigned int lifeSpan)
{
if(getScene() != nullptr)
{
graphicalPart->moveTo(pos);
// Create the rigid body object
btMotionState *motionState = graphicalPart->getMotionState();
btVector3 localInertia;
physicsShape->calculateLocalInertia(mass, localInertia);
btRigidBody *body = new btRigidBody(mass, motionState, physicsShape, localInertia);
body->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
body->setUserPointer(graphicalPart);
getScene()->getPhysics()->addRigidBody(body);
m_gibs.push_back(new Gib(body, graphicalPart, getEngine().getTime()+lifeSpan));
addChild(graphicalPart);
}
}
void GibGeneratorNode::update()
{
// TODO : optimisation possible : use some kind of heap stack structure instead of vector
for(auto it = m_gibs.begin(); it != m_gibs.end();)
{
Gib *g = *it;
if(g->expiration < getEngine().getTime())
{
getScene()->getPhysics()->removeCollisionObject(g->body);
g->graphics->destroyWhenOrphan();
removeChild(g->graphics);
it = m_gibs.erase(it);
delete g;
}
else
++it;
}
SceneNode::update();
}