• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
steg

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

11 posts in this topic

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:

 

https://www.youtube.com/watch?v=bbxRRBDBWRQ

 

Thanks,

Steve

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites
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...

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

Hi,

 

The sphere/AABB is working fine, no issue with that.  I'm checking for collision before moving, could that be part of the problem? The code

is shown:

 


CollisionType coll = this.isCollision(0);

if (Keyboard.isKeyDown(Keyboard.KEY_W))// move forwards
			{
				direction = 0;

				if (coll != CollisionType.FRONT) {
					camera.walkBackwards(.1f);
				}

			}

 

I am really surprised how difficult this is proving to me!

 

This is the isCollision method:

 

private CollisionType isCollision(int dir) {

		Vector3f v = this.camera.getCameraPosition();

		float tempx = (v.x) / BLOCKWIDTH * BLOCKHEIGHT;
		int tempX = (int) tempx;

		float tempz = (v.z) / BLOCKWIDTH * BLOCKHEIGHT;
		int tempZ = (int) tempz;

		float tempy = (v.y) / BLOCKWIDTH * BLOCKHEIGHT;
		int tempY = (int) tempy;

		int bx = (int) tempX;
		int by = (int) tempY;
		int bz = (int) tempZ;

		bx = bx % BLOCKWIDTH;
		bz = bz % BLOCKHEIGHT;
		by = by % BLOCKHEIGHT;

		int zchunk = Math.abs(tempZ / Chunk.CHUNK_SIZE) * WORLDWIDTH;
		int xchunk = Math.abs(tempX / Chunk.CHUNK_SIZE);
		int chunktoget = zchunk + xchunk;

		CollisionType collisionType = CollisionType.NONE;

		Block b = null;
		b = ChunkManager.getInstance().getBlockInChunk(chunktoget,
				Math.abs(bx), (int) Math.abs(by), Math.abs(bz));

		// Check ground
		if (b != null && b.IsActive()) {
			// playerSphere radius is 1
			Vector playerPosition = new Vector();
			playerPosition.x = Math.abs(v.x);
			playerPosition.y = Math.abs(v.y);
			playerPosition.z = Math.abs(v.z);
			playerSphere.update(playerPosition, 1.0f);

			AABB voxel = new AABB(1f, 1f, 1f); // width, height, depth
			Vector voxelPosition = new Vector();
			voxelPosition.x = Math.abs(bx);
			voxelPosition.y = Math.abs(by);
			voxelPosition.z = Math.abs(bz);
			voxel.update(voxelPosition);

			if (CollisionLibrary.testCircleAABB(playerSphere, voxel)) {
				collisionType = CollisionType.GROUND;
				System.out.println("GROUND COLLISION");
				groundLevel = (int) voxelPosition.y;
			}
		}

		// Check chunk for any collisions - should have early bail out
		for (int x = 0; x < Chunk.CHUNK_SIZE; x++) {
			for (int y = this.ground; y < Chunk.CHUNK_SIZE; y++) {
				for (int z = 0; z < Chunk.CHUNK_SIZE; z++) {
					b = ChunkManager.getInstance().getBlockInChunk(chunktoget,
							x, y, z);

					if (b != null && b.IsActive()) {

						Vector playerPosition = new Vector();
						playerPosition.x = Math.abs(v.x);
						playerPosition.y = Math.abs(v.y);
						playerPosition.z = Math.abs(v.z);
						playerSphere.update(playerPosition, .8f);

						AABB voxel = new AABB(1f, 1f, 1f); // width, height,
															// depth
						Vector voxelPosition = new Vector();
						voxelPosition.x = x;
						voxelPosition.y = y;
						voxelPosition.z = z;
						voxel.update(voxelPosition);

						if (CollisionLibrary
								.testCircleAABB(playerSphere, voxel)) {
							System.out.println("COLLISION on Direction:"
									+ direction);
							if (direction == 0) {
								collisionType = CollisionType.FRONT;
							}
							if (direction == 1) {
								collisionType = CollisionType.BACK;
							}
							if (direction == 2) {
								collisionType = CollisionType.LEFT;
							}
							if (direction == 3) {
								collisionType = CollisionType.RIGHT;
							}
							return collisionType;
						}
					}
				}
			}
		}

		return collisionType;
	}

 

 

Thanks for your input and advice, I really appreciate it.

0

Share this post


Link to post
Share on other sites

Maybe I need to do a dot product on the surface I've collided with?

Edited by steg
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0