C# XNA 2D基本冲突列表
我是编程界的初学者,现在我正在用C#尝试XNA编程,现在我正在做一个基本游戏,玩家是飞机或宇宙飞船,你必须击落流星C# XNA 2D基本冲突列表,c#,xna,collision-detection,C#,Xna,Collision Detection,我是编程界的初学者,现在我正在用C#尝试XNA编程,现在我正在做一个基本游戏,玩家是飞机或宇宙飞船,你必须击落流星 gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height); foreach (var item in meteor_pos) { meteor_rect = new Rectangle((int)item.X, (int)item.Y, g
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
目前我正在尝试框碰撞检测,但我不太明白,我真的不知道如何获得列表中每个项目的位置,我需要您的帮助
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
所以基本上我有一个shiptexture和一个meteortexture。modeltexture被绘制10次,并获得随机位置。我想在每颗流星周围画一个矩形,但我不知道怎么画。我尝试了下图所示的foreach循环,但当我与它碰撞时,只有一颗流星起作用。对不起,如果我的英语不是最好的,我感谢大家的帮助
List<Vector2> meteor_pos = new List<Vector2>();
//loadcontent
for (int i = 0; i < 10; i++)
{
meteor_pos.Add(new Vector2(myRnd.Next(800), myRnd.Next(600)));
double tmp_angle = (myRnd.Next(1000) * Math.PI * 2) / 1000.0;
double tmp_speed = 0.5 + 3.0 * (myRnd.Next(1000) / 1000.0);
meteor_speed.Add(new Vector2((float)(tmp_speed * Math.Cos(tmp_angle)),
(float)(tmp_speed * Math.Sin(tmp_angle))));
}
//protected override void Update(GameTime gameTime)
for (int i = 0; i < meteor_pos.Count; i++)
{
meteor_pos[i] += meteor_speed[i];
Vector2 v = meteor_pos[i];
//Outside the screen?
if (v.X < -80)
v.X = graphics.GraphicsDevice.Viewport.Width + 80;
if (v.X > graphics.GraphicsDevice.Viewport.Width + 80)
v.X = -80;
if (v.Y < -60)
v.Y = graphics.GraphicsDevice.Viewport.Height + 60;
if (v.Y > graphics.GraphicsDevice.Viewport.Height + 60)
v.Y = -60;
//Uppdate the list
meteor_pos[i] = v;
}
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
}
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
//protected override void Draw(GameTime gameTime)
for (int i = 0; i < meteor_pos.Count; i++)
{
spriteBatch.Draw(gfx_meteor, meteor_pos[i], null, Color.White, 0,
new Vector2(gfx_meteor.Width / 2, gfx_meteor.Height / 2), 1.0f, SpriteEffects.None, 0);
}}
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
List meteor_pos=new List();
//装载量
对于(int i=0;i<10;i++)
{
流星位置添加(新矢量2(myRnd.Next(800),myRnd.Next(600));
双tmp_角=(myRnd.Next(1000)*Math.PI*2)/1000.0;
双tmp_速度=0.5+3.0*(myRnd.Next(1000)/1000.0);
meteor_speed.Add(新矢量2((浮点)(tmp_speed*Math.Cos(tmp_角度)),
(浮动)(tmp_速度*数学正弦(tmp_角度));
}
//受保护覆盖无效更新(游戏时间游戏时间)
对于(int i=0;igraphics.GraphicsDevice.Viewport.Width+80)
v、 X=-80;
如果(v.Y<-60)
v、 Y=graphics.GraphicsDevice.Viewport.Height+60;
如果(v.Y>graphics.GraphicsDevice.Viewport.Height+60)
v、 Y=-60;
//请在名单上注明日期
流星位置[i]=v;
}
foreach(meteor_pos中的var项目)
{
meteor_rect=新矩形((int)item.X,(int)item.Y,gfx_meteor.Width,gfx_meteor.Height);
}
gfx_rect=新矩形((int)position.X,(int)position.Y,gfx.Width,gfx.Height);
如果(gfx直角相交(流星直角))
{
位置X=0;
位置Y=0;
}
//受保护覆盖无效绘制(游戏时间游戏时间)
对于(int i=0;i
您的方法非常混乱,您需要创建一个类,至少可能是一个处理位置和其他一些基本信息的sprite类
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
然而,为了解决当前的问题,在foreach循环中覆盖meteor_rect的值,然后在最后只检查一次碰撞
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
因此,将代码切换为如下所示:
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}
但是就像我说的,请查阅一个基础教程来帮助你:)
gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)
{
meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
if (gfx_rect.Intersects(meteor_rect))
{
position.X = 0;
position.Y = 0;
}
}