Skip to content

Commit

Permalink
Merge branch 'explosion'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotolegokot committed Oct 19, 2016
2 parents e6f132d + 53c746c commit f9414ac
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ logfile
*.blend1
*.swp
build/*
CMakeLists.txt.user
4 changes: 4 additions & 0 deletions PlaneRunner.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Add option='-isystem&quot;/usr/include/bullet&quot;' />
<Add option='-isystem&quot;deps/include/bullet&quot;' />
<Add option="-DDEBUG_OUTPUT=false" />
<Add option="-DFAR_CAMERA_DISTANCE=false" />
<Add directory="include" />
<Add directory="include/obstacles" />
</Compiler>
Expand Down Expand Up @@ -78,6 +79,7 @@
<Add option='-isystem&quot;deps\include\irrlicht&quot;' />
<Add option='-isystem&quot;deps\include\bullet&quot;' />
<Add option="-DDEBUG_OUTPUT=false" />
<Add option="-DFAR_CAMERA_DISTANCE=false" />
<Add directory="include" />
<Add directory="include/obstacles" />
</Compiler>
Expand Down Expand Up @@ -106,6 +108,7 @@
<Unit filename="include/Config.h" />
<Unit filename="include/DebugDrawer.h" />
<Unit filename="include/EventReceiver.h" />
<Unit filename="include/Explosion.h" />
<Unit filename="include/GUI.h" />
<Unit filename="include/Game.h" />
<Unit filename="include/IBody.h" />
Expand All @@ -130,6 +133,7 @@
<Unit filename="src/Config.cpp" />
<Unit filename="src/DebugDrawer.cpp" />
<Unit filename="src/EventReceiver.cpp" />
<Unit filename="src/Explosion.cpp" />
<Unit filename="src/GUI.cpp" />
<Unit filename="src/Game.cpp" />
<Unit filename="src/MotionState.cpp" />
Expand Down
37 changes: 37 additions & 0 deletions include/Explosion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef EXPLOSION_H
#define EXPLOSION_H

#include <memory>
#include <algorithm>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h>

using namespace irr;

class Explosion
{
public:
Explosion(btDynamicsWorld &world, IrrlichtDevice &device,
const btVector3 &position = btVector3(0, 0, 0), btScalar radius = 1000);
~Explosion();

void setPosition(const btVector3 &position);
btVector3 getPosition() const;

void explode();

private:
btDynamicsWorld &world;
IrrlichtDevice &device;
btScalar radius = 0.0f;
std::unique_ptr<btGhostObject> explosionObject;
std::unique_ptr<scene::IParticleSystemSceneNode> particleSystem;


void startAnimation();
};

#endif // EXPLOSION_H
4 changes: 4 additions & 0 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <iostream>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h>
#include <CGUITTFont.h>
#include <ITimer.h>
Expand All @@ -32,6 +34,7 @@
#include "PlaneControl.h"
#include "DebugDrawer.h"
#include "options.h"
#include "Explosion.h"

using namespace irr;

Expand Down Expand Up @@ -77,6 +80,7 @@ class Game
ObstacleGenerator *obstacleGenerator = nullptr;
Plane *plane = nullptr;
PlaneControl *planeControl = nullptr;
Explosion *explosion = nullptr;

void error(const core::stringw &str) const;
bool initializeDevice();
Expand Down
3 changes: 2 additions & 1 deletion include/IBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IBody : public IObstacle

scene::ISceneNode &getNode()
{
return *((MotionState *) rigidBody->getMotionState())->getNode();
return ((MotionState *) rigidBody->getMotionState())->getNode();
}

btVector3 getPosition() const override
Expand Down Expand Up @@ -87,6 +87,7 @@ class IBody : public IObstacle
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState.get(), shape.get(), inertia);

rigidBody = std::make_unique<btRigidBody>(rigidBodyCI);
rigidBody->setUserIndex(0); // default index for bodies
world.addRigidBody(rigidBody.get());
}

Expand Down
4 changes: 2 additions & 2 deletions include/IObstaclePattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef IOBSTACLEPATTERN_H
#define IOBSTACLEPATTERN_H

#include <deque>
#include <list>
#include <memory>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
Expand All @@ -33,7 +33,7 @@ class IObstaclePattern
world(world), device(device), position(position) {}
virtual ~IObstaclePattern() {}

virtual void addObstaclesToDeque(std::deque<std::unique_ptr<IObstacle>> &deque) = 0;
virtual void addObstaclesToList(std::list<std::unique_ptr<IObstacle>> &list) = 0;
virtual size_t getObstacleCount() const = 0;

protected:
Expand Down
7 changes: 4 additions & 3 deletions include/MotionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef MOTIONSTATE_H
#define MOTIONSTATE_H

#include <memory>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h>
Expand All @@ -34,14 +35,14 @@ class MotionState : public btMotionState
{
protected:
btTransform transform;
scene::ISceneNode *node = nullptr;
std::unique_ptr<scene::ISceneNode> node;
static btVector3 quatToEuler(const btQuaternion &quat);

public:
MotionState(const btTransform &startTransform = btTransform::getIdentity(), scene::ISceneNode *node = nullptr);
virtual ~MotionState();
void setNode(scene::ISceneNode *node);
scene::ISceneNode *getNode() const;
void setNode(std::unique_ptr<scene::ISceneNode> node);
scene::ISceneNode &getNode();
void setPosition(const core::vector3df &position);
core::vector3df getPosition() const;
virtual void getWorldTransform(btTransform &worldTrans) const;
Expand Down
4 changes: 2 additions & 2 deletions include/ObstacleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef OBSTACLEGENERATOR_H
#define OBSTACLEGENERATOR_H

#include <deque>
#include <list>
#include <memory>
#include <functional>
#include <irrlicht.h>
Expand Down Expand Up @@ -62,7 +62,7 @@ class ObstacleGenerator
btScalar farValueWithBuffer() const;

IrrlichtDevice &device;
std::deque<std::unique_ptr<IObstacle>> obstacles;
std::list<std::unique_ptr<IObstacle>> obstacles;

u32 obstacleCount = 0;

Expand Down
12 changes: 12 additions & 0 deletions include/Plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
#include "ObjMesh.h"

#define PLANE_MODEL "media/models/plane.obj"

#if FAR_CAMERA_DISTANCE
#define CAMERA_DISTANCE 600
#else
#define CAMERA_DISTANCE 200
#endif // FAR_CAMERA_DISTANCE

#define PLANE_MASS 1

// this class defines plane
Expand All @@ -36,6 +42,9 @@ class Plane : public IBody
public:
Plane(btDynamicsWorld &world, IrrlichtDevice &device, const btVector3 &position);

void setExploded(bool exploded);
bool getExploded() const;

// some convenient
btVector3 getLinearVelocity() const;
void setLinearVelocity(const btVector3 &linearVelocity);
Expand Down Expand Up @@ -63,6 +72,9 @@ class Plane : public IBody
virtual void createMotionState(std::unique_ptr<scene::ISceneNode> node) override;
virtual void createShape() override;
virtual btScalar getMass() override;

private:
bool exploded = false;
};

#endif // PLANE_H
4 changes: 4 additions & 0 deletions include/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@
#ifndef DEBUG_OUTPUT
#define DEBUG_OUTPUT true
#endif // DEBUG_OUTPUT

#ifndef FAR_CAMERA_DISTANCE
#define FAR_CAMERA_DISTANCE false
#endif // FAR_CAMERA_DISTANCE
6 changes: 3 additions & 3 deletions include/patterns/Crystal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class Crystal : public IObstaclePattern
cone2->getRigidBody().setCenterOfMassTransform(transform);
}

void addObstaclesToDeque(std::deque<std::unique_ptr<IObstacle>> &deque) override
void addObstaclesToList(std::list<std::unique_ptr<IObstacle>> &list) override
{
deque.push_back(std::move(cone1));
deque.push_back(std::move(cone2));
list.push_back(std::move(cone1));
list.push_back(std::move(cone2));
}

size_t getObstacleCount() const override
Expand Down
4 changes: 2 additions & 2 deletions include/patterns/Tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class Tunnel : public IObstaclePattern
boxes[3] = std::make_unique<Box>(world, device, position + btVector3(0, -radius, 0), btVector3(radius, radius / 10, length / 2));
}

void addObstaclesToDeque(std::deque<std::unique_ptr<IObstacle>> &deque) override
void addObstaclesToList(std::list<std::unique_ptr<IObstacle>> &list) override
{
for (int i = 0; i < 4; i++)
deque.push_back(std::move(boxes[i]));
list.push_back(std::move(boxes[i]));
}

size_t getObstacleCount() const override
Expand Down
2 changes: 0 additions & 2 deletions src/EventReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ bool EventReceiver::OnEvent(const SEvent &event)
tabPressed = true;
if (pressedKeys[KEY_UP])
upPressed = true;
if (pressedKeys[KEY_LEFT])
leftPressed = true;
if (pressedKeys[KEY_DOWN])
downPressed = true;
return escapePressed;
Expand Down
90 changes: 90 additions & 0 deletions src/Explosion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Explosion.h"

using namespace irr;

Explosion::Explosion(btDynamicsWorld &world, IrrlichtDevice &device,
const btVector3 &position, btScalar radius) :
world(world), device(device), radius(radius)
{
explosionObject = std::make_unique<btGhostObject>();
explosionObject->setCollisionShape(new btSphereShape(radius));
explosionObject->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), position));
explosionObject->setCollisionFlags(explosionObject->getCollisionFlags() | btCollisionObject::CollisionFlags::CF_NO_CONTACT_RESPONSE);

world.addCollisionObject(explosionObject.get());
world.getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback);

/*particleSystem =
std::unique_ptr<scene::IParticleSystemSceneNode>(device.getSceneManager()->addParticleSystemSceneNode(false));
auto particleAffector =
std::unique_ptr<scene::IParticleAffector>(particleSystem->createFadeOutParticleAffector());
particleSystem->addAffector(particleAffector.get());
particleAffector.release()->drop();
particleAffector =
std::unique_ptr<scene::IParticleAffector>(particleSystem->createScaleParticleAffector(core::dimension2df(5.f, 5.f)));
particleSystem->addAffector(particleAffector.get());
particleAffector.release()->drop();*/
}

Explosion::~Explosion()
{
world.removeCollisionObject(explosionObject.get());
particleSystem.release()->remove();
}

void Explosion::setPosition(const btVector3 &position)
{
explosionObject->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), position));
}

btVector3 Explosion::getPosition() const
{
return explosionObject->getWorldTransform().getOrigin();
}

void Explosion::explode()
{
#if DEBUG_OUTPUT
std::cout << "PLANE EXPLODED!!" << std::endl;
#endif // DEBUG_OUTPUT

//startAnimation();

for (int i = 0; i < explosionObject->getNumOverlappingObjects(); i++)
{
btRigidBody &body = *dynamic_cast<btRigidBody *>(explosionObject->getOverlappingObject(i));
if (body.getUserIndex() == 1) continue; // if plane, skip it

btVector3 force = body.getCenterOfMassPosition() - this->getPosition();
btScalar distance = force.length();
force.safeNormalize();
force *= (radius - distance * 0.1f) * 100.0f;
body.activate();
body.applyForce(force, btVector3(0, 0, 0));
}
}

void Explosion::startAnimation()
{
/*auto emitter =
std::unique_ptr<scene::IParticleEmitter>(particleSystem->createPointEmitter(
core::vector3df(0.f, 0.01f, 0.f), // direction
80, 100, // emit rate
video::SColor(0, 255, 255, 255), // min color
video::SColor(0, 255, 255, 255), // max color
2000, 4000, 0, // min and max age, angle
core::dimension2df(10.f, 10.f), // min size
core::dimension2df(20.f, 20.f))); // max size
particleSystem->setEmitter(emitter.get());
emitter.release()->drop();
const auto &position = getPosition();
particleSystem->setPosition(core::vector3df(position.x(), position.y(), position.z()));
particleSystem->setScale(core::vector3df(20, 20, 20));
particleSystem->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
particleSystem->setMaterialTexture(0, device.getVideoDriver()->getTexture("media/textures/lsd.png"));
particleSystem->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);*/
}
Loading

0 comments on commit f9414ac

Please sign in to comment.