source: OGRE/trunk/ogrenew/PlugIns/ParticleFX/src/OgreDirectionRandomiserAffector.cpp @ 692

Revision 692, 6.5 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreDirectionRandomiserAffector.h"
26#include "OgreParticleSystem.h"
27#include "OgreParticle.h"
28#include "OgreStringConverter.h"
29
30
31namespace Ogre {
32
33    // Instantiate statics
34    DirectionRandomiserAffector::CmdRandomness DirectionRandomiserAffector::msRandomnessCmd;
35    DirectionRandomiserAffector::CmdScope DirectionRandomiserAffector::msScopeCmd;
36    DirectionRandomiserAffector::CmdKeepVelocity DirectionRandomiserAffector::msKeepVelocityCmd;
37
38    //-----------------------------------------------------------------------
39    DirectionRandomiserAffector::DirectionRandomiserAffector(ParticleSystem* psys)
40       : ParticleAffector(psys)
41    {
42        mType = "DirectionRandomiser";
43
44        // defaults
45        mRandomness = 1.0;
46        mScope = 1.0;
47        mKeepVelocity = false;
48
49        // Set up parameters
50        if (createParamDictionary("DirectionRandomiserAffector"))
51        {
52            addBaseParameters();
53            // Add extra paramaters
54            ParamDictionary* dict = getParamDictionary();
55            dict->addParameter(ParameterDef("randomness",
56                "The amount of randomness (chaos) to apply to the particle movement.",
57                PT_REAL), &msRandomnessCmd);
58            dict->addParameter(ParameterDef("scope",
59                "The percentage of particles which is affected.",
60                PT_REAL), &msScopeCmd);
61            dict->addParameter(ParameterDef("keep_velocity",
62                "Detemines whether the velocity of the particles is changed.",
63                PT_BOOL), &msKeepVelocityCmd);
64        }
65    }
66    //-----------------------------------------------------------------------
67    void DirectionRandomiserAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
68    {
69        ParticleIterator pi = pSystem->_getIterator();
70        Particle *p;
71        Real length;
72
73        while (!pi.end())
74        {
75            p = pi.getNext();
76            if (mScope > Math::UnitRandom())
77            {
78                if (!p->direction.isZeroLength())
79                {
80                    if (mKeepVelocity)
81                    {
82                        length = p->direction.length();
83                    }
84
85                    p->direction += Vector3(Math::RangeRandom(-mRandomness, mRandomness) * timeElapsed,
86                        Math::RangeRandom(-mRandomness, mRandomness) * timeElapsed,
87                        Math::RangeRandom(-mRandomness, mRandomness) * timeElapsed);
88
89                    if (mKeepVelocity)
90                    {
91                        p->direction *= length / p->direction.length();
92                    }
93                }
94            }
95        }
96    }
97    //-----------------------------------------------------------------------
98    void DirectionRandomiserAffector::setRandomness(Real force)
99    {
100        mRandomness = force;
101    }
102    //-----------------------------------------------------------------------
103    void DirectionRandomiserAffector::setScope(Real scope)
104    {
105        mScope = scope;
106    }
107    //-----------------------------------------------------------------------
108    void DirectionRandomiserAffector::setKeepVelocity(bool keepVelocity)
109    {
110        mKeepVelocity = keepVelocity;
111    }
112    //-----------------------------------------------------------------------
113    Real DirectionRandomiserAffector::getRandomness(void) const
114    {
115        return mRandomness;
116    }
117    //-----------------------------------------------------------------------
118    Real DirectionRandomiserAffector::getScope(void) const
119    {
120        return mScope;
121    }
122    //-----------------------------------------------------------------------
123    bool DirectionRandomiserAffector::getKeepVelocity(void) const
124    {
125        return mKeepVelocity;
126    }
127
128    //-----------------------------------------------------------------------
129    //-----------------------------------------------------------------------
130    // Command objects
131    //-----------------------------------------------------------------------
132    //-----------------------------------------------------------------------
133    String DirectionRandomiserAffector::CmdRandomness::doGet(const void* target) const
134    {
135        return StringConverter::toString(
136            static_cast<const DirectionRandomiserAffector*>(target)->getRandomness() );
137    }
138    void DirectionRandomiserAffector::CmdRandomness::doSet(void* target, const String& val)
139    {
140        static_cast<DirectionRandomiserAffector*>(target)->setRandomness(StringConverter::parseReal(val));
141    }
142
143    String DirectionRandomiserAffector::CmdScope::doGet(const void* target) const
144    {
145        return StringConverter::toString(
146            static_cast<const DirectionRandomiserAffector*>(target)->getScope() );
147    }
148    void DirectionRandomiserAffector::CmdScope::doSet(void* target, const String& val)
149    {
150        static_cast<DirectionRandomiserAffector*>(target)->setScope(StringConverter::parseReal(val));
151    }
152    String DirectionRandomiserAffector::CmdKeepVelocity::doGet(const void* target) const
153    {
154        return StringConverter::toString(
155            static_cast<const DirectionRandomiserAffector*>(target)->getKeepVelocity() );
156    }
157    void DirectionRandomiserAffector::CmdKeepVelocity::doSet(void* target, const String& val)
158    {
159        static_cast<DirectionRandomiserAffector*>(target)->setKeepVelocity(StringConverter::parseBool(val));
160    }
161
162}
Note: See TracBrowser for help on using the repository browser.