1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#include "IrrCompileConfig.h"
6
#include "CParticleSphereEmitter.h"
8
#include "IAttributes.h"
16
CParticleSphereEmitter::CParticleSphereEmitter(
17
const core::vector3df& center, f32 radius,
18
const core::vector3df& direction, u32 minParticlesPerSecond,
19
u32 maxParticlesPerSecond, const video::SColor& minStartColor,
20
const video::SColor& maxStartColor, u32 lifeTimeMin, u32 lifeTimeMax,
22
const core::dimension2df& minStartSize,
23
const core::dimension2df& maxStartSize )
24
: Center(center), Radius(radius), Direction(direction),
25
MinStartSize(minStartSize), MaxStartSize(maxStartSize),
26
MinParticlesPerSecond(minParticlesPerSecond),
27
MaxParticlesPerSecond(maxParticlesPerSecond),
28
MinStartColor(minStartColor), MaxStartColor(maxStartColor),
29
MinLifeTime(lifeTimeMin), MaxLifeTime(lifeTimeMax),
30
Time(0), Emitted(0), MaxAngleDegrees(maxAngleDegrees)
33
setDebugName("CParticleSphereEmitter");
39
//! Prepares an array with new particles to emitt into the system
40
//! and returns how much new particles there are.
41
s32 CParticleSphereEmitter::emitt(u32 now, u32 timeSinceLastCall, SParticle*& outArray)
43
Time += timeSinceLastCall;
45
const u32 pps = (MaxParticlesPerSecond - MinParticlesPerSecond);
46
const f32 perSecond = pps ? ((f32)MinParticlesPerSecond + os::Randomizer::frand() * pps) : MinParticlesPerSecond;
47
const f32 everyWhatMillisecond = 1000.0f / perSecond;
49
if(Time > everyWhatMillisecond)
51
Particles.set_used(0);
52
u32 amount = (u32)((Time / everyWhatMillisecond) + 0.5f);
56
if(amount > MaxParticlesPerSecond*2)
57
amount = MaxParticlesPerSecond * 2;
59
for(u32 i=0; i<amount; ++i)
61
// Random distance from center
62
const f32 distance = os::Randomizer::frand() * Radius;
64
// Random direction from center
65
p.pos.set(Center + distance);
66
p.pos.rotateXYBy(os::Randomizer::frand() * 360.f, Center );
67
p.pos.rotateYZBy(os::Randomizer::frand() * 360.f, Center );
68
p.pos.rotateXZBy(os::Randomizer::frand() * 360.f, Center );
75
core::vector3df tgt = Direction;
76
tgt.rotateXYBy(os::Randomizer::frand() * MaxAngleDegrees);
77
tgt.rotateYZBy(os::Randomizer::frand() * MaxAngleDegrees);
78
tgt.rotateXZBy(os::Randomizer::frand() * MaxAngleDegrees);
82
p.endTime = now + MinLifeTime;
83
if (MaxLifeTime != MinLifeTime)
84
p.endTime += os::Randomizer::rand() % (MaxLifeTime - MinLifeTime);
86
if (MinStartColor==MaxStartColor)
87
p.color=MinStartColor;
89
p.color = MinStartColor.getInterpolated(MaxStartColor, os::Randomizer::frand());
91
p.startColor = p.color;
92
p.startVector = p.vector;
94
if (MinStartSize==MaxStartSize)
95
p.startSize = MinStartSize;
97
p.startSize = MinStartSize.getInterpolated(MaxStartSize, os::Randomizer::frand());
100
Particles.push_back(p);
103
outArray = Particles.pointer();
105
return Particles.size();
111
//! Writes attributes of the object.
112
void CParticleSphereEmitter::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
114
out->addVector3d("Center", Direction);
115
out->addFloat("Radius", Radius);
117
out->addVector3d("Direction", Direction);
118
out->addFloat("MinStartSizeWidth", MinStartSize.Width);
119
out->addFloat("MinStartSizeHeight", MinStartSize.Height);
120
out->addFloat("MaxStartSizeWidth", MaxStartSize.Width);
121
out->addFloat("MaxStartSizeHeight", MaxStartSize.Height);
122
out->addInt("MinParticlesPerSecond", MinParticlesPerSecond);
123
out->addInt("MaxParticlesPerSecond", MaxParticlesPerSecond);
124
out->addColor("MinStartColor", MinStartColor);
125
out->addColor("MaxStartColor", MaxStartColor);
126
out->addInt("MinLifeTime", MinLifeTime);
127
out->addInt("MaxLifeTime", MaxLifeTime);
128
out->addInt("MaxAngleDegrees", MaxAngleDegrees);
131
//! Reads attributes of the object.
132
void CParticleSphereEmitter::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
134
Center = in->getAttributeAsVector3d("Center");
135
Radius = in->getAttributeAsFloat("Radius");
137
Direction = in->getAttributeAsVector3d("Direction");
138
if (Direction.getLength() == 0)
139
Direction.set(0,0.01f,0);
142
idx = in->findAttribute("MinStartSizeWidth");
144
MinStartSize.Width = in->getAttributeAsFloat(idx);
145
idx = in->findAttribute("MinStartSizeHeight");
147
MinStartSize.Height = in->getAttributeAsFloat(idx);
148
idx = in->findAttribute("MaxStartSizeWidth");
150
MaxStartSize.Width = in->getAttributeAsFloat(idx);
151
idx = in->findAttribute("MaxStartSizeHeight");
153
MaxStartSize.Height = in->getAttributeAsFloat(idx);
154
MinParticlesPerSecond = in->getAttributeAsInt("MinParticlesPerSecond");
155
MaxParticlesPerSecond = in->getAttributeAsInt("MaxParticlesPerSecond");
157
MinParticlesPerSecond = core::max_(1u, MinParticlesPerSecond);
158
MaxParticlesPerSecond = core::max_(MaxParticlesPerSecond, 1u);
159
MaxParticlesPerSecond = core::min_(MaxParticlesPerSecond, 200u);
160
MinParticlesPerSecond = core::min_(MinParticlesPerSecond, MaxParticlesPerSecond);
162
MinStartColor = in->getAttributeAsColor("MinStartColor");
163
MaxStartColor = in->getAttributeAsColor("MaxStartColor");
164
MinLifeTime = in->getAttributeAsInt("MinLifeTime");
165
MaxLifeTime = in->getAttributeAsInt("MaxLifeTime");
166
MinLifeTime = core::max_(0u, MinLifeTime);
167
MaxLifeTime = core::max_(MaxLifeTime, MinLifeTime);
168
MinLifeTime = core::min_(MinLifeTime, MaxLifeTime);
170
MaxAngleDegrees = in->getAttributeAsInt("MaxAngleDegrees");
173
} // end namespace scene
174
} // end namespace irr