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

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

XNA 2D粒子性能测试

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

 效果图,重力值设置的比较小不够明显,但是看得出来,注意DEBUG下136帧:

进一步发散

Release版,提高了40帧-_-

 

新建一个XNA Windows项目XnaStructure3,

第一步:新加一个struct:StructParticle2D_Meteor.cs,代码如下:

代码

 最大偏移位置
/// </summary>
public float FloatSPMaxRandomPos
{
get { return floatSPMaxRandomPos; }
set { floatSPMaxRandomPos = value; }
}
internal float floatSPMaxRandomPos;
/// <summary>
/// 最小偏移位置
/// </summary>
public float FloatSPMinRandomPos
{
get { return floatSPMinRandomPos; }
set { floatSPMinRandomPos = value; }
}
internal float floatSPMinRandomPos;
/// <summary>
/// 初始位置
/// </summary>
public Vector2[] Vector2SPStartPos
{
get { return vector2SPStartPos; }
set { vector2SPStartPos = value; }
}
internal Vector2[] vector2SPStartPos;
/// <summary>
/// 随机体积
/// </summary>
public float[] FloatSPOrigin
{
get { return floatSPOrigin; }
set { floatSPOrigin = value; }
}
internal float[] floatSPOrigin;
/// <summary>
/// 随机方向
/// </summary>
public int[] IntSPRandomRotation
{
get { return intSPRandomRotation; }
set { intSPRandomRotation = value; }
}
internal int[] intSPRandomRotation;
/// <summary>
/// 随机速度
/// </summary>p
public float[] FloatSPRandomSpeed
{
get { return floatSPRandomSpeed; }
set { floatSPRandomSpeed = value; }
}
float[] floatSPRandomSpeed;
/// <summary>
/// 随机位置
/// </summary>
public Vector2[] Vector2SPRandomPos
{
get { return vector2SPRandomPos; }
set { vector2SPRandomPos = value; }
}
internal Vector2[] vector2SPRandomPos;
/// <summary>
/// 惯性   
/// </summary>
public float[] FloatSPRandomInertiaX
{
get { return floatSPRandomInertiaX; }
set { floatSPRandomInertiaX = value; }
}
internal float[] floatSPRandomInertiaX;

internal int intBPCount;
public int IntBPCount
{
get { return intBPCount; }
set { intBPCount = value; }
}

internal float floatBPMaxX;
public float FloatBPMaxX
{
get { return floatBPMaxX; }
set { floatBPMaxX = value; }
}

internal float floatBPMinX;
public float FloatBPMinX
{
get { return floatBPMinX; }
set { floatBPMinX = value; }
}

internal float floatBPMaxY;
public float FloatBPMaxY
{
get { return floatBPMaxY; }
set { floatBPMaxY = value; }
}

internal float floatBPMinY;
public float FloatBPMinY
{
get { return floatBPMinY; }
set { floatBPMinY = value; }
}

internal float floatBPMoveParamX;
public float FloatBPMoveParamX
{
get { return floatBPMoveParamX; }
set { floatBPMoveParamX = value; }
}

internal float floatBPMoveParamY;
public float FloatBPMoveParamY
{
get { return floatBPMoveParamY; }
set { floatBPMoveParamY = value; }
}

internal float floatBPMinSize;
public float FloatBPMinSize
{
get { return floatBPMinSize; }
set { floatBPMinSize = value; }
}

internal float floatBPMaxSize;
public float FloatBPMaxSize
{
get { return floatBPMaxSize; }
set { floatBPMaxSize = value; }
}

internal Vector2[] vector2BPStartPos;
public Vector2[] Vector2BPStartPos
{
get { return vector2BPStartPos; }
set { vector2BPStartPos = value; }
}
/// <summary>
/// 大粒子体积
/// </summary>
internal Vector2[] vector2BPOrigin;
public Vector2[] Vector2BPOrigin
{
get { return vector2BPOrigin; }
set { vector2BPOrigin = value; }
}
/// <summary>
/// 记录第一个粒子的初始位置
/// </summary>
internal Vector2 vector2BPStartPosParam;
public Vector2 Vector2BPStartPosParam
{
get { return vector2BPStartPosParam; }
set { vector2BPStartPosParam = value; }
}
//小粒子颜色
internal Color colorSP;
public Color ColorSP
{
get { return colorSP; }
set { colorSP = value; }
}
//小粒子颜色
internal Color colorBP;
public Color ColorBP   
{
get { return colorBP; }
set { colorBP = value; }
}
#endregion

//构造函数
public StructParticle2D_Meteor(int intSPCount)
{
//Small
this.intSPCount                 = 0;
this.floatSmallParticleInterval = 0;
this.intSPStartRotation         = 0;
this.intSPEndRotation           = 0;
this.floatSPMinSize             = 0;
this.floatSPMaxSize             = 0;
this.flaotSPOriginParam         = 0;
this.floatSPGParam              = 0;
this.floatSPMaxRandomPos        = 0;
this.floatSPMinRandomPos        = 0;
vector2SPStartPos               = null;
floatSPOrigin                   = null;
intSPRandomRotation             = null;
floatSPRandomSpeed              = null;
vector2SPRandomPos              = null;
floatSPG                        = null;
floatSPRandomInertiaX           = null;
colorSP                         = Color.White;

//Big
this.intBPCount                 = 0;
this.floatBPMaxX                = 0;
this.floatBPMinX                = 0;
this.floatBPMaxY                = 0;
this.floatBPMinY                = 0;
this.floatBPMoveParamX          = 0;
this.floatBPMoveParamY          = 0;
this.floatBPMinSize             = 0;
this.floatBPMaxSize             = 0;
vector2BPStartPos               = null;
vector2BPOrigin                 = null;
vector2BPOrigin                 = null;
vector2BPStartPosParam          = Vector2.Zero;
colorBP                         = Color.White;
}

internal void Update(GameTime gameTime)
{
for (int i = 0; i < intBPCount; i++)
{
vector2BPStartPos[i].X += Game3.MoveFactorPerSecond * floatBPMoveParamX;
vector2BPStartPos[i].Y += Game3.MoveFactorPerSecond * floatBPMoveParamY;
}

for (int i = 0; i < intSPCount; i++)
if (vector2BPStartPos[0].X >= vector2BPStartPosParam.X + i * floatSmallParticleInterval)
{
if (vector2SPStartPos[i] == Vector2.Zero)
{
vector2SPStartPos[i].X = vector2BPStartPos[0].X + vector2SPRandomPos[i].X;
vector2SPStartPos[i].Y = vector2BPStartPos[0].Y + vector2SPRandomPos[i].Y;
}
else
{
//发散
vector2SPStartPos[i].X += Game3.MoveFactorPerSecond * Game3.bulletHelper.floatCirqueX360[intSPRandomRotation[i]] * floatSPRandomSpeed[i];
vector2SPStartPos[i].Y += Game3.MoveFactorPerSecond * Game3.bulletHelper.floatCirqueY360[intSPRandomRotation[i]] * floatSPRandomSpeed[i];
floatSPG[i] += Game3.MoveFactorPerSecond * floatBPMoveParamX * floatSPGParam;
//下坠
vector2SPStartPos[i].Y += 0.5f * 9.8f * floatSPG[i] * floatSPG[i];
//惯性
vector2SPStartPos[i].X += Game3.MoveFactorPerSecond * floatBPMoveParamX * floatSPRandomInertiaX[i];
//体积变小
floatSPOrigin[i] -= Game3.MoveFactorPerSecond * flaotSPOriginParam;
}
}
}

internal void Draw()
{
//小
for (int i = 0; i < intSPCount; i++)
if (vector2SPStartPos[i] != Vector2.Zero)
if (floatSPOrigin[i] >= 0.02f)
for (int j = 0; j < 3; j++)
drawBullet(vector2SPStartPos[i], colorSP, floatSPOrigin[i]);

updateBPCountLimit();

//大
for (int i = 0; i < intBPCountLimit; i++)
drawBullet(vector2BPStartPos[i], colorBP, vector2BPOrigin[i]);
}

void updateBPCountLimit()
{
//if (vector2SPStartPos[1000 - 1] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 10;
//else if (vector2SPStartPos[900] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 9;
//else if (vector2SPStartPos[800] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 8;
//else if (vector2SPStartPos[700] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 7;
//else if (vector2SPStartPos[600] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 6;
//else if (vector2SPStartPos[500] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 5;
//else if (vector2SPStartPos[400] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 4;
//else if (vector2SPStartPos[300] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 3;
//else if (vector2SPStartPos[200] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 2;
//else if (vector2SPStartPos[100] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 1;
//else if (vector2SPStartPos[0] != Vector2.Zero)
//    intBPCountLimit = intBPCount - 0;

for (int i = intSPCount; i >= 0; i -= 100)
if (i == intSPCount)
{
if (vector2SPStartPos[intSPCount - 1] != Vector2.Zero)
intBPCountLimit = 0;
return;
}
else
{
if (vector2SPStartPos[i] != Vector2.Zero)
intBPCountLimit = intBPCount - Convert.ToInt32(i / 100);
return;
}
}

void drawBullet(Vector2 vector2Pos, Color color, Vector2 vector2Origin)
{
Game3.spriteBatch2.Draw(
Game3.textureDark_Bullet,
vector2Pos,
Game3.bulletHelper.recDarkBullet,
color,
0,
Game3.bulletHelper.vector2DarkBullet,
vector2Origin,
SpriteEffects.None,
0);
}

void drawBullet(Vector2 vector2Pos, Color color, float floatOrigin)
{
Game3.spriteBatch2.Draw(
Game3.textureDark_Bullet,
vector2Pos,
Game3.bulletHelper.recDarkBullet,
color,
0,
Game3.bulletHelper.vector2DarkBullet,
floatOrigin,
SpriteEffects.None,
0);
}
}
}

About D8

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