泛型粒子系统的设计6 (转)

泛型粒子系统的设计6 (转)[@more@]

XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

// 移动位置更新

template<

  class _ParticleType,

  size_t nVelIndex

> class TMovePosActor {

protected:

  typedef _ParticleType  tParticle;

public:

  // 执行更新

  template< size_t nIndex >

  void Action( const double& dTime, tParticle& p ) {

  p.Part< nIndex >().m_Value += p.Part< nVelIndex >().m_Value * dTime;

  }

};

TMovePosActor是一个移动位置更新器,它根据当前的速度值更新位置,只能应用于粒子的位置部分。它有两个模板参数,_ParticleType用于指定所需要更新的粒子类型,nVelIndex用于指定粒子的速度部分在粒子结构中的索引

// 重力速度更新器

template<

  class _ParticleType,

  typename _Vector3Type

> class TGravityVelActor {

protected:

  typedef _ParticleType  tParticle;

  typedef _Vector3Type  tVector3;

  // 重力加速度

  tVectorsourceValue="3" UnitName="m">3 m_vGravity;

public:

  TGravityVelActor( void )

  : m_vGravity( tVector3( 0.0,-9.8, 0.0 ) )

  {}

  // 设置重力加速度

  void SetGravity( const tVector3& vec3 ) {

  m_vGravity = vec3;

  }

  // 执行更新

  template< size_t nIndex >

  void Action( const double& dTime, tParticle& p ) {

  p.Part< nIndex >().m_Value += m_vGravity * dTime;

  }

};

TGravityVelActor是一个重力速度更新器,它根据指定的重力加速度更新速度,只能应用于粒子的速度部分。它有两个模板参数,_ParticleType用于指定所需要更新的粒子类型,_Vector3Type用于指定重力加速度的类型。公共成员函数SetGravity终于设置重力加速度。

// 寿命逝去更新器

template<

  class _ParticleType

> class TElapseLifeActor {

protected:

  typedef _ParticleType  tParticle;

public:

  // 执行更新

  template< size_t nIndex >

  void Action( const double& dTime, tParticle& p ) {

  p.Part< nIndex >().m_Value -= dTime;

  }

};

TElapseLifeActor是一个寿命逝去更新器,它根据逝去的时间减少寿命,只能应用于粒子的寿命部分。它有一个模板参数,_ParticleType用于指定所需要更新的粒子类型。

// 死亡断言更新器

template<

  class _ParticleType

> class TDeadLifeActor {

protected:

  typedef _ParticleType  tParticle;

  typedef boost::function< bool ( const tParticle& ) >  tfnDead;

  // 死亡断言函数对象

  tfnDead m_DeadPred;

public:

  // Set dead predicate

  void SetDeadPredicate( const tfnDead& DeadPred ) { m_DeadPred = DeadPred; }

  // 执行更新

  template< size_t nIndex >

  void Action( const double&, tParticle& p ) {

  if( ! m_DeadPred.empty() ) {

  if( m_DeadPred( p ) ) p.Part< nIndex >().m_Value = -0.0f;

  }

  }

};

TDeadLifeActor是一个死亡断言更新器,它调用死亡断言函数对象来确认粒子是否死亡,如果死亡则修改寿命值为-0.0,只能应用于粒子的寿命部分。它有一个模板参数,_ParticleType用于指定所需要更新的粒子类型。

如果以上提到的内建更新器中并没有您需要的,那么动手自定义一个更新器也是一件非常轻松愉快的事情。

// 退色更新器

template<

  class _ParticleType,

  typename _ColorType,

  size_t nFadeIndex

> class TFadeColorActor {

protected:

  typedef _ParticleType  tParticle;

  typedef _ColorType  tColor;

  // 颜色一

  tColor  m_Value1;

  // 颜色二

  tColor  m_Value2;

public:

  TFadeColorActor( void )

  : m_Value1( tColor( 1.0, 1.0, 1.0, 1.0 ) )

  , m_Value2( tColor( 0.0, 0.0, 0.0, 1.0 ) )

  {}

  // 设定颜色一

  void SetColorOne( const tColor& col ) {

  m_Value1 = col;

  }

  // 设定颜色二

  void SetColorTwo( const tColor& col ) {

  m_Value2 = col;

  }

  // 执行更新

  template< size_t nIndex >

  void Action( const double& dTime, tParticle& p ) {

  p.Part< nIndex >().m_Value += ( m_Value2 - m_Value1 ) * p.Part< nFadeIndex >().m_Value * dTime;

  }

};

这里我们自定义了一个根据寿命值使粒子颜色由颜色一退为颜色二的更新器,只能应用于粒子的颜色部分。它有三个模板参数,_ParticleType用于指定所需要更新的粒子类型,_ColorType用于指定颜色类型,nFadeIndex用于指定退色因子在粒子结构中的索引。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10794571/viewspace-969593/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10794571/viewspace-969593/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值