public void HandleExcavationMouseover(Ray hoverRay) { Vector3?space = getLastSpaceAlongRay(hoverRay); Vector3?block = getBlockAlongRay(hoverRay); if (space.HasValue && block.HasValue) { IntVector3 spaceLoc = new IntVector3((Vector3)space); IntVector3 blockLoc = new IntVector3((Vector3)block); Vector3 pointingTowardBlock = blockLoc.toVector3() - spaceLoc.toVector3(); CubeAnimator.AnimatedBodyPartGroup flag = new CubeAnimator.AnimatedBodyPartGroup(@"worldMarkup\shovel.chr", 1f / 26f); flag.setRootPartLocation(spaceLoc.toVector3() + new Vector3(.5f, .5f, .5f)); flag.setRootPartRotationOffset(Quaternion.CreateFromRotationMatrix(GeometryFunctions.getRotationMatrixFromNormal(pointingTowardBlock))); Compositer.addAnimatedBodyPartGroupForThisFrame(flag); } }
Vector3?rayTileHitsViaModernaInSpaceContext(bool wantNearestHit, float range, byte[, ,] array, Vector3 firstRef, Vector3 secondRef) { List <Vector3> intersected; intersected = new List <Vector3>(); int xMin, xMax, yMin, yMax, zMin, zMax; if (!System.Single.IsNaN(range)) { xMin = (int)(firstRef.X - range); xMax = (int)(firstRef.X + range); yMin = (int)(firstRef.Y - range); yMax = (int)(firstRef.Y + range); zMin = (int)(firstRef.Z - range); zMax = (int)(firstRef.Z + range); } else //it's Nan { xMin = 0; xMax = spaceWidth; yMin = 0; yMax = spaceHeight; zMin = 0; zMax = spaceWidth; } for (int a = xMin; a < xMax; a += 10) { for (int b = yMin; b < yMax; b += 10) { for (int c = zMin; c < zMax; c += 10) { if (withinSpace(new Vector3(a, b, c))) { Vector3 wat = new Vector3(0, 0, 0); if (GeometryFunctions.CheckLineBox(new Vector3(a, b, c), new Vector3(a + 10, b + 10, c + 10), secondRef, firstRef, ref wat)) { for (int x = a; x < a + 10; x++) { for (int y = b; y < b + 10; y++) { for (int z = c; z < c + 10; z++) { if (withinSpace(new Vector3(x, y, z))) { Vector3 lul = new Vector3(0, 0, 0); if (GeometryFunctions.CheckLineBox(new Vector3(x, y, z), new Vector3(x + 1, y + 1, z + 1), secondRef, firstRef, ref lul)) { intersected.Add(new Vector3(x, y, z)); } } } } } } } } } } float minDist = 99999999999; List <float> distances = new List <float>(); Vector3 closest = new Vector3(); Vector3 secondClosest = new Vector3(); bool foundOne = false; for (int i = 0; i < intersected.Count; i++) { Vector3 test = intersected[i]; if (array[(int)test.X, (int)test.Y, (int)test.Z] != 0)//if (getTypeAt((int)alreadyOccupiedBlock.X, (int)alreadyOccupiedBlock.Y, (int)alreadyOccupiedBlock.Z).transLoS == false) { Vector3 dist = firstRef - test; float length = (float)Math.Sqrt((firstRef.X - test.X) * (firstRef.X - test.X) + (firstRef.Y - test.Y) * (firstRef.Y - test.Y) + (firstRef.Z - test.Z) * (firstRef.Z - test.Z)); distances.Add(length); if ((float)length < (float)minDist) { minDist = length; foundOne = true; closest = test; } } } BoundingBox box = new BoundingBox(new Vector3(closest.X, closest.Y, closest.Z), new Vector3(closest.X + 1, closest.Y + 1, closest.Z + 1)); Ray ray = new Ray(firstRef, Vector3.Normalize(secondRef - firstRef)); float?distanceToHitMaybe = box.Intersects(ray); if (!distanceToHitMaybe.HasValue) { return(null); } float distanceToHit = (float)distanceToHitMaybe; float backup = .0001f; Vector3 slightlyCloser = ray.Direction * (distanceToHit - backup) + firstRef; secondClosest = slightlyCloser; if (foundOne) { if (withinSpace(closest)) { } } if (wantNearestHit) { return(closest); } else { return(secondClosest); } }