AABB - monogame解决冲突问题

时间:2015-11-21 20:03:37

标签: c# xna monogame aabb

我这里有点问题。我正在尝试解决碰撞但是当我按下按钮时,玩家会转到墙上。当我发布它的确定。 其他一切看起来都很正常,对于向上运动也是如此。 我不能让这个工作。

issue when pressed right button, when i release it is ok

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    RectangleF player_rec = new RectangleF(new Vector2(64f, 64f), new Vector2(32f, 32f));
    RectangleF bariera2_rec = new RectangleF(new Vector2(64f, 0f), new Vector2(32f, 32f));
    RectangleF bariera_rec = new RectangleF(new Vector2(64f, 128f), new Vector2(32f, 32f));
    RectangleF bariera3_rec = new RectangleF(new Vector2(128f, 64f), new Vector2(32f, 32f));

    Sprite player = new Sprite();
    Sprite kostka = new Sprite();
    Sprite bariera3 = new Sprite();
    Sprite bariera2 = new Sprite();
    Sprite bariera = new Sprite();
    KeyboardState state;


    int[,] pole = new int[10, 10]
    {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 1, 0, 0, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    };

    protected override void Initialize()
    {
        bariera.position = bariera_rec.position;
        bariera2.position = bariera2_rec.position;
        bariera3.position = bariera3_rec.position;
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        player.texture = Content.Load<Texture2D>("blank.png");
        bariera.texture = Content.Load<Texture2D>("blank2.png");
        bariera2.texture = Content.Load<Texture2D>("blank2.png");
        bariera3.texture = Content.Load<Texture2D>("blank2.png");
        kostka.texture = Content.Load<Texture2D>("kostka.png");
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        state = Keyboard.GetState();

        player.position = player_rec.position;
        player_rec.Update();

        if (state.IsKeyDown(Keys.Up))
        {
            player_rec.position.Y -= 8.2f;
        }

        if (state.IsKeyDown(Keys.Down))
        {
            player_rec.position.Y += 8.2f;
        }

        if (state.IsKeyDown(Keys.Right))
        {
            player_rec.position.X += 8.2f;
        }


        if (RectangleF.intersect(player_rec, bariera2_rec) == true)
        {
            player_rec.position.Y += Math.Abs(bariera2_rec.position_rd.Y - player_rec.position.Y);
        }

        if (RectangleF.intersect(player_rec, bariera_rec) == true)
        {
            player_rec.position.Y -= Math.Abs(player_rec.position_rd.Y - bariera_rec.position.Y);
        }

        if (RectangleF.intersect(player_rec, bariera3_rec) == true)
        {
            player_rec.position.X -= Math.Abs(bariera2_rec.position_rd.X - player_rec.position.X);
        }

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        if(RectangleF.intersect(player_rec,bariera_rec) == true)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
        }
        else
        {
            GraphicsDevice.Clear(Color.OrangeRed);
        }

        spriteBatch.Begin();
        /*
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (pole[j, i] == 1)
                {
                    kostka.position = new Vector2(i * 32, j * 32);
                    kostka.Draw(gameTime, spriteBatch);
                }
            }
        }
        */
        bariera3.Draw(gameTime, spriteBatch);

        bariera2.Draw(gameTime, spriteBatch);

        bariera.Draw(gameTime, spriteBatch);

        player.Draw(gameTime, spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

这是我的Rectangle float精度类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Game1
{
class RectangleF
{
    public Vector2 position;
    public Vector2 size;
    public Vector2 position_rd;
    public Vector2 origin;

    public RectangleF()
    {
    }

    public RectangleF(Vector2 Position, Vector2 Size)
    {
        this.position.X = Position.X;
        this.position.Y = Position.Y;
        this.size.X = Size.X;
        this.size.Y = Size.Y;
        this.position_rd.X = Position.X + Size.X;
        this.position_rd.Y = Position.Y + Size.Y;
    }

    public void Update()
    {
        this.position_rd.Y = position.Y + size.Y;
        this.position_rd.X = position.X + size.X;
        this.origin.X = position.X + size.X / 2;
        this.origin.Y = position.Y + size.Y / 2;
    }

    public static bool intersect(RectangleF rectangle_1, RectangleF rectangle_2)
    {
        if ((rectangle_1.position_rd.X > rectangle_2.position.X) && (rectangle_1.position.X < rectangle_2.position_rd.X) &&
            (rectangle_1.position_rd.Y > rectangle_2.position.Y) && (rectangle_1.position.Y < rectangle_2.position_rd.Y) == true)
        {
            return true;
        }
        else             
        {
            return false;
        }
    }

}
}

0 个答案:

没有答案