XNA 4.0寻路算法问题

3
我正在进行一个个人项目,对于XNA和C#我还很陌生(我有一些Visual Basic和C++的经验)。这个项目只是在我开始制作真正的游戏之前测试代码和算法。
我的问题是,我想在按下“Enter”键时在地图上生成一个球体,但我不希望球体生成在另一个球体或玩家上面,因为我只有一个非常基本的碰撞检测系统。
具体问题在于,即使应该只能在原点找到空间,但查找空间仍会返回一些奇怪的矩形。而且有时它会在另一个球体上方生成一个球体。如果我将玩家移动到球体里,这将导致崩溃。
这就是问题所在的地方 -
     if (currentKeyState.IsKeyDown(Keys.Enter) && newBallDelay == 0)
        {
            Npcs newBall = new Npcs();
            //find room
            Rectangle rect;
            if (findRoom(newBall, ref rect))
            {
                newBall.postion = new Vector2(rect.X, rect.Y);
                characters.Add(newBall); //adds to a list of Npcs which is drawn in a foreach loop
                newBallDelay = 1; //prevents from adding too many spheres at once
            }
        }

这个调用

 bool findRoom(Npcs newObject, ref Rectangle rectObject)
    {
        Rectangle player = new Rectangle((int)spritelocation.X, (int)spritelocation.Y, (int)sprite.Bounds.Width/4, (int)sprite.Bounds.Height/4);
        Rectangle check;
        for (int i = 0; i < characters.Count; i++)
        {
            check = new Rectangle((int)characters[i].postion.X, (int)characters[i].postion.Y, (int)characters[i].size.X, (int)characters[i].size.Y);
            for (int j = 0; j < 50; j++)
            {
                rectObject = new Rectangle(i * (int)newObject.size.X * 2, j * (int)newObject.size.Y * 2, (int)newObject.size.X, (int)newObject.size.Y); //size.X and size.Y are the height and width of the object, this creates rectangular grid to check through.
                if (!rectObject.Intersects(player) && !rectObject.Intersects(check))
                    return (true);
            }
        }

        return (false);
    }

我觉得这可能没有关系,但是关于newballdelay,我在更新中有以下内容

        if (newBallDelay > 0)
        {
            newBallDelay++;
            if (newBallDelay == 50)
                newBallDelay = 0;
        }

感谢您抽出时间查看。我还是新手,对于编码也不太熟悉,如果您有任何提示或建议,将不胜感激。
编辑:修复了内部循环。检查和递增的变量应该是j而不是i。但问题仍然存在。
编辑2:FindRoom算法似乎无论位置是否有东西都会返回两个位置中的一个。
编辑3:发现了一个小问题,精灵的矩形高度和宽度必须除以4才能正确检测精灵。
以下是我的整个代码,如果有帮助的话(抱歉如果代码有点混乱,不符合规范,我还在学习中)。
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D sprite;
    Texture2D background;
    Texture2D sphere;

    bool walking = false;
    int walkSpeed = 2;
    int newBallDelay = 0;
    //bool jump = false;

    int runSpeed = 5;
    int frame = 0;
    Random rand = new Random();

    int walkdir = 3; //0 = down, 1 = left, 2 = right, 3 = up
    Vector2 spritelocation = new Vector2(0,0);

    int imageH = 0;
    int imageW = 0;
    float elapsed = 0;

    private const int Frames = 4;
    private float frameSpeed = 0.15f;

    List<Npcs> characters = new List<Npcs>();
    Npcs ball = new Npcs();
    Npcs ball2 = new Npcs();
    Camera2d cam = new Camera2d();

    private SpriteBatch batch;
    SpriteFont Font1;
    Vector2 FontPos;

    public Game1()
    {

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

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        IsMouseVisible = true;
        Window.AllowUserResizing = true;
        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        ball.size = (new Vector2(50, 50));
        ball.postion = (new Vector2(50, 50));
        characters.Add(ball);

        ball2.size = (new Vector2(50, 50));
        ball2.postion = (new Vector2(160, 80));
        characters.Add(ball2);
        spriteBatch = new SpriteBatch(GraphicsDevice);
        sprite = Content.Load<Texture2D>("sprite\\scaled");
        background = Content.Load<Texture2D>("sprite\\grass");
        sphere = Content.Load<Texture2D>("sprite\\ball");
        imageW = sprite.Bounds.Width;
        imageH = sprite.Bounds.Height;
        cam.Pos = new Vector2(350, 50);
        //cam.Rotation = 0.5f;
        // cam.Zoom = 2.0f // Example of Zoom in
        // cam.Zoom = 0.5f // Example of Zoom out

        spriteBatch = new SpriteBatch(GraphicsDevice);
        Font1 = Content.Load<SpriteFont>("LucidaConsole");

        batch = new SpriteBatch(this.graphics.GraphicsDevice);

        FontPos = new Vector2(graphics.GraphicsDevice.Viewport.Width - 90, 20);
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        getInput();
        // TODO: Add your update logic here

        elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

        //Delay for ball
        if (newBallDelay > 0)
        {
            newBallDelay++;
            if (newBallDelay == 50)
                newBallDelay = 0;
        }

        checkCollsion();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.PreferredBackBufferWidth = 1366;
        graphics.PreferredBackBufferHeight = 728;
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        //// if using XNA 4.0
        spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend,null,null,null,null, cam.get_transformation(null));

        //spriteBatch.Draw(background, new Vector2(-2000,-2000), new Rectangle(0,0,4000,4000), Color.White);

        for (int i = 0; i < characters.Count(); i++ )
        {
            spriteBatch.Draw(sphere, characters[i].postion, new Rectangle(0, 0, (int)Math.Round(characters[i].size.X), (int)Math.Round(characters[i].size.Y)), Color.White);
        }

        elapsed += (int)gameTime.ElapsedGameTime.TotalSeconds;
        while (elapsed > frameSpeed && walking)
        {
            frame++;
            elapsed = 0;
            frame = frame % Frames;
        }

        if (!walking)
            spriteBatch.Draw(sprite, spritelocation, new Rectangle(0, walkdir * (imageH / 4), (imageW / 4), (imageH / 4)), Color.White);
        else
            spriteBatch.Draw(sprite, spritelocation, new Rectangle(frame * (imageW / 4), walkdir * (imageH / 4), (imageW / 4), (imageH / 4)), Color.White);

        spriteBatch.End();

        base.Draw(gameTime);

    }


    protected void getInput() //Recieves keyboard input
    {
    KeyboardState currentKeyState = Keyboard.GetState();
        if (currentKeyState.IsKeyDown(Keys.Up))
        {
            walkdir = 3;
            walking = true;
            if ((currentKeyState.IsKeyDown(Keys.LeftShift)))
            {
                frameSpeed = 0.05f;
                spritelocation.Y -= runSpeed;
                cam.Move(new Vector2(0, -runSpeed));
            }
            else
            {
                frameSpeed = 0.15f;
                spritelocation.Y -= walkSpeed;
                cam.Move(new Vector2(0, -walkSpeed));
            }
        }
        else if (currentKeyState.IsKeyDown(Keys.Down))
        {
            walkdir = 0;
            walking = true;
            if ((currentKeyState.IsKeyDown(Keys.LeftShift)))
            {
                frameSpeed = 0.05f;
                spritelocation.Y += runSpeed;
                cam.Move(new Vector2(0, runSpeed));
            }
            else
            {
                frameSpeed = 0.15f;
                spritelocation.Y += walkSpeed;
                cam.Move(new Vector2(0, walkSpeed));
            }
        }
        else if (currentKeyState.IsKeyDown(Keys.Right))
        {
            walkdir = 2;
            walking = true;
            if ((currentKeyState.IsKeyDown(Keys.LeftShift)))
            {
                frameSpeed = 0.05f;
                spritelocation.X += runSpeed;
                cam.Move(new Vector2(runSpeed, 0));
            }
            else
            {
                frameSpeed = 0.15f;
                spritelocation.X += walkSpeed;
                cam.Move(new Vector2(walkSpeed, 0));
            }
        }
        else if (currentKeyState.IsKeyDown(Keys.Left))
        {
            walkdir = 1;
            walking = true;
            if ((currentKeyState.IsKeyDown(Keys.LeftShift)))
            {
                frameSpeed = 0.05f;
                spritelocation.X -= runSpeed;
                cam.Move(new Vector2(-runSpeed, 0));
            }
            else
            {
                frameSpeed = 0.15f;
                spritelocation.X -= walkSpeed;
                cam.Move(new Vector2(-walkSpeed, 0));
            }
        }
        else
        {
            walking = false;
            frameSpeed = 1f;
        }

        if (currentKeyState.IsKeyDown(Keys.Enter) && newBallDelay == 0)
        {
            Npcs newBall = new Npcs();
            //find room
            Rectangle rect = new Rectangle(0,0,(int)newBall.size.X,(int)newBall.size.Y);
            if (findRoom(newBall, ref rect))
            {
                newBall.postion = new Vector2(rect.X, rect.Y);
                characters.Add(newBall);
                newBallDelay = 1;
            }
        } //if enter key is hit
    } //get input


 bool findRoom(Npcs newObject, ref Rectangle rectObject)
 {
    Rectangle player = new Rectangle((int)spritelocation.X, (int)spritelocation.Y,     (int)sprite.Bounds.Width/4, (int)sprite.Bounds.Height/4);
    Rectangle check;
    for (int i = 0; i < characters.Count; i++)
    {
        check = new Rectangle((int)characters[i].postion.X, (int)characters[i].postion.Y, (int)characters[i].size.X, (int)characters[i].size.Y);
        for (int j = 0; j < 50; j++)
        {
            rectObject = new Rectangle(i * (int)newObject.size.X * 2, j * (int)newObject.size.Y * 2, (int)newObject.size.X, (int)newObject.size.Y); //size.X and size.Y are the height and width of the object, this creates rectangular grid to check through.
            if (!rectObject.Intersects(player) && !rectObject.Intersects(check))
                return (true);
        }
    }

    return (false);
}


    void checkCollsion(Npcs character, int index)
    {
        Rectangle char1 = new Rectangle((int)character.postion.X, (int)character.postion.Y, (int)character.size.X, (int)character.size.Y);
        Rectangle char2 = new Rectangle();
        for (int i = 0; i < characters.Count(); i++)
        {
            if (i != index)
            {
                char2 = new Rectangle((int)characters[i].postion.X, (int)characters[i].postion.Y, (int)characters[i].size.X, (int)characters[i].size.Y);
                if (char1.Intersects(char2))
                {
                    characters[i].hit(walkdir, (int)character.speed);
                    checkCollsion(characters[i], i);
                }
            }
        }
        character.speed = 0.0f;
    }

    void checkCollsion()
    {
        Rectangle char1 = new Rectangle((int)spritelocation.X, (int)spritelocation.Y, (int)sprite.Bounds.Width/4 - 10, (int)sprite.Bounds.Height/4 - 15);
        Rectangle char2 = new Rectangle();
        for (int i = 0; i < characters.Count(); i++)
        {
                char2 = new Rectangle((int)characters[i].postion.X, (int)characters[i].postion.Y, (int)characters[i].size.X, (int)characters[i].size.Y);
                if (char1.Intersects(char2))
                {
                    characters[i].hit(walkdir, runSpeed);

                    checkCollsion(characters[i], i);
                }

        }
    }
}
3个回答

3

我不再看到任何可能的错误,但是我不知道上下文。

我认为最好的方法是逐步调试,可能将重要的数字记录到文件中,这样您可以快速查看一些异常行为...


那只是矩形开始和检查的X,Y部分,不应该引起问题。 - Denora
你的内部循环正在初始化<code>int j = 0</code>,但使用<code>i++</code>。这样做不会超过<code>characters</code>数组大小吗? - kailoon
我认为汤姆的评论可能仍然是问题所在。 - kailoon
@Denora:左右应该是X,上下应该是Y,但你有三个X而只有一个Y。因此你的矩形是不正确的... - Tamara Wijsman
1
这些仅仅是检查的坐标,它们是否相同并不重要。一个被i乘以,另一个被j乘以,形成了一种网格来进行检查。我使用size.X(对象的宽度)作为默认值。为了论证,我将其更改为size.Y,问题仍然存在。 - Denora

0
你的内部循环正在初始化 int j = 0,但使用了 i++。这样会不会超过 characters 数组的大小?这可能是问题所在。

0

好的,我从头开始重新设计了我的碰撞代码,以解决渲染findRoom无意义的问题。我还使用随机变量来确定球体的生成点。感谢大家的帮助,非常感激。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接