C# XNA 4.0 2d碰撞检测

C# XNA 4.0 2d碰撞检测,c#,xna,collision-detection,C#,Xna,Collision Detection,我有一个小坦克和一个黑色正方形的程序。油箱应响应用户输入,并在接触油箱时停止。但是,在窗口出现(包含盒子和油箱)后,我没有用户输入。当我按下其中一个箭头键时,坦克根本不动 下面是我代码中一些更重要的例子 public class TopDownShooter : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; private Texture2D

我有一个小坦克和一个黑色正方形的程序。油箱应响应用户输入,并在接触油箱时停止。但是,在窗口出现(包含盒子和油箱)后,我没有用户输入。当我按下其中一个箭头键时,坦克根本不动

下面是我代码中一些更重要的例子

public class TopDownShooter : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private Texture2D tank, blackSquare;
    private KeyboardState state;
    private float angle1 = 0;
    private float angle2 = 0;
    private float xLocation, yLocation;
    Vector2 location1 = new Vector2(900, 400), origin1;
    Vector2 location2 = new Vector2(400, 400), origin2;
    Rectangle playerRectangle, obstacleRectangle;



    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        playerRectangle = Move(playerRectangle);

        base.Update(gameTime);
    }


    private Boolean IsNotCollision(Rectangle rectangle1, Rectangle rectangle2)
    {
        if (rectangle1.Intersects(rectangle2)) return true;
        else return false;
    }


    /// <summary>
    /// Sets the velocity of the targeted object based on user keyboard input
    /// </summary>
    private Rectangle Move(Rectangle rectangle)
    {
        state = Keyboard.GetState();
        xLocation = rectangle.X;
        yLocation = rectangle.Y;

        if (IsNotCollision(obstacleRectangle, playerRectangle))
        {
            if (state.IsKeyDown(Keys.Left)) rectangle.X += -4;
            if (state.IsKeyDown(Keys.Right)) rectangle.X += 4;
            if (state.IsKeyDown(Keys.Up)) rectangle.Y += -4;
            if (state.IsKeyDown(Keys.Down)) rectangle.Y += 4;
        }
        return rectangle;
    }



    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        playerRectangle = new Rectangle(0, 0, tank.Width, tank.Height);
        origin1 = new Vector2(tank.Width / 2, tank.Height);
        obstacleRectangle = new Rectangle(0, 0, blackSquare.Width, blackSquare.Height);
        origin2 = new Vector2(blackSquare.Width / 2, blackSquare.Height);

        spriteBatch.Draw(tank, location1, playerRectangle, Color.White, angle1, origin1, 1.0f, SpriteEffects.None, 1);
        spriteBatch.Draw(blackSquare, location2, obstacleRectangle, Color.White, angle2, origin2, 1.0f, SpriteEffects.None, 1); 

        spriteBatch.End();

        base.Draw(gameTime);
    }
公共类TopDownShooter:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
布莱克广场私人2 D坦克;
私人键盘状态;
私人浮动角度1=0;
私人浮动角度2=0;
私人浮动位置、浮动位置;
矢量2位置1=新矢量2(900400),原始1;
Vector2位置2=新Vector2(400400),原始2;
矩形playerRectangle、obstaclerRectangle;
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(Keyboard.GetState().IsKeyDown(Keys.Escape))
这是Exit();
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
playerRectangle=移动(playerRectangle);
更新(游戏时间);
}
私有布尔值IsNotCollision(矩形矩形1,矩形矩形2)
{
if(矩形1.相交(矩形2))返回true;
否则返回false;
}
/// 
///根据用户键盘输入设置目标对象的速度
/// 
私有矩形移动(矩形)
{
state=Keyboard.GetState();
xLocation=矩形.X;
Y位置=矩形。Y;
if(IsNotCollision(障碍物透视角、播放者透视角))
{
如果(state.IsKeyDown(Keys.Left))矩形.X+=-4;
如果(state.IsKeyDown(Keys.Right))rectangle.X+=4;
如果(state.IsKeyDown(Keys.Up))矩形.Y+=-4;
如果(state.IsKeyDown(Keys.Down))rectangle.Y+=4;
}
返回矩形;
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
playerRectangle=新矩形(0,0,坦克宽度,坦克高度);
origin1=新矢量2(油箱宽度/2,油箱高度);
obstaclerlectangle=新矩形(0,0,blackSquare.Width,blackSquare.Height);
origin2=新矢量2(blackSquare.Width/2,blackSquare.Height);
spriteBatch.Draw(坦克、位置1、播放器视角、颜色、白色、角度1、原始1、1.0f、SpriteTeffects.None、1);
spriteBatch.Draw(黑色正方形,位置2,障碍物透视角度,颜色。白色,角度2,原始2,1.0f,SpriteBeffects.None,1);
spriteBatch.End();
基础。抽签(游戏时间);
}
编辑:


实际上..你似乎从来没有更新过
位置1
..

这并没有提供问题的答案。要评论或要求作者澄清,请在他们的帖子下方留下评论。我确实有一个更大的答案..但看起来好像是OP的一个小疏忽,他们没有实际更新位置。因此,这这是我的答案。这是正确的答案。Jonheel在每一帧中都在相同的位置绘制坦克。