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表示平面的法向量,mPlanePointmPlaneNormal共同决定了一个平面,也是平面的两个属性;mBounce表示平面的反弹系数,比如1.0表示完全反弹。

核心成员函数:

_affectParticles(ParticleSystem* pSystem, Real timeElapsed);

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

    void DeflectorPlaneAffector::_affectParticles(ParticleSystem* pSystem,Real timeElapsed)

    {

        //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是粒子,他有两个重要的属性,positiondirectionposition表示粒子当前的位置,direction表示当前的速度,速度的方向表示粒子的方向,速度的大小表示粒子的速率。

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值