Skip to content

Commit

Permalink
feat: #102 add Cylinder, line, ring zone
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Sep 7, 2020
1 parent 51171a2 commit 8793b08
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ namespace Skylicht
return z;
}

CCylinder* CFactory::createCylinderZone(const core::vector3df& pos, const core::vector3df& direction, float radius, float length)
{
CCylinder *z = new CCylinder(pos, direction, radius, length);
m_zones.push_back(z);
return z;
}

CLine* CFactory::createLineZone(const core::vector3df& p1, const core::vector3df& p2)
{
CLine *z = new CLine(p1, p2);
m_zones.push_back(z);
return z;
}

CRing* CFactory::createRingZone(const core::vector3df& pos, const core::vector3df& normal, float minRadius, float maxRadius)
{
CRing *z = new CRing(pos, normal, minRadius, maxRadius);
m_zones.push_back(z);
return z;
}

void CFactory::deleteZone(CZone *z)
{
std::vector<CZone*>::iterator i = std::find(m_zones.begin(), m_zones.end(), z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ This file is part of the "Skylicht Engine".
#include "Zones/CPoint.h"
#include "Zones/CSphere.h"
#include "Zones/CAABox.h"
#include "Zones/CCylinder.h"
#include "Zones/CLine.h"
#include "Zones/CRing.h"

#include "Emitters/CRandomEmitter.h"
#include "Emitters/CStraightEmitter.h"
Expand Down Expand Up @@ -72,6 +75,12 @@ namespace Skylicht

CAABox* createAABoxZone(const core::vector3df& pos, const core::vector3df& dimension);

CCylinder* createCylinderZone(const core::vector3df& pos, const core::vector3df& direction, float radius, float length);

CLine* createLineZone(const core::vector3df& p1, const core::vector3df& p2);

CRing* createRingZone(const core::vector3df& pos, const core::vector3df& normal, float minRadius, float maxRadius);

void deleteZone(CZone *z);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace Skylicht
void CSphericEmitter::generateVelocity(CParticle& particle, float speed, CZone* zone)
{
float a = random(m_cosAngleMax, m_cosAngleMin);
float theta = std::acos(a);
float theta = acosf(a);
float phi = random(0.0f, 2.0f * core::PI);

float sinTheta = sinf(theta);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
!@
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 "CCylinder.h"
#include "ParticleSystem/Particles/CParticle.h"

namespace Skylicht
{
namespace Particle
{
CCylinder::CCylinder(const core::vector3df& position,
const core::vector3df& direction,
float radius,
float length) :
CZone(Cylinder),
m_position(position),
m_direction(direction),
m_radius(radius),
m_length(length)
{
m_direction.normalize();
}

CCylinder::~CCylinder()
{

}

void CCylinder::generatePosition(CParticle& particle, bool full)
{
float cLength = random(0.0f, m_length) - m_length * 0.5f;
float cAngle = random(0.0f, 2 * core::PI);
float cRadius = full ? random(0.0f, m_radius) : m_radius;

core::vector3df pos = getTransformPosition(m_position);
core::vector3df dir = getTransformVector(m_direction);

// We need at least two points to compute a base
core::vector3df rPoint = pos + core::vector3df(10.0f, 10.0f, 10.0f);
float dist = dir.dotProduct(rPoint);

while (dist == 0.0f || dir * dist + pos == rPoint)
{
// avoid dist == 0, which leads to a div by zero.
rPoint += core::vector3df(10.0f, 10.0f, random(-10.0f, 10.0f));
dist = dir.dotProduct(rPoint);
}

core::vector3df p1 = dir * dist + pos;
dist = p1.getDistanceFrom(rPoint);

core::vector3df a = (rPoint - p1) / dist;
core::vector3df tmp1 = dir, tmp2 = a; tmp2 = tmp2.crossProduct(tmp1*(-1));
core::vector3df b = tmp2;

particle.Position = pos + cLength * dir + a * cRadius * cosf(cAngle) + b * cRadius * sinf(cAngle);
}

core::vector3df CCylinder::computeNormal(const core::vector3df& point)
{
core::vector3df pos = getTransformPosition(m_position);
core::vector3df dir = getTransformVector(m_direction);

float dist = dir.dotProduct(point - pos);

if (dist >= m_length * 0.5f)
return dir;

if (dist <= -m_length * 0.5f)
return -dir;

core::vector3df ext = point - (dir*dist + pos);
ext.normalize();

return ext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
!@
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 CCylinder : public CZone
{
protected:
core::vector3df m_position;
core::vector3df m_direction;
float m_radius;
float m_length;

public:
CCylinder(const core::vector3df& position,
const core::vector3df& direction,
float radius,
float length);

virtual ~CCylinder();

inline void setPosition(const core::vector3df& pos)
{
m_position = pos;
}

inline void setDirection(const core::vector3df& direction)
{
m_direction = direction;
m_direction.normalize();
}

inline void setRadius(float r)
{
m_radius = r;
}

inline void setLength(float l)
{
m_length = l;
}

inline const core::vector3df& getPosition()
{
return m_position;
}

inline const core::vector3df& getDirection()
{
return m_direction;
}

inline float getRadius()
{
return m_radius;
}

inline float getLength()
{
return m_length;
}

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

virtual core::vector3df computeNormal(const core::vector3df& point);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
!@
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 "CLine.h"
#include "ParticleSystem/Particles/CParticle.h"

namespace Skylicht
{
namespace Particle
{
CLine::CLine(const core::vector3df& p1, const core::vector3df& p2) :
CZone(Line)
{
setLine(p1, p2);
}

CLine::~CLine()
{

}

void CLine::setLine(const core::vector3df& p1, const core::vector3df& p2)
{
m_p1 = p1;
m_p2 = p2;
}

void CLine::generatePosition(CParticle& particle, bool full)
{
core::vector3df pos = getTransformPosition(m_p1);
core::vector3df direction = getTransformPosition(m_p2) - pos;

float ratio = random(0.0f, 1.0f);
particle.Position = pos + direction * ratio;
}

core::vector3df CLine::computeNormal(const core::vector3df& point)
{
core::vector3df pos = getTransformPosition(m_p1);
core::vector3df direction = getTransformPosition(m_p2) - pos;

float d = -direction.dotProduct(point);
float sqrNorm = direction.getLengthSQ();
float t = 0.0f;
if (sqrNorm > 0.0f)
{
t = -(direction.dotProduct(pos) + d) / sqrNorm;

// t is clamped to the segment
if (t < 0.0f)
t = 0.0f;
else if (t > 1.0f)
t = 1.0f;
}

core::vector3df normal = point;
normal -= pos + t * direction;
normal.normalize();

return normal;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
!@
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 CLine : public CZone
{
protected:
core::vector3df m_p1;
core::vector3df m_p2;

public:
CLine(const core::vector3df& p1, const core::vector3df& p2);

virtual ~CLine();

void setLine(const core::vector3df& p1, const core::vector3df& p2);

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

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

0 comments on commit 8793b08

Please sign in to comment.