简体   繁体   English

碰撞块运动

[英]Collision Block Movement

i have problem with blocking movement i already read question on Blocking Movement On Collision but i dont have any idea for my own problem. 我在阻止运动方面有问题,我已经阅读了关于在碰撞中阻止运动的问题,但是我对自己的问题一无所知。 if you can give me the logic i will try to solve my own problem. 如果您能给我逻辑,我将尝试解决自己的问题。 i hope you can help me this my player class update 我希望你能帮我这个我的球员更新

:EDIT: thanks for Kai Hartmann for reply my problem is for 2D graphic and i want to know how to stop movement when object1 collision with object2 :EDIT:感谢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();
}

and this my collision 这是我的碰撞

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;
    }
}

No need for a collision check Method as far as I'm concerned :) I've written this code really quickly but should work okay?(ish). 就我而言,不需要冲突检查方法:)我已经非常快地编写了这段代码,但是应该可以吗? I've assumed you want to check if something is in the player's way and also assumed that there are more than one objects that could be in your players way so to do this I suggest making a class for the 'Solid' object and giving it a position, width and height. 我假设您要检查玩家的方式是否有问题,并且还假设您的玩家方式中可能有多个物体,所以为此,我建议为“ Solid”物体制作一个类并给它位置,宽度和高度。 Then make a list of this class to contain the multiple 'solid' objects. 然后列出此类以包含多个“实体”对象。

// 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.

Then in your players update function you refer to this list of objects. 然后,在播放器更新功能中,您将引用此对象列表。

// 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();
}

Sorry if this isn't much help, especially the first block of code, that was very much an example of how you could create multiple object2s. 抱歉,如果没有太大帮助,尤其是第一段代码,那非常说明如何创建多个object2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM