【问题标题】:Collision Block Movement碰撞块运动
【发布时间】:2014-01-11 14:49:58
【问题描述】:

我在阻止移动时遇到问题我已经阅读了Blocking Movement On Collision 上的问题 但我对自己的问题一无所知。 如果你能给我逻辑,我会尝试解决我自己的问题。 我希望你能帮帮我 这是我的播放器类更新

:编辑:感谢 Kai Hartmann 的回复 我的问题是 2D 图形,我想知道当 object1 与 object2 碰撞时如何停止移动

public void Update(GameTime gameTime)
{
    // Ani Test
    up_ani.Update(gameTime);
    down_ani.Update(gameTime);
    left_ani.Update(gameTime);
    right_ani.Update(gameTime);

    position += velocity;

    if (Keyboard.GetState().IsKeyDown(key.MoveUp) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft))
    {
        currentFace = FacePosition.Up;
        velocity.Y = -3;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft))
    {
        currentFace = FacePosition.Down;
        velocity.Y = 3;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveRight) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveLeft))
    {
        currentFace = FacePosition.Right;
        velocity.X = 3;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveLeft) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveUp))
    {
        currentFace = FacePosition.Left;
        velocity.X = -3;
    }
    else
    {
        //currentFace = FacePosition.Down;
        velocity = Vector2.Zero;
    }

    prevState = Keyboard.GetState();
}

这是我的碰撞

public void IntersectWithScore(Vector2 vector1, Vector2 vector2, Texture2D object1, Texture2D object2, int doSomethingToScore, bool isIncrease)
{
    if (vector1.X + object1.Width < vector2.X || vector1.X > vector2.X + object2.Width ||
    vector1.Y + object1.Height < vector2.Y || vector1.Y > vector2.Y + object2.Height)
    {

    }
    else
    {
        player1.Velocity = Vector2.Zero;
    }
}

【问题讨论】:

  • 你没有说你的问题到底是什么。还有,这是3D还是2D?

标签: c# xna 2d collision


【解决方案1】:

就我而言,不需要碰撞检查方法 :) 我已经很快编写了这段代码,但应该可以正常工作吗?(ish)。我假设你想检查玩家的方式是否有东西,并且还假设有不止一个对象可能在你的玩家的方式中,所以我建议为“Solid”对象创建一个类并给它位置、宽度和高度。然后列出这个类以包含多个“实体”对象。

// collidable object's Class name = SolidBlock
SolidBlock newSolidBlock;
List<SolidBlock> solidBlocksList = new List<SolidBlock>();

// This will create 10 'solid' objects and adds them all to a list
// This would preferably go in the Load() function so you can set the positions of
// the blocks after creating them (after the loop)
int i = 0;
while (i < 10)
{
    newSolidBlock = new SolidBlock(Vector2, position) // Assuming width and height 
                                                      // are set within class
    solidBlocksList.Add(newSolidBlock);
    i++;
}

// It doesn't matter how you create them, the important lines here are the creation of a
// newSolidBlock and then the line adding it to the list of blocks.

solidBlocksList[0].position = new Vector (20, 50); // < for example this is how you would set
                                                   // a block's position.

然后在你的玩家更新函数中你引用这个对象列表。

// Input a list of the objects you want your player to collide with
// this is a list in case you have multiple object2s you want to check 
// for collision.
// An Object2 should have a position, height, and width, yes?

public void Update(GameTime gameTime, List<Object2> object2List)
{
    // Ani Test
    up_ani.Update(gameTime);
    down_ani.Update(gameTime);
    left_ani.Update(gameTime);
    right_ani.Update(gameTime);

    position += velocity;

    if (Keyboard.GetState().IsKeyDown(key.MoveUp) && prevState.IsKeyUp(key.MoveDown) &&      prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft))
    {
        foreach (Object2 o in object2List)
        {
            if (position.Y <= o.position.Y + o.height)
            {
                velocity.Y = 0;
            }
            else
            {
                velocity.Y = -3;
            }
        } 
        currentFace = FacePosition.Up;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) &&     prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft))
    {
        foreach (Object2 o in object2List)
        {
            if (position.Y + playerWidth >= o.position.Y)
            {
                velocity.Y = 0;
            }
            else
            {
                velocity.Y = 3;
            }
        } 
        currentFace = FacePosition.Down;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveRight) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveLeft))
    {
        foreach (Object2 o in object2List)
        {
            if (position.X + playerWidth >= o.position.X)
            {
                velocity.X = 0;
            }
            else
            {
                velocity.X = 3;
            }
        } 
        currentFace = FacePosition.Right;
    }
    else if (Keyboard.GetState().IsKeyDown(key.MoveLeft) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveUp))
    {
        foreach (Object2 o in object2List)
        {
            if (position.X <= o.position.X + o.width)
            {
                velocity.X = 0;
            }
            else
            {
                velocity.X = -3;
            }
        } 
        currentFace = FacePosition.Left;
    }
    else
    {
        velocity = Vector2.Zero;
    }

    prevState = Keyboard.GetState();
}

很抱歉,如果这没有多大帮助,尤其是第一段代码,那是您如何创建多个 object2s 的一个很好的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多