Skip to content

Commit

Permalink
feat: #102 Add random emitter & point zone
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Sep 1, 2020
1 parent 238a9a7 commit c4ae303
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ This file is part of the "Skylicht Engine".

#include "Renderers/IRenderer.h"
#include "Zones/CZone.h"
#include "Emitters/CEmitter.h"

#include "Emitters/CRandomEmitter.h"
#include "Zones/CPoint.h"

namespace Skylicht
{
Expand All @@ -45,8 +49,42 @@ namespace Skylicht
for (CZone *z : m_zones)
delete z;

for (CEmitter *e : m_emitters)
delete e;

m_renderers.clear();
m_zones.clear();
m_emitters.clear();
}

CEmitter* CFactory::createEmitter(EEmitter type)
{
CEmitter *e = NULL;

switch (type)
{
case EEmitter::Random:
e = new CRandomEmitter();
break;
default:
e = NULL;
break;
}

if (e != NULL)
m_emitters.push_back(e);

return e;
}

void CFactory::deleteEmitter(CEmitter *e)
{
std::vector<CEmitter*>::iterator i = std::find(m_emitters.begin(), m_emitters.end(), e);
if (i != m_emitters.end())
{
m_emitters.erase(i);
delete e;
}
}

IRenderer* CFactory::createRenderer(ERenderer type)
Expand All @@ -66,7 +104,22 @@ namespace Skylicht

CZone* CFactory::createZone(EZone type)
{
return NULL;
CZone *z = NULL;

switch (type)
{
case EZone::Point:
z = new CPoint();
break;
default:
z = NULL;
break;
}

if (z != NULL)
m_zones.push_back(z);

return z;
}

void CFactory::deleteZone(CZone *z)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Skylicht
{
namespace Particle
{
class CEmitter;
class CZone;
class IRenderer;

Expand All @@ -42,6 +43,11 @@ namespace Skylicht
Cylinder
};

enum EEmitter
{
Random,
};

enum ERenderer
{
Quad,
Expand All @@ -53,12 +59,17 @@ namespace Skylicht
protected:
std::vector<IRenderer*> m_renderers;
std::vector<CZone*> m_zones;
std::vector<CEmitter*> m_emitters;

public:
CFactory();

virtual ~CFactory();

CEmitter* createEmitter(EEmitter type = Random);

void deleteEmitter(CEmitter *e);

IRenderer* createRenderer(ERenderer type = Quad);

void deleteRenderer(IRenderer* r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ namespace Skylicht
return (u32)nbBorn;
}

void CEmitter::generateVelocity(CParticle& particle)
void CEmitter::generateVelocity(CParticle& particle, CZone* zone)
{
float force = m_forceMin + (m_forceMax - m_forceMin) * os::Randomizer::frand();
generateVelocity(particle, force / particle.Mass);
generateVelocity(particle, force / particle.Mass, zone);
}

void CEmitter::emitParticle(CParticle &particle, CZone* zone)
{
zone->generatePosition(particle, m_emitFullZone);
generateVelocity(particle);
generateVelocity(particle, zone);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ namespace Skylicht

virtual u32 updateNumber(float deltaTime);

void generateVelocity(CParticle& particle);
void generateVelocity(CParticle& particle, CZone* zone);

void emitParticle(CParticle& particle, CZone* zone);

virtual void generateVelocity(CParticle& particle, float speed) const = 0;
virtual void generateVelocity(CParticle& particle, float speed, CZone* zone) = 0;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "pch.h"
#include "CRandomEmitter.h"
#include "ParticleSystem/Particles/CParticle.h"
#include "ParticleSystem/Particles/Zones/CZone.h"

namespace Skylicht
{
namespace Particle
{

CRandomEmitter::CRandomEmitter()
{

}

CRandomEmitter::~CRandomEmitter()
{

}

void CRandomEmitter::generateVelocity(CParticle& particle, float speed, CZone* zone)
{
float norm;

do
{
particle.Velocity.set(random(-1.0f, 1.0f),
random(-1.0f, 1.0f),
random(-1.0f, 1.0f));

norm = particle.Velocity.getLength();
} while (norm > 1.0f || norm == 0.0f);

particle.Velocity *= speed / norm;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "CEmitter.h"

namespace Skylicht
{
namespace Particle
{
class CRandomEmitter : public CEmitter
{
public:
CRandomEmitter();

virtual ~CRandomEmitter();

virtual void generateVelocity(CParticle& particle, float speed, CZone* zone);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CPoint.h"
#include "ParticleSystem/Particles/CParticle.h"

namespace Skylicht
{
namespace Particle
{
CPoint::CPoint()
{

}

CPoint::~CPoint()
{

}

void CPoint::generatePosition(CParticle& particle, bool full)
{
particle.Position = getTransformPosition(m_position);
}

core::vector3df CPoint::computeNormal(const core::vector3df& point)
{
core::vector3df tpos = getTransformPosition(m_position);
core::vector3df v = point - tpos;
getTransformPosition(v);
return v;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "CZone.h"

namespace Skylicht
{
namespace Particle
{
class CPoint : public CZone
{
public:
CPoint();

virtual ~CPoint();

virtual void generatePosition(CParticle& particle, bool full);

virtual core::vector3df computeNormal(const core::vector3df& point);
};
}
}
Loading

0 comments on commit c4ae303

Please sign in to comment.