/// <summary> /// This variante of the BoundsValue constructor makes a fixed origin and direction that /// won't properly update when the universe's world axes rotate, or the object in question /// the bounding box surrounds moves. /// </summary> /// <param name="bounds"></param> /// <param name="origin"></param> /// <param name="direction"></param> /// <param name="shared"></param> public BoundsValue(Vector min, Vector max, Vector origin, Direction facing, SharedObjects shared) { unityBounds = new Bounds(); unityBounds.SetMinMax(min.ToVector3(), max.ToVector3()); this.origin = origin; this.shared = shared; this.facing = facing; RegisterInitializer(InitializeSuffixes); }
public BoundsValue GetBoundsValue() { Direction myFacing = VesselUtils.GetFacing(Vessel); Quaternion inverseMyFacing = myFacing.Rotation.Inverse(); Vector rootOrigin = Parts[0].GetPosition(); Bounds unionBounds = new Bounds(); for (int pNum = 0; pNum < Parts.Count; ++pNum) { PartValue p = Parts[pNum]; Vector partOriginOffsetInVesselBounds = p.GetPosition() - rootOrigin; Bounds b = p.GetBoundsValue().GetUnityBounds(); Vector partCenter = new Vector(b.center); // Just like the logic for the part needing all 8 corners of the mesh's bounds, // this needs all 8 corners of the part bounds: for (int signX = -1; signX <= 1; signX += 2) { for (int signY = -1; signY <= 1; signY += 2) { for (int signZ = -1; signZ <= 1; signZ += 2) { Vector corner = partCenter + new Vector(signX * b.extents.x, signY * b.extents.y, signZ * b.extents.z); Vector worldCorner = partOriginOffsetInVesselBounds + p.GetFacing() * corner; Vector3 vesselCorner = inverseMyFacing * worldCorner.ToVector3(); unionBounds.Encapsulate(vesselCorner); } } } } Vector min = new Vector(unionBounds.min); Vector max = new Vector(unionBounds.max); // The above operation is expensive and should force the CPU to do a WAIT 0: Shared.Cpu.YieldProgram(new YieldFinishedNextTick()); return(new BoundsValue(min, max, delegate { return Parts[0].GetPosition(); }, delegate { return VesselUtils.GetFacing(Vessel); }, Shared)); }