Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreLinearForceAffector.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreLinearForceAffector.h"
00026 #include "OgreParticleSystem.h"
00027 #include "OgreParticle.h"
00028 #include "OgreStringConverter.h"
00029 
00030 
00031 namespace Ogre {
00032 
00033     // Instantiate statics
00034     LinearForceAffector::CmdForceVector LinearForceAffector::msForceVectorCmd;
00035     LinearForceAffector::CmdForceApp LinearForceAffector::msForceAppCmd;
00036 
00037 
00038     //-----------------------------------------------------------------------
00039     LinearForceAffector::LinearForceAffector()
00040     {
00041         mType = "LinearForce";
00042 
00043         // Default to gravity-like
00044         mForceApplication = FA_ADD;
00045         mForceVector.x = mForceVector.z = 0;
00046         mForceVector.y = -100;
00047 
00048         // Set up parameters
00049         if (createParamDictionary("LinearForceAffector"))
00050         {
00051             addBaseParameters();
00052             // Add extra paramaters
00053             ParamDictionary* dict = getParamDictionary();
00054             dict->addParameter(ParameterDef("force_vector", 
00055                 "The vector representing the force to apply.",
00056                 PT_VECTOR3),&msForceVectorCmd);
00057             dict->addParameter(ParameterDef("force_application", 
00058                 "How to apply the force vector to partices.",
00059                 PT_UNSIGNED_INT),&msForceAppCmd);
00060 
00061         }
00062 
00063     }
00064     //-----------------------------------------------------------------------
00065     void LinearForceAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
00066     {
00067         ParticleIterator pi = pSystem->_getIterator();
00068         Particle *p;
00069 
00070         Vector3 scaledVector;
00071 
00072         // Precalc scaled force for optimisation
00073         if (mForceApplication == FA_ADD)
00074         {
00075             // Scale force by time
00076             scaledVector = mForceVector * timeElapsed;
00077         }
00078 
00079         while (!pi.end())
00080         {
00081             p = pi.getNext();
00082             if (mForceApplication == FA_ADD)
00083             {
00084                 p->mDirection += scaledVector;
00085             }
00086             else // FA_AVERAGE
00087             {
00088                 p->mDirection = (p->mDirection + mForceVector) / 2;
00089             }
00090         }
00091         
00092     }
00093     //-----------------------------------------------------------------------
00094     void LinearForceAffector::setForceVector(const Vector3& force)
00095     {
00096         mForceVector = force;
00097     }
00098     //-----------------------------------------------------------------------
00099     void LinearForceAffector::setForceApplication(ForceApplication fa)
00100     {
00101         mForceApplication = fa;
00102     }
00103     //-----------------------------------------------------------------------
00104     Vector3 LinearForceAffector::getForceVector(void) const
00105     {
00106         return mForceVector;
00107     }
00108     //-----------------------------------------------------------------------
00109     LinearForceAffector::ForceApplication LinearForceAffector::getForceApplication(void) const
00110     {
00111         return mForceApplication;
00112     }
00113 
00114     //-----------------------------------------------------------------------
00115     //-----------------------------------------------------------------------
00116     // Command objects
00117     //-----------------------------------------------------------------------
00118     //-----------------------------------------------------------------------
00119     String LinearForceAffector::CmdForceVector::doGet(const void* target) const
00120     {
00121         return StringConverter::toString(
00122             static_cast<const LinearForceAffector*>(target)->getForceVector() );
00123     }
00124     void LinearForceAffector::CmdForceVector::doSet(void* target, const String& val)
00125     {
00126         static_cast<LinearForceAffector*>(target)->setForceVector(
00127             StringConverter::parseVector3(val));
00128     }
00129     //-----------------------------------------------------------------------
00130     String LinearForceAffector::CmdForceApp::doGet(const void* target) const
00131     {
00132         ForceApplication app = static_cast<const LinearForceAffector*>(target)->getForceApplication();
00133         switch(app)
00134         {
00135         case LinearForceAffector::FA_AVERAGE:
00136             return "average";
00137             break;
00138         case LinearForceAffector::FA_ADD:
00139             return "add";
00140             break;
00141         }
00142         // Compiler nicety
00143         return "";
00144     }
00145     void LinearForceAffector::CmdForceApp::doSet(void* target, const String& val)
00146     {
00147         if (val == "average")
00148         {
00149             static_cast<LinearForceAffector*>(target)->setForceApplication(FA_AVERAGE);
00150         }
00151         else if (val == "add")
00152         {
00153             static_cast<LinearForceAffector*>(target)->setForceApplication(FA_ADD);
00154         }
00155     }
00156 
00157 
00158 }
00159 

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:19 2004