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