Minecraft style XNA game collision?

Posted by Levi on Game Development See other posts from Game Development or by Levi
Published on 2012-03-25T17:40:51Z Indexed on 2012/03/25 23:42 UTC
Read the original article Hit count: 299

Filed under:
|
|
|

I've been trying to get this working for ages now, I can detect if there's a solid block at any place on the map and I can check how far something is inside of it, but I don't understand how to fix the collision. I've tried loads of ways and all of them end up by the player getting stuck, glitching around, incorrect responses and I really have no idea how to go about this :/.

       int Chnk = Utility.GetChunkFromPosition(origin);
        if (Chnk == -1)
            return;

        Vector3 Pos = Utility.GetCubeVectorFromPosition(origin);

        if (GlobalWorld.LoadedChunks[Chnk].Blocks[(byte)Pos.X, (byte)Pos.Y, (byte)Pos.Z] != 0)
        {
            isInIllegalState = true;
            if (velocity.Y < 0f)
                velocity.Y = 0f;
        }

        while (isInIllegalState)
        {
            if (GlobalWorld.LoadedChunks[Chnk].Blocks[(byte)Pos.X, (byte)origin.Y, (byte)Pos.Z] != 0)
                origin.Y = (int)(origin.Y + 1);
            else isInIllegalState = false;
        }

        if (origin.Y < Chunk.YSize - 2 && GlobalWorld.LoadedChunks[Chnk].Blocks[(byte)Pos.X, (byte)(origin.Y + playerHeight.Y), (byte)Pos.Z] != 0)
        {
            velocity.Y = 0f;

            //Acceleration.Y = 0f;

            origin.Y = (int)origin.Y;// -0.5f;
        }



        for (int x = -1; x <= 1; x+=2)
        {
            for (int z = -1; z <= 1; z += 2)
            {
                Vector3 CornerPosition = new Vector3(boundingSize * x, 0, boundingSize * z);
                bool CorrectX = false;
                bool CorrectZ = false;

                Vector3 RoundedOrigin = Utility.RoundVector(origin);
                Vector3 RoundedCorner = Utility.RoundVector(origin + CornerPosition);

                byte BlockAdjacent = Utility.GetCubeFromPosition(origin + CornerPosition);

                if (BlockAdjacent == 0)
                    continue;

                if (RoundedCorner.X != RoundedOrigin.X && RoundedCorner.Z != RoundedOrigin.Z)
                {
                    CorrectX = true;
                    CorrectZ = true;
                }

                if (RoundedCorner.Z != RoundedOrigin.Z && RoundedCorner.X == RoundedOrigin.X)
                    CorrectZ = true;
                if (RoundedCorner.X != RoundedOrigin.X && RoundedCorner.Z == RoundedOrigin.Z)
                    CorrectX = true;

                if (CorrectX && CornerPosition.X > 0)
                {
                    if (origin.X > 0f)
                        origin.X = (int)(origin.X + 1) - boundingSize;
                    else origin.X = (int)origin.X - boundingSize;
                }
                else if (CorrectX && CornerPosition.X < 0)
                {
                    if (origin.X > 0f)
                        origin.X = (int)(origin.X) + boundingSize;
                    else origin.X = (int)(origin.X - 1) + boundingSize;
                }

                if (CorrectZ && CornerPosition.Z > 0)
                {
                    if (origin.Z > 0f)
                        origin.Z = (int)(origin.Z + 1) - boundingSize;
                    else origin.Z = (int)origin.Z - boundingSize;
                }
                else if (CorrectZ && CornerPosition.Z < 0)
                {
                    if (origin.Z > 0f)
                        origin.Z = (int)(origin.Z) + boundingSize;
                    else origin.Z = (int)(origin.Z - 1) + boundingSize;
                }
            }
        }

© Game Development or respective owner

Related posts about XNA

Related posts about c#