public void CollideBoxBox_CollideTime_Diagonal() { float collideTime; Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(-20, -20, 0), 1, 1, 1), new Vector3(20, 20, 0), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(20, -20, 0), 1, 1, 1), new Vector3(-20, 20, 0), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(-20, 20, 0), 1, 1, 1), new Vector3(20, -20, 0), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(20, 20, 0), 1, 1, 1), new Vector3(-20, -20, 0), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, -20, -20), 1, 1, 1), new Vector3(0, 20, 20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 20, -20), 1, 1, 1), new Vector3(0, -20, 20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, -20, 20), 1, 1, 1), new Vector3(0, 20, -20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 20, 20), 1, 1, 1), new Vector3(0, -20, -20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(-20, 0, -20), 1, 1, 1), new Vector3(20, 0, 20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(20, 0, -20), 1, 1, 1), new Vector3(-20, 0, 20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(-20, 0, 20), 1, 1, 1), new Vector3(20, 0, -20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(20, 0, 20), 1, 1, 1), new Vector3(-20, 0, -20), new Box3D(Vector3.Zero, 20, 20, 20), 0, out collideTime)); Assert.AreEqual(0.475, collideTime, 0.0001); }
public void CollideBoxBox_UnitDir_NoCollide() { Assert.IsFalse(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), Vector3.Zero, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsFalse(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), Vector3.UnitX, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsFalse(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), -Vector3.UnitX, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsFalse(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), Vector3.UnitY, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsFalse(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), -Vector3.UnitY, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); }
public void CollideBoxBox_UnitDir_Collide() { Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(-100, 0, 0), 1, 1, 1), Vector3.UnitX * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(100, 0, 0), 1, 1, 1), -Vector3.UnitX * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, -100, 0), 1, 1, 1), Vector3.UnitY * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 100, 0), 1, 1, 1), -Vector3.UnitY * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 0, -100), 1, 1, 1), Vector3.UnitZ * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 0, 100), 1, 1, 1), -Vector3.UnitZ * 100, new Box3D(Vector3.Zero, 20, 20, 20), 0, out _)); }
public void CollideBoxBox_ReturnedSide_UnitDir() { Vector3 collisionNormal; Vector3 collisionEdgeUp; Vector3 collisionEdgeSide; Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(0, 0, 0), 2, 2, 2), new Vector3(100, 0, 0), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(-1, 0, 0), collisionNormal); Assert.AreEqual(new Vector3(0, 0, 1), collisionEdgeSide); Assert.AreEqual(new Vector3(0, 1, 0), collisionEdgeUp); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(200, 0, 0), 2, 2, 2), new Vector3(-100, 0, 0), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(1, 0, 0), collisionNormal); Assert.AreEqual(new Vector3(0, 0, 1), collisionEdgeSide); Assert.AreEqual(new Vector3(0, 1, 0), collisionEdgeUp); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(100, 100, 0), 2, 2, 2), new Vector3(0, -100, 0), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(0, 1, 0), collisionNormal); Assert.AreEqual(new Vector3(0, 0, 1), collisionEdgeSide); Assert.AreEqual(new Vector3(1, 0, 0), collisionEdgeUp); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(100, -100, 0), 2, 2, 2), new Vector3(0, 100, 0), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(0, -1, 0), collisionNormal); Assert.AreEqual(new Vector3(0, 0, 1), collisionEdgeSide); Assert.AreEqual(new Vector3(1, 0, 0), collisionEdgeUp); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(100, 0, 100), 2, 2, 2), new Vector3(0, 0, -100), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(0, 0, 1), collisionNormal); Assert.AreEqual(new Vector3(1, 0, 0), collisionEdgeSide); Assert.AreEqual(new Vector3(0, 1, 0), collisionEdgeUp); Assert.IsTrue(Collision3D.CollideBoxBox(new Box3D(new Vector3(100, 0, -100), 2, 2, 2), new Vector3(0, 0, 100), new Box3D(new Vector3(100, 0, 0), 20, 20, 20), 0, out _, out collisionNormal, out collisionEdgeUp, out collisionEdgeSide)); Assert.AreEqual(new Vector3(0, 0, -1), collisionNormal); Assert.AreEqual(new Vector3(1, 0, 0), collisionEdgeSide); Assert.AreEqual(new Vector3(0, 1, 0), collisionEdgeUp); }
private void VoxelizeObject(SimpleMesh mesh) { VoxelArea va = VoxelArea; for (int i = 0; i < mesh._triangles.Length; i += 3) { uint min_x, min_y, min_z, max_x, max_y, max_z; Vector3 tri1 = mesh._matrix.MultiplyPoint3x4(mesh._vertices[mesh._triangles[i]]); Vector3 tri2 = mesh._matrix.MultiplyPoint3x4(mesh._vertices[mesh._triangles[i + 1]]); Vector3 tri3 = mesh._matrix.MultiplyPoint3x4(mesh._vertices[mesh._triangles[i + 2]]); Vector3 minPos = Vector3.Min(Vector3.Min(tri1, tri2), tri3); Vector3 maxPos = Vector3.Max(Vector3.Max(tri1, tri2), tri3); if (!va.GetCellCount(minPos, out min_x, out min_y, out min_z)) { Debug.Log("bound min err"); return; } if (!va.GetCellCount(maxPos, out max_x, out max_y, out max_z)) { Debug.Log("bound max err"); return; } for (uint z = (uint)min_z; z <= max_z; ++z) { for (uint y = (uint)min_y; y <= max_y; ++y) { for (uint x = (uint)min_x; x <= max_x; ++x) { uint index; if (!va.GetCellIndex(out index, x, y, z)) { continue; } // 이미 검출되었고, 위를 향한 면이라서 더 검출을 할 필요가 없다. if (va.Voxels.ContainsKey(index)) { BuildSpan(index); continue; } Vector3 position; if (!va.GetCellPosition(x, y, z, out position)) { continue; } AABox box = new AABox(position, new Vector3(va.CellSize, va.CellHeight, va.CellSize)); Triangle triangle = new Triangle(tri1, tri2, tri3); bool reverseSide = false; if (Collision3D.CollisionDetectTriangle(triangle, box, out reverseSide, Vector3.up)) //if(Collision3D.triBoxOverlap(position, new Vector3(va.CellSize, va.CellHeight, va.CellSize)*0.5f, triangle)) // 이게 더 빠르다.. 근데 아직 미완성. { va.Voxels[index] = new Voxel(index); if (reverseSide) { va.Voxels[index].FaceReverse = true; } BuildSpan(index); } } } } } }