1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreLinearForceAffector.h" |
---|
30 | #include "OgreParticleSystem.h" |
---|
31 | #include "OgreParticle.h" |
---|
32 | #include "OgreStringConverter.h" |
---|
33 | |
---|
34 | |
---|
35 | namespace Ogre { |
---|
36 | |
---|
37 | // Instantiate statics |
---|
38 | LinearForceAffector::CmdForceVector LinearForceAffector::msForceVectorCmd; |
---|
39 | LinearForceAffector::CmdForceApp LinearForceAffector::msForceAppCmd; |
---|
40 | |
---|
41 | |
---|
42 | //----------------------------------------------------------------------- |
---|
43 | LinearForceAffector::LinearForceAffector(ParticleSystem* psys) |
---|
44 | :ParticleAffector(psys) |
---|
45 | { |
---|
46 | mType = "LinearForce"; |
---|
47 | |
---|
48 | // Default to gravity-like |
---|
49 | mForceApplication = FA_ADD; |
---|
50 | mForceVector.x = mForceVector.z = 0; |
---|
51 | mForceVector.y = -100; |
---|
52 | |
---|
53 | // Set up parameters |
---|
54 | if (createParamDictionary("LinearForceAffector")) |
---|
55 | { |
---|
56 | addBaseParameters(); |
---|
57 | // Add extra paramaters |
---|
58 | ParamDictionary* dict = getParamDictionary(); |
---|
59 | dict->addParameter(ParameterDef("force_vector", |
---|
60 | "The vector representing the force to apply.", |
---|
61 | PT_VECTOR3),&msForceVectorCmd); |
---|
62 | dict->addParameter(ParameterDef("force_application", |
---|
63 | "How to apply the force vector to partices.", |
---|
64 | PT_STRING),&msForceAppCmd); |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | //----------------------------------------------------------------------- |
---|
70 | void LinearForceAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed) |
---|
71 | { |
---|
72 | ParticleIterator pi = pSystem->_getIterator(); |
---|
73 | Particle *p; |
---|
74 | |
---|
75 | Vector3 scaledVector; |
---|
76 | |
---|
77 | // Precalc scaled force for optimisation |
---|
78 | if (mForceApplication == FA_ADD) |
---|
79 | { |
---|
80 | // Scale force by time |
---|
81 | scaledVector = mForceVector * timeElapsed; |
---|
82 | } |
---|
83 | |
---|
84 | while (!pi.end()) |
---|
85 | { |
---|
86 | p = pi.getNext(); |
---|
87 | if (mForceApplication == FA_ADD) |
---|
88 | { |
---|
89 | p->direction += scaledVector; |
---|
90 | } |
---|
91 | else // FA_AVERAGE |
---|
92 | { |
---|
93 | p->direction = (p->direction + mForceVector) / 2; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|
98 | //----------------------------------------------------------------------- |
---|
99 | void LinearForceAffector::setForceVector(const Vector3& force) |
---|
100 | { |
---|
101 | mForceVector = force; |
---|
102 | } |
---|
103 | //----------------------------------------------------------------------- |
---|
104 | void LinearForceAffector::setForceApplication(ForceApplication fa) |
---|
105 | { |
---|
106 | mForceApplication = fa; |
---|
107 | } |
---|
108 | //----------------------------------------------------------------------- |
---|
109 | Vector3 LinearForceAffector::getForceVector(void) const |
---|
110 | { |
---|
111 | return mForceVector; |
---|
112 | } |
---|
113 | //----------------------------------------------------------------------- |
---|
114 | LinearForceAffector::ForceApplication LinearForceAffector::getForceApplication(void) const |
---|
115 | { |
---|
116 | return mForceApplication; |
---|
117 | } |
---|
118 | |
---|
119 | //----------------------------------------------------------------------- |
---|
120 | //----------------------------------------------------------------------- |
---|
121 | // Command objects |
---|
122 | //----------------------------------------------------------------------- |
---|
123 | //----------------------------------------------------------------------- |
---|
124 | String LinearForceAffector::CmdForceVector::doGet(const void* target) const |
---|
125 | { |
---|
126 | return StringConverter::toString( |
---|
127 | static_cast<const LinearForceAffector*>(target)->getForceVector() ); |
---|
128 | } |
---|
129 | void LinearForceAffector::CmdForceVector::doSet(void* target, const String& val) |
---|
130 | { |
---|
131 | static_cast<LinearForceAffector*>(target)->setForceVector( |
---|
132 | StringConverter::parseVector3(val)); |
---|
133 | } |
---|
134 | //----------------------------------------------------------------------- |
---|
135 | String LinearForceAffector::CmdForceApp::doGet(const void* target) const |
---|
136 | { |
---|
137 | ForceApplication app = static_cast<const LinearForceAffector*>(target)->getForceApplication(); |
---|
138 | switch(app) |
---|
139 | { |
---|
140 | case LinearForceAffector::FA_AVERAGE: |
---|
141 | return "average"; |
---|
142 | break; |
---|
143 | case LinearForceAffector::FA_ADD: |
---|
144 | return "add"; |
---|
145 | break; |
---|
146 | } |
---|
147 | // Compiler nicety |
---|
148 | return ""; |
---|
149 | } |
---|
150 | void LinearForceAffector::CmdForceApp::doSet(void* target, const String& val) |
---|
151 | { |
---|
152 | if (val == "average") |
---|
153 | { |
---|
154 | static_cast<LinearForceAffector*>(target)->setForceApplication(FA_AVERAGE); |
---|
155 | } |
---|
156 | else if (val == "add") |
---|
157 | { |
---|
158 | static_cast<LinearForceAffector*>(target)->setForceApplication(FA_ADD); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | } |
---|
164 | |
---|