首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

Ogre粒子系统中的DeflectorPlaneAffector粒子影打击乐器

2013-03-13 
Ogre粒子系统中的DeflectorPlaneAffector粒子影响器ParticleAffector(粒子影响器)用于在粒子的生命周期内

Ogre粒子系统中的DeflectorPlaneAffector粒子影响器
ParticleAffector(粒子影响器)

用于在粒子的生命周期内更新粒子的属性,最重要的方法是 _affectParticles(),OGRE中提供了如下派生类,自己也可以根据需要编写自己的Affector:

ColourFaderAffector

ColourImageAffector

ColourInterpolatorAffector

DeflectorPlaneAffector

DirectionRandomiserAffector

LinearForceAffector

RotationAffector

ScaleAffector

下面对基于平面的粒子影响器DeflectorPlaneAffector进行解析:

成员变量如下:

///deflector plane point

Vector3 mPlanePoint;

///deflector plane normal vector

Vector3 mPlaneNormal;

///bounce factor (0.5 means 50 percent)

Real mBounce;

其中,mPlanePoint表示平面上的某个参考点,mPlaneNormal表示平面的法向量,mPlanePoint和mPlaneNormal共同决定了一个平面,也是平面的两个属性;mBounce表示平面的反弹系数,比如1.0表示完全反弹。

核心成员函数:

_affectParticles(ParticleSystem*pSystem, RealtimeElapsed);

粒子影响器的核心函数,将会影响粒子的运动轨迹。如果遇到平面,粒子会反弹;否则,按粒子的正常运动轨迹进行运动。具体实现如下:

    void DeflectorPlaneAffector::_affectParticles(ParticleSystem*pSystem,RealtimeElapsed)

    {

        //precalculate distance of plane from origin

        RealplaneDistance= -mPlaneNormal.dotProduct(mPlanePoint)/Math::Sqrt(mPlaneNormal.dotProduct(mPlaneNormal));

              Vector3directionPart;

 

        ParticleIteratorpi =pSystem->_getIterator();

        Particle*p;

 

        while(!pi.end())

        {

            p = pi.getNext();

 

            Vector3direction(p->direction*timeElapsed);

            if(mPlaneNormal.dotProduct(p->position +direction)+ planeDistance <= 0.0)

            {

                Reala =mPlaneNormal.dotProduct(p->position)+planeDistance;

                if(a > 0.0)

                {

                    //for intersection point

                                   directionPart =direction* (- a /direction.dotProduct(mPlaneNormal));

                    //set new position

                                   p->position = (p->position+ (directionPart )) + (((directionPart) -direction)* mBounce);

 

                    //reflect direction vector

                    p->direction = (p->direction- (2.0 *p->direction.dotProduct(mPlaneNormal ) *mPlaneNormal))* mBounce;

                }

            }

        }

}

planeDistance表示平面上的某个参考点到原点的距离;

directionPart表示粒子与平面的碰撞点;

mPlaneNormal.dotProduct(p->position+ direction) + planeDistance <= 0.0表示粒子与平面碰撞的情况;

a = mPlaneNormal.dotProduct(p->position) +planeDistance; a > 0表示粒子的前一时刻(或者说粒子在前一帧)位于平面上方;

值得一提的是,p是粒子,他有两个重要的属性,position和direction,position表示粒子当前的位置,direction表示当前的速度,速度的方向表示粒子的方向,速度的大小表示粒子的速率。

p->position = (p->position + ( directionPart )) + (((directionPart) -direction) * mBounce);表示更新粒子的位置;

p->direction = (p->direction - (2.0 *p->direction.dotProduct( mPlaneNormal ) * mPlaneNormal)) * mBounce;表示更新粒子的速度;

热点排行