53 lines
1.7 KiB
C++
53 lines
1.7 KiB
C++
#include "gibgeneratornode.h"
|
|
#include <btBulletCollisionCommon.h>
|
|
#include <btBulletDynamicsCommon.h>
|
|
#include "scenetree.h"
|
|
#include "graphicalnode.h"
|
|
|
|
void GibGeneratorNode::createGib(GraphicalNode* graphicalPart,
|
|
btCollisionShape *physicsShape,
|
|
float mass,
|
|
const glm::vec3 &pos,
|
|
const glm::vec3 &velocity,
|
|
unsigned int lifeSpan)
|
|
{
|
|
if(m_scene != 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));
|
|
|
|
getEngine().getPhysics()->addRigidBody(body);
|
|
|
|
m_gibs.push_back(new Gib(body, graphicalPart, getEngine().getTime()+lifeSpan));
|
|
addChild(graphicalPart);
|
|
m_scene->updateShaders(); // TODO : optimisations needed
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
removeChild(g->graphics);
|
|
delete g->graphics;
|
|
it = m_gibs.erase(it);
|
|
getEngine().getPhysics()->removeCollisionObject(g->body);
|
|
delete g;
|
|
}
|
|
else
|
|
++it;
|
|
}
|
|
for(SceneNode* child : m_children)
|
|
child->update();
|
|
}
|