4
\$\begingroup\$

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;
                }
            }
        }
\$\endgroup\$
5
  • \$\begingroup\$ Can you give us a bit more to go on? Order of operations, even a bit of code would help out here. \$\endgroup\$
    – James
    Commented Mar 25, 2012 at 17:42
  • \$\begingroup\$ I keep deleting the code and starting over, I've been using 4 points around the bottom of the player in a square. First I check if the player's feet are intersecting the block under them and if they are I move them to the next block up and repeat this until it's successful. Then I check the blocks at each of the 4 points around the player and attempt to correct it, the code at the moment is really messed up from me trying to fix it but I'll add it to the post. There is loads of issues with it like the wrong movement being applied, just loads. \$\endgroup\$
    – Levi H
    Commented Mar 25, 2012 at 17:55
  • 1
    \$\begingroup\$ Possible duplicate: gamedev.stackexchange.com/questions/19198/… \$\endgroup\$
    – House
    Commented Mar 26, 2012 at 1:48
  • \$\begingroup\$ @Byte56 isn't this a more general version, the linked question appears to be dealing explicitly with the ground plane while this one looks to deal with all objects on the map. \$\endgroup\$ Commented Apr 26, 2012 at 15:10
  • \$\begingroup\$ @JoshuaDrake the problem is actually the same \$\endgroup\$ Commented May 4, 2012 at 10:04

1 Answer 1

1
\$\begingroup\$

If you're still stuck on this issue, I've written a short "guide" on collision for voxel engines.

http://thewebsiteabout.me/all-about-voxel-engines-part-3-view/

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .