// Update is called once per frame void Update() { if (isMoving) { // How long has it been since we last updated our position float distanceIncrement = this.moveRate * Time.deltaTime; // find out how far we should move // convert to T value 0...1 this.currentDistanceAlongDubinPath += distanceIncrement; if (currentDistanceAlongDubinPath >= finalDistanceAlongDubinPath) { distanceIncrement = currentDistanceAlongDubinPath - finalDistanceAlongDubinPath; currentDistanceAlongDubinPath = finalDistanceAlongDubinPath; isMoving = false; } else { this.CurrentMoveDistance -= distanceIncrement; } this.CurrentMoveDistance = Mathf.Max(0.0f, CurrentMoveDistance); DubinCSC currentDubin = CurrentPath.path[this.currentDubinIdx]; float relativeDistance = currentDistanceAlongDubinPath - runningDubinDistanceTotal; if (relativeDistance > currentDubin.totalLength) { this.runningDubinDistanceTotal += currentDubin.totalLength; relativeDistance -= currentDubin.totalLength; currentDubinIdx++; currentDubin = this.currentPath.path[currentDubinIdx]; } Vector3 nextPos; Vector3 nextDir; currentDubin.GetPositionAndHeadingFromDistance(relativeDistance, this.currentPath.turnRadius, out nextPos, out nextDir); // move to that spot. this.transform.position = nextPos; this.transform.forward = this.CurrentPath.isReverse ? -nextDir : nextDir; gameEventSystem.RaiseEvent(GameEventType.ObjectPropertyChanged, this.gameObject, this.gameObject); // TODO: // Do something with tile grid? // Do we care for in motion stuff, or just the end position? if (!isMoving) { ////TODO: Do we really want to clear the entire, or just from the last point? //// in which case, we'd need to clip the dubin, basicaly....or repath //this.CurrentPath.path.Clear(); this.CurrentPath.Clip(currentDistanceAlongDubinPath); gameEventSystem.RaiseEvent(GameEventType.PathChanged, this.gameObject, CurrentPath); gameEventSystem.RaiseEvent(GameEventType.MoveEnded, this.gameObject, CurrentPath); } } }
public List <HeadingPositionPair> AsPointsList(LineRenderArgs args, float distInc) { Vector3 nextPos = Vector3.zero; Vector3 nextDir; int dubinIndex = path.Count - 1; float totalLength = this.CalculateTotalDistance(); float pathDistance = 0.0f; DubinCSC currentDubin = path[path.Count - 1]; // Figure out which Dubin in the list we should start on for (int i = 0; i < path.Count; ++i) { pathDistance += path[i].totalLength; if (args.StartDistance <= pathDistance) { currentDubin = path[i]; dubinIndex = i; pathDistance -= path[i].totalLength; break; //Found our dubin } } // const int numSegmentsPerDubin = 100; // int totalNumSegments = numSegmentsPerDubin * (path.Count - dubinIndex); //float distInc = Mathf.Ceil(currentDubin.totalLength / numSegmentsPerDubin); int numPairs = (int)(totalLength / distInc); List <HeadingPositionPair> pathLines = new List <HeadingPositionPair>(numPairs); for (float distance = args.StartDistance; distance < args.EndDistance;) { float relativeDistanceStart = distance - pathDistance; float relativeDistanceEnd = Mathf.Min(currentDubin.totalLength, args.EndDistance - pathDistance); //float distInc = Mathf.Ceil(currentDubin.totalLength / numSegmentsPerDubin); for (float relativeDistance = relativeDistanceStart; relativeDistance <= relativeDistanceEnd; relativeDistance += distInc) { currentDubin.GetPositionAndHeadingFromDistance(relativeDistance, turnRadius, out nextPos, out nextDir); pathLines.Add(new HeadingPositionPair(nextPos, nextDir)); } // ensure that the last spot is included currentDubin.GetPositionAndHeadingFromDistance(relativeDistanceEnd, turnRadius, out nextPos, out nextDir); pathLines.Add(new HeadingPositionPair(nextPos, nextDir)); distance += currentDubin.totalLength; pathDistance += currentDubin.totalLength; dubinIndex++; if (dubinIndex >= path.Count) { break; } currentDubin = path[dubinIndex]; } return(pathLines); }
public void DrawDubin(AccessibleLineRenderer lineRenderer, LineRenderArgs args) { if (path == null || path.Count == 0) { //TEMP lineRenderer.enabled = false; return; } lineRenderer.UnderlyingLineRenderer.SetWidth(args.LineWidth, args.LineWidth); //TEMP if (lineRenderer.enabled == false) { lineRenderer.enabled = true; } Vector3 nextPos = Vector3.zero; Vector3 nextDir; int dubinIndex = path.Count - 1; float totalLength = this.CalculateTotalDistance(); float pathDistance = 0.0f; DubinCSC currentDubin = path[path.Count - 1]; // Figure out which Dubin in the list we should start on for (int i = 0; i < path.Count; ++i) { pathDistance += path[i].totalLength; if (args.StartDistance <= pathDistance) { currentDubin = path[i]; dubinIndex = i; pathDistance -= path[i].totalLength; break; //Found our dubin } } const int numSegmentsPerDubin = 100; int totalNumSegments = numSegmentsPerDubin * (path.Count - dubinIndex); lineRenderer.SetVertexCount(totalNumSegments); //hackery int idx = totalNumSegments - 1; for (float distance = args.StartDistance; distance < args.EndDistance;) { float relativeDistanceStart = distance - pathDistance; float relativeDistanceEnd = Mathf.Min(currentDubin.totalLength, args.EndDistance - pathDistance); float distInc = Mathf.Ceil(currentDubin.totalLength / numSegmentsPerDubin); for (float relativeDistance = relativeDistanceStart; relativeDistance <= relativeDistanceEnd; relativeDistance += distInc) { currentDubin.GetPositionAndHeadingFromDistance(relativeDistance, turnRadius, out nextPos, out nextDir); nextPos.y += 1.0f; lineRenderer.SetPosition(idx--, nextPos); } // ensure that the last spot is included currentDubin.GetPositionAndHeadingFromDistance(relativeDistanceEnd, turnRadius, out nextPos, out nextDir); nextPos.y += 1.0f; lineRenderer.SetPosition(idx, nextPos); distance += currentDubin.totalLength; pathDistance += currentDubin.totalLength; dubinIndex++; if (dubinIndex >= path.Count) { break; } currentDubin = path[dubinIndex]; } int badMath = 0; //for (int i = idx; i < totalNumSegments; i++) for (int i = idx; i >= 0; --i) { badMath++; lineRenderer.SetPosition(i, nextPos); } // Debug.Log("BadMath: " + badMath); }