Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #381 from Broken-Gem-Studio/FyX_CharacterChontrollers
Browse files Browse the repository at this point in the history
fixed particle crash when deleting the GO
  • Loading branch information
AitorSimona authored May 27, 2020
2 parents 8ce9545 + dd1748c commit bcd6927
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
31 changes: 17 additions & 14 deletions Broken Engine/Source/ComponentCharacterController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ComponentCharacterController::~ComponentCharacterController()

void ComponentCharacterController::Enable()
{
if (hasBeenDeactivated)
if (controller == nullptr)
{
controller = App->physics->mControllerManager->createController(*desc);

Expand All @@ -89,20 +89,24 @@ void ComponentCharacterController::Enable()

void ComponentCharacterController::Disable()
{
if (controller) {
controller->getActor()->getShapes(&shape, 1);
if (shape->getActor() != nullptr)
if (controller != nullptr) {
if (shape)
{
//GetActor()->setActorFlag(physx::PxActorFlag::eDISABLE_SIMULATION, true);
if (!hasBeenDeactivated)
if (shape->getActor() != nullptr)
{
desc->position = controller->getFootPosition();
App->physics->actors.erase(shape->getActor());
controller->release();
controller = nullptr;
hasBeenDeactivated = true;
//GetActor()->setActorFlag(physx::PxActorFlag::eDISABLE_SIMULATION, true);
if (!hasBeenDeactivated )
{
desc->position = controller->getFootPosition();
if(App->physics->actors.size() > 0)
App->physics->actors.erase(shape->getActor());
}
}
}
shape = nullptr;
controller->release();
controller = nullptr;
hasBeenDeactivated = true;
}
active = false;
}
Expand Down Expand Up @@ -278,9 +282,8 @@ void ComponentCharacterController::Delete()
{
if (controller)
{
physx::PxShape* shape = nullptr;
controller->getActor()->getShapes(&shape, 1);
App->physics->actors.erase(shape->getActor());
if(shape && App->physics->actors.size()>0)
App->physics->actors.erase(shape->getActor());
controller->release();
controller = nullptr;
}
Expand Down
5 changes: 4 additions & 1 deletion Broken Engine/Source/ComponentParticleEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ ComponentParticleEmitter::~ComponentParticleEmitter()
App->particles->DeleteEmitter(this);

for (int i = 0; i < maxParticles; ++i) {

App->particles->particlesToDraw.erase(particles[i]);
delete particles[i];
particles[i] = nullptr;
}

if (particleSystem && App->physics->mScene) {
Expand Down Expand Up @@ -313,7 +316,7 @@ void ComponentParticleEmitter::SortParticles()
float distance = App->renderer3D->active_camera->frustum.NearPlane().Distance(particles[i]->position);

drawingIndices[1.0f / distance] = i;
App->particles->particlesToDraw[1.0f / distance] = particles[i];
App->particles->particlesToDraw[particles[i]] = 1.0f / distance;
}
}

Expand Down
52 changes: 28 additions & 24 deletions Broken Engine/Source/ModuleParticles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,46 @@ bool ModuleParticles::CleanUp()

void ModuleParticles::DrawParticles(bool shadowsPass)
{

// -- Frustum culling --
Plane cameraPlanes[6];
App->renderer3D->culling_camera->frustum.GetPlanes(cameraPlanes);

std::map<float, Particle*>::iterator it = particlesToDraw.begin();
std::map<Particle*, float>::iterator it = particlesToDraw.begin();
while (it != particlesToDraw.end())
{
//Check if the particles are inside the frustum of the camera
bool draw = true;
for (int i = 0; i < 6; ++i)
if ((*it).first->emitter != nullptr)
{
//If the particles is on the positive side of one ore more planes, it's outside the frustum
bool shadowsHandle = ((shadowsPass && (*it).second->emitter->m_CastShadows == false) || (!shadowsPass && (*it).second->emitter->m_OnlyShadows));
if (cameraPlanes[i].IsOnPositiveSide((*it).second->position) || shadowsHandle)
//Check if the particles are inside the frustum of the camera
bool draw = true;
for (int i = 0; i < 6; ++i)
{
draw = false;
break;
//If the particles is on the positive side of one ore more planes, it's outside the frustum
bool shadowsHandle = ((shadowsPass && (*it).first->emitter->m_CastShadows == false) || (!shadowsPass && (*it).first->emitter->m_OnlyShadows));
if (cameraPlanes[i].IsOnPositiveSide((*it).first->position) || shadowsHandle)
{
draw = false;
break;
}
}
}

if (draw)
{
if (!shadowsPass && (*it).second->emitter)
if (draw)
{
(*it).second->emitter->SetEmitterBlending();
if ((*it).second->emitter->particlesFaceCulling)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
if (!shadowsPass && (*it).first->emitter)
{
(*it).first->emitter->SetEmitterBlending();
if ((*it).first->emitter->particlesFaceCulling)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
}

(*it).first->Draw(shadowsPass);

if (!shadowsPass)
if ((*it).first->emitter->particlesFaceCulling == false)
glEnable(GL_CULL_FACE);
}

(*it).second->Draw(shadowsPass);

if (!shadowsPass)
if ((*it).second->emitter->particlesFaceCulling == false)
glEnable(GL_CULL_FACE);
}
it++;
}
Expand Down
2 changes: 1 addition & 1 deletion Broken Engine/Source/ModuleParticles.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BROKEN_API ModuleParticles : public Module

public:

std::map<float, Particle*> particlesToDraw;
std::map<Particle*, float> particlesToDraw;

std::vector<ComponentParticleEmitter*> particleEmitters;
};
Expand Down

0 comments on commit bcd6927

Please sign in to comment.