public void SetWall(WallLogic wall, WallLogic prev, float start, float end) { Wall = wall; StartPoint = start; EndPoint = end; float3 prevCenter = (prev.GetEndPosition() + prev.GetStartPosition()) / 2; ClosestPoint = MathHelper.ClosestPoint(wall.GetStartPosition(), wall.GetEndPosition(), prevCenter); Position = (startPosition + endPosition) / 2; float distance2 = MathHelper.SqrDistance(ClosestPoint.xyz, prevCenter); float offsetT = data.BoundingSize.x / 2 / math.sqrt(distance2); // Debug.Log(offsetT); //Direction = GetPointSide(point); //cachedPoint = math.lerp(Position, point, offsetT); float3 movement = prevCenter - ClosestPoint.xyz; offset = movement * offsetT; Position += offset; startPosition = math.lerp(wall.GetStartPosition(), wall.GetEndPosition(), start) + offset - Position; endPosition = math.lerp(wall.GetStartPosition(), wall.GetEndPosition(), end) + offset - Position; Position = new float3(Position.x, data.ZoffsetinMeters, Position.z); transform.position = Position; vaild = true; UpdateVisuals(); }
public void UpdatePosition(float3 point) { selected = true; var walls = Blueprint.Walls; float min = float.MaxValue; float t = .5f; float3 cachedPoint = point; Position = point; ClosestPoint = new float4(cachedPoint, ClosestPoint.w); transform.position = Position; vaild = false; if (!data.FreeFloating) { for (int i = 0; i < walls.Count; i++) { WallLogic wall = walls[i]; float4 closest = wall.GetClosestPoint(point, out t); float distance2 = MathHelper.SqrDistance(closest.xyz, point); float halfWidthT = (data.BoundingSize.x + 1) / 2 / walls[i].Length; float startPoint = t - halfWidthT; float endPoint = t + halfWidthT; //Debug.Log((distance2 < Placeable.SnapDistance) + " : " + (distance2 < min) + " : " + (0 <= startPoint) + " : " + (endPoint <= 1)); if (distance2 < Dragable.SnapDistance && distance2 < min && 0 <= startPoint && endPoint <= 1) { CenterT = t; vaild = true; min = distance2; Wall = wall; ClosestPoint = closest; float sign = math.sign(closest.w); StartPoint = startPoint; EndPoint = endPoint; bool moved = false; for (int j = 0; j < Wall.Cutouts.Count; j++) { var other = Wall.Cutouts[j]; float otherSign = math.sign(other.ClosestPoint.w); bool intersect = (!data.Offset || !other.data.Offset) || ((data.Offset && other.data.Offset) && sign == otherSign); bool overlap = (other.data.BlockCutsBottom && data.BlockCutsBottom) || (other.data.BlocksCutsTop && data.BlocksCutsTop); //Debug.Log(intersect + " : " + overlap); if (intersect && overlap) { //Debug.Log(StartPoint + " : " + other.EndPoint+":"+(StartPoint <= other.EndPoint) ); //Debug.Log(EndPoint + " : " + other.EndPoint + ":" + (EndPoint >= other.EndPoint)); //Debug.Log(StartPoint + " : " + other.StartPoint + ":" + (EndPoint >= other.StartPoint)); //Debug.Log(EndPoint + " : " + other.StartPoint + ":" + (EndPoint >= other.StartPoint)); if (StartPoint <= other.EndPoint && EndPoint >= other.EndPoint) { if (moved) { vaild = false; } var snap = other.EndPoint - StartPoint; t += snap; cachedPoint = math.lerp(Wall.GetStartPosition(), Wall.GetEndPosition(), t); ClosestPoint = new float4(cachedPoint, ClosestPoint.w); } else if (StartPoint <= other.StartPoint && EndPoint >= other.StartPoint) { if (moved) { vaild = false; } var snap = other.StartPoint - EndPoint; t += snap; cachedPoint = math.lerp(Wall.GetStartPosition(), Wall.GetEndPosition(), t); ClosestPoint = new float4(cachedPoint, ClosestPoint.w); } } } for (int j = 0; j < Wall.Placeables.Count; j++) { var other = Wall.Placeables[j]; float otherSign = math.sign(other.ClosestPoint.w); bool intersect = (!data.Offset || !other.data.Offset) || ((data.Offset && other.data.Offset) && sign == otherSign); bool overlap = (other.data.BlockCutsBottom && data.BlockCutsBottom) || (other.data.BlocksCutsTop && data.BlocksCutsTop); // Debug.Log(intersect + " : " + overlap); if (intersect && overlap) { //Debug.Log(StartPoint + " : " + other.EndPoint + ":" + (StartPoint <= other.EndPoint)); //Debug.Log(EndPoint + " : " + other.EndPoint + ":" + (EndPoint >= other.EndPoint)); //Debug.Log(StartPoint + " : " + other.StartPoint + ":" + (EndPoint >= other.StartPoint)); //Debug.Log(EndPoint + " : " + other.StartPoint + ":" + (EndPoint >= other.StartPoint)); if (StartPoint <= other.EndPoint && EndPoint >= other.EndPoint) { if (moved) { vaild = false; } var snap = other.EndPoint - StartPoint; t += snap; cachedPoint = math.lerp(Wall.GetStartPosition(), Wall.GetEndPosition(), t); ClosestPoint = new float4(cachedPoint, ClosestPoint.w); } else if (StartPoint <= other.StartPoint && EndPoint >= other.StartPoint) { if (moved) { vaild = false; } var snap = other.StartPoint - EndPoint; t += snap; cachedPoint = math.lerp(Wall.GetStartPosition(), Wall.GetEndPosition(), t); ClosestPoint = new float4(cachedPoint, ClosestPoint.w); } } } if (StartPoint < 0 || EndPoint > 1) { vaild = false; } //transform.eulerAngles = new Vector3(0, math.abs(closest.w), 0); Position = ClosestPoint.xyz; if (data.Offset) { float offsetT = data.BoundingSize.x / 2 / math.sqrt(distance2); // Debug.Log(offsetT); //Direction = GetPointSide(point); //cachedPoint = math.lerp(Position, point, offsetT); float3 movement = point - closest.xyz; offset = movement * offsetT; Position += offset; } startPosition = math.lerp(wall.GetStartPosition(), wall.GetEndPosition(), StartPoint); endPosition = math.lerp(wall.GetStartPosition(), wall.GetEndPosition(), EndPoint); startPosition += offset - Position; endPosition += offset - Position; HalfWidthT = data.BoundingSize.x / 2 / Wall.Length; Position = new float3(Position.x, data.ZoffsetinMeters, Position.z); transform.position = Position; } else { } } } else { vaild = true; transform.position = Position; } UpdateVisuals(); }