Collision detection on minecraft type landscape - i.e. Voxels

Started by
10 comments, last by steg 11 years ago

Hi,

I've generated a 'world' made up of voxels - think minecraft. I'm using AABB for collision. My issue is, what to do when collision occurs, so if moving forwards and collision, stop forward movement, if collision occurred while strafing left, stop left movement. I kind of did this but if I am moving forwards and collision occurs and then I turn the player around say 90 degrees and then move forward, I still can't as still thinks there is a collision...How can I sort this out, do I need some form of plane intersection or normal vector?

Any help is much appreciated. Link to video of issue below:

">

Thanks,

Steve

If it isn't working, take a bath, have a think and try again...

Advertisement
you need to push your object completely outside the voxel it's colliding with.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Push or slide?

If it isn't working, take a bath, have a think and try again...

Push or slide?

I'd take the center point of the voxel, and the center point of the colliding object, get the plane of the face you are closest colliding to, then slide against that plane. I simplify the concept, but from watching the video, and the problem you describe, it sounds like your object is "inside" the voxel, so it's constantly colliding. when you detect collision, you need to resolve it such that you are no longer actually colliding with the voxel.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Thanks for that.

So, when there is a collision, you need to resolve so there isn't one? Is this what the slide would do?

Thanks again.

If it isn't working, take a bath, have a think and try again...

Thanks for that.

So, when there is a collision, you need to resolve so there isn't one? Is this what the slide would do?

Thanks again.

Basically. You add the velocity to your object, figure out the plane of the voxel your closest to, then you figure out the closest point to that plane that does not cause an intersection with the plane. this has the added benefit of producing a sliding effect if done correctly.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

If you're moving each axis individually you may as well just do a check for each of them (easier to code, though I'm not sure how much it affects performance). You'd still have slide behavior since each axis is affected separately. Depends on how you're handling this, though.

If you happen to know how to handle collision against tiles in 2D you can apply the same logic here. Voxels are pretty much 3D tiles.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

public class CollisionLibrary {
	public static boolean testAABBAABB(final AABB box1, final AABB box2) {
		   if (Math.abs(box1.center.x - box2.center.x) > (box1.r[0] + box2.r[0])) return false;
		   if (Math.abs(box1.center.y - box2.center.y) > (box1.r[1] + box2.r[1])) return false;
		   if (Math.abs(box1.center.z - box2.center.z) > (box1.r[2] + box2.r[2])) return false;
		   return true;
		}
	
	public static float sqDistPointAABB(final Vector p, final AABB aabb) {
		   float sqDist = 0.0f;
		   float v;
		   float minX, minY, minZ, maxX, maxY, maxZ;
		      
		   // get the minX, maxX, minY, maxY and minZ, maxZ points of the AABB
		   minX = aabb.center.x - aabb.r[0];
		   maxX = aabb.center.x + aabb.r[0];
		      
		   minY = aabb.center.y - aabb.r[1];
		   maxY = aabb.center.y + aabb.r[1];
		      
		   minZ = aabb.center.z - aabb.r[2];
		   maxZ = aabb.center.z + aabb.r[2];
		      
		   // test the bounds against the points X axis
		   v = p.x;
		      
		   if (v < minX) sqDist += (minX - v) * (minX - v);
		   if (v > maxX) sqDist += (v - maxX) * (v - maxX);
		      
		   // test the bounds against the points Y axis
		   v = p.y;
		      
		   if (v < minY) sqDist += (minY - v) * (minY - v);
		   if (v > maxY) sqDist += (v - maxY) * (v - maxY);
		      
		   // test the bounds against the points Z axis
		   v = p.z;
		      
		   if (v < minZ) sqDist += (minZ - v) * (minZ - v);
		   if (v > maxZ) sqDist += (v - maxZ) * (v - maxZ);
		      
		   return sqDist;
		}
	
		public static boolean testCircleAABB(final Circle circle, final AABB box) {
		   // get the squared distance between circle center and the AABB
		   float sqDist = sqDistPointAABB(circle.center, box);
		   float r = circle.radius;
		      
		   return sqDist <= r * r;
		}
	
}
 

Thanks,

I'm using a bog standard sphere to AABB collision detection as shown in the code. The issue I'm having is what to do when a collision occurs, for instance, if I move forwards and there is a collision, I then cannot move forwards which is correct, but if I turn around say 90 degrees and then move forward, I should be able to move, but cannot. I think you need to check the surface normals or something...

If it isn't working, take a bath, have a think and try again...

That sounds like you're checking for the collision before moving (which means that as soon as you collide with something you can't move anymore as you're always colliding). I'm not sure, but just guessing out of the behavior.

I'm really suspicious about how you do the sphere/box check though... It's like you're trying to turn the box into a sphere o_O Is that correct?

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This isn't really an OpenGL question, so I'm moving you to our Maths & Physics forum. smile.png

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement