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

Revision 692, 7.0 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 "OgreDeflectorPlaneAffector.h"
26#include "OgreParticleSystem.h"
27#include "OgreParticle.h"
28#include "OgreStringConverter.h"
29
30
31namespace Ogre {
32
33    // Instantiate statics
34    DeflectorPlaneAffector::CmdPlanePoint DeflectorPlaneAffector::msPlanePointCmd;
35    DeflectorPlaneAffector::CmdPlaneNormal DeflectorPlaneAffector::msPlaneNormalCmd;
36    DeflectorPlaneAffector::CmdBounce DeflectorPlaneAffector::msBounceCmd;
37
38    //-----------------------------------------------------------------------
39    DeflectorPlaneAffector::DeflectorPlaneAffector(ParticleSystem* psys)
40        : ParticleAffector(psys)
41    {
42        mType = "DeflectorPlane";
43
44        // defaults
45        mPlanePoint = Vector3::ZERO;
46        mPlaneNormal = Vector3::UNIT_Y;
47        mBounce = 1.0;
48
49        // Set up parameters
50        if (createParamDictionary("DeflectorPlaneAffector"))
51        {
52            addBaseParameters();
53            // Add extra paramaters
54            ParamDictionary* dict = getParamDictionary();
55            dict->addParameter(ParameterDef("plane_point",
56                "A point on the deflector plane. Together with the normal vector it defines the plane.",
57                PT_VECTOR3), &msPlanePointCmd);
58            dict->addParameter(ParameterDef("plane_normal",
59                "The normal vector of the deflector plane. Together with the point it defines the plane.",
60                PT_VECTOR3), &msPlaneNormalCmd);
61            dict->addParameter(ParameterDef("bounce",
62                "The amount of bouncing when a particle is deflected. 0 means no deflection and 1 stands for 100 percent reflection.",
63                PT_REAL), &msBounceCmd);
64        }
65    }
66    //-----------------------------------------------------------------------
67    void DeflectorPlaneAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
68    {
69        // precalculate distance of plane from origin
70        Real planeDistance = - mPlaneNormal.dotProduct(mPlanePoint) / Math::Sqrt(mPlaneNormal.dotProduct(mPlaneNormal));
71                Vector3 directionPart;
72
73        ParticleIterator pi = pSystem->_getIterator();
74        Particle *p;
75
76        while (!pi.end())
77        {
78            p = pi.getNext();
79
80            Vector3 direction(p->direction * timeElapsed);
81            if (mPlaneNormal.dotProduct(p->position + direction) + planeDistance <= 0.0)
82            {
83                Real a = mPlaneNormal.dotProduct(p->position) + planeDistance;
84                if (a > 0.0)
85                {
86                    // for intersection point
87                                        directionPart = direction * (- a / direction.dotProduct( mPlaneNormal ));
88                    // set new position
89                                        p->position = (p->position + ( directionPart )) + (((directionPart) - direction) * mBounce);
90
91                    // reflect direction vector
92                    p->direction = (p->direction - (2.0 * p->direction.dotProduct( mPlaneNormal ) * mPlaneNormal)) * mBounce;
93                }
94            }
95        }
96    }
97    //-----------------------------------------------------------------------
98    void DeflectorPlaneAffector::setPlanePoint(const Vector3& pos)
99    {
100        mPlanePoint = pos;
101    }
102    //-----------------------------------------------------------------------
103    void DeflectorPlaneAffector::setPlaneNormal(const Vector3& normal)
104    {
105        mPlaneNormal = normal;
106    }
107    //-----------------------------------------------------------------------
108    void DeflectorPlaneAffector::setBounce(Real bounce)
109    {
110        mBounce = bounce;
111    }
112
113    //-----------------------------------------------------------------------
114    Vector3 DeflectorPlaneAffector::getPlanePoint(void) const
115    {
116        return mPlanePoint;
117    }
118    //-----------------------------------------------------------------------
119    Vector3 DeflectorPlaneAffector::getPlaneNormal(void) const
120    {
121        return mPlaneNormal;
122    }
123    //-----------------------------------------------------------------------
124    Real DeflectorPlaneAffector::getBounce(void) const
125    {
126        return mBounce;
127    }
128
129    //-----------------------------------------------------------------------
130    //-----------------------------------------------------------------------
131    // Command objects
132    //-----------------------------------------------------------------------
133    //-----------------------------------------------------------------------
134    String DeflectorPlaneAffector::CmdPlanePoint::doGet(const void* target) const
135    {
136        return StringConverter::toString(
137            static_cast<const DeflectorPlaneAffector*>(target)->getPlanePoint() );
138    }
139    void DeflectorPlaneAffector::CmdPlanePoint::doSet(void* target, const String& val)
140    {
141        static_cast<DeflectorPlaneAffector*>(target)->setPlanePoint(
142            StringConverter::parseVector3(val));
143    }
144    //-----------------------------------------------------------------------
145    String DeflectorPlaneAffector::CmdPlaneNormal::doGet(const void* target) const
146    {
147        return StringConverter::toString(
148            static_cast<const DeflectorPlaneAffector*>(target)->getPlaneNormal() );
149    }
150    void DeflectorPlaneAffector::CmdPlaneNormal::doSet(void* target, const String& val)
151    {
152        static_cast<DeflectorPlaneAffector*>(target)->setPlaneNormal(
153            StringConverter::parseVector3(val));
154    }
155    //-----------------------------------------------------------------------
156    String DeflectorPlaneAffector::CmdBounce::doGet(const void* target) const
157    {
158        return StringConverter::toString(
159            static_cast<const DeflectorPlaneAffector*>(target)->getBounce() );
160
161    }
162    void DeflectorPlaneAffector::CmdBounce::doSet(void* target, const String& val)
163    {
164        static_cast<DeflectorPlaneAffector*>(target)->setBounce(
165            StringConverter::parseReal(val));
166    }
167
168}
Note: See TracBrowser for help on using the repository browser.