# Minecraft style XNA game collision?

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

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

• Can you give us a bit more to go on? Order of operations, even a bit of code would help out here. – James Mar 25 '12 at 17:42
• 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. – Levi H Mar 25 '12 at 17:55
• Possible duplicate: gamedev.stackexchange.com/questions/19198/… – MichaelHouse Mar 26 '12 at 1:48
• @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. – Joshua Drake Apr 26 '12 at 15:10
• @JoshuaDrake the problem is actually the same – Gustavo Maciel May 4 '12 at 10:04