• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > net编程 >

XNA 2D粒子性能测试(2)

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-06-27点击:

第二步:在Game.cs中添加如下代码:

代码

//粒子类
StructParticle2D_Meteor[] structParticle2D_Meteor = new StructParticle2D_Meteor[5];
internal static SpriteBatch             spriteBatch1;
internal static SpriteBatch             spriteBatch2;
internal static SpriteBatch             spriteBatch3;
internal static SpriteBatch             spriteBatch4;

第三步:在Game.cs中初始化粒子类:

代码

//构造函数
public Game3        {
for (int i = 0; i < structParticle2D_Meteor.Length; i++)
ConfigMeteor(i);
}

void ConfigMeteor(int intGroup)
{
structParticle2D_Meteor[intGroup]                               = new StructParticle2D_Meteor(0);
structParticle2D_Meteor[intGroup].FloatSmallParticleInterval    = 0.2f;
structParticle2D_Meteor[intGroup].FlaotSPOriginParam            = 0.01f;//体积变小参数
structParticle2D_Meteor[intGroup].FloatSPGParam                 = 0.0004f;
//大粒子数量,小粒子数量 = 大粒子数量 * 100
structParticle2D_Meteor[intGroup].IntBPCount                    = 15;
structParticle2D_Meteor[intGroup].IntSPCount                    = structParticle2D_Meteor[intGroup].intBPCount * 100;
structParticle2D_Meteor[intGroup].colorSP                       = new Color(100, 100, 100, 150);
structParticle2D_Meteor[intGroup].colorBP                       = new Color(255, 255, 255, 150);

if (intGroup == 0)
{
structParticle2D_Meteor[intGroup].FloatBPMoveParamX = 10;//X角度(速度)
structParticle2D_Meteor[intGroup].FloatBPMoveParamY = 2;//Y角度(速度)
}
else if (intGroup == 1)
{
structParticle2D_Meteor[intGroup].FloatBPMoveParamX = 15;
structParticle2D_Meteor[intGroup].FloatBPMoveParamY = 4;
}
else if (intGroup == 2)
{
structParticle2D_Meteor[intGroup].FloatBPMoveParamX = 13;
structParticle2D_Meteor[intGroup].FloatBPMoveParamY = 5;
}
else if (intGroup == 3)
{
structParticle2D_Meteor[intGroup].FloatBPMoveParamX = 10;
structParticle2D_Meteor[intGroup].FloatBPMoveParamY = 4.5f;
}
else if (intGroup == 4)
{
structParticle2D_Meteor[intGroup].FloatBPMoveParamX = 7;
structParticle2D_Meteor[intGroup].FloatBPMoveParamY = 6;
}
structParticle2D_Meteor[intGroup].FloatSPOrigin         = new float[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].IntSPRandomRotation   = new int[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].FloatSPRandomSpeed    = new float[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].Vector2SPRandomPos    = new Vector2[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].FloatSPRandomInertiaX = new float[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].Vector2SPStartPos     = new Vector2[structParticle2D_Meteor[intGroup].IntSPCount];
structParticle2D_Meteor[intGroup].Vector2BPStartPos     = new Vector2[structParticle2D_Meteor[intGroup].IntBPCount];
structParticle2D_Meteor[intGroup].Vector2BPOrigin       = new Vector2[structParticle2D_Meteor[intGroup].IntBPCount];
structParticle2D_Meteor[intGroup].FloatSPG              = new float[structParticle2D_Meteor[intGroup].IntSPCount];

for (int i = 0; i < structParticle2D_Meteor[intGroup].IntSPCount; i++)
{

structParticle2D_Meteor[intGroup].FloatSPOrigin[i]          = RandomHelper.GetRandomFloat(0.06f, 0.1f);
structParticle2D_Meteor[intGroup].IntSPRandomRotation[i]    = RandomHelper.GetRandomInt(250, 290);
//发散速度
structParticle2D_Meteor[intGroup].FloatSPRandomSpeed[i]     = RandomHelper.GetRandomFloat(1.0f, 3.0f);
structParticle2D_Meteor[intGroup].Vector2SPRandomPos[i]     = new Vector2(
RandomHelper.GetRandomFloat(-3.0f, 3.0f),
RandomHelper.GetRandomFloat(-3.0f, 3.0f));
structParticle2D_Meteor[intGroup].FloatSPRandomInertiaX[i]  = RandomHelper.GetRandomFloat(0.1f, 0.3f);
}

for (int i = 0; i < structParticle2D_Meteor[intGroup].IntBPCount; i++)
{
if (intGroup == 0)
{
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].X = RandomHelper.GetRandomFloat(-0.5f, 0.5f) - 5;//初始位置
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].Y = RandomHelper.GetRandomFloat(-0.5f, 0.5f) + 5;//初始位置
}
else if (intGroup == 1)
{
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].X = RandomHelper.GetRandomFloat(-0.5f, 0.5f) - 5;
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].Y = RandomHelper.GetRandomFloat(-0.5f, 0.5f) + 15;
}
else if (intGroup == 2)
{
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].X = RandomHelper.GetRandomFloat(-0.5f, 0.5f) - 5;
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].Y = RandomHelper.GetRandomFloat(-0.5f, 0.5f) + 30;
}
else if (intGroup == 3)
{
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].X = RandomHelper.GetRandomFloat(-0.5f, 0.5f) - 6;
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].Y = RandomHelper.GetRandomFloat(-0.5f, 0.5f) + 40;
}
else if (intGroup == 4)
{
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].X = RandomHelper.GetRandomFloat(-0.5f, 0.5f) - 7;
structParticle2D_Meteor[intGroup].Vector2BPStartPos[i].Y = RandomHelper.GetRandomFloat(-0.5f, 0.5f) + 50;
}

structParticle2D_Meteor[intGroup].Vector2BPOrigin[i] = new Vector2(
RandomHelper.GetRandomFloat(0.15f, 0.2f),
RandomHelper.GetRandomFloat(0.1f, 0.15f));
}

structParticle2D_Meteor[intGroup].Vector2BPStartPosParam = structParticle2D_Meteor[intGroup].Vector2BPStartPos[intGroup];
}

 

第四步:重写Update与Draw函数

代码

public override void Update(GameTime gameTime)
{
for (int i = 0; i < structParticle2D_Meteor.Length; i++)
structParticle2D_Meteor[i].Update(gameTime);

base.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{
Game3.spriteBatch2.Begin(SpriteBlendMode.Additive, SpriteSortMode.FrontToBack, SaveStateMode.None);

//1.粒子
for (int i = 0; i < structParticle2D_Meteor.Length; i++)
structParticle2D_Meteor[i].Draw();

Game3.spriteBatch2.End();
base.Draw(gameTime);
}

修改Update里的代码可以让粒子按曲线飞行,代码上肯定有待改善的地方,回来试试,待更新:-D

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1170734538@qq.com ,  QQ