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

Started by
10 comments, last by steg 11 years ago

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.

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

Advertisement

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

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

This topic is closed to new replies.

Advertisement