0
votes

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

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;
    }
}
1
You did not say what your problem exactly is. Also, is this 3D or 2D?Kai Hartmann

1 Answers

0
votes

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