private float GetRotation(bool smooth) { float frontAngle = RailPosition.Segment.Angle; RailPathPosition backPosition = Train.GetBackwardsPathPosition(RailPosition, Length); float backAngle = backPosition.Segment.Angle; float avgAngle = (frontAngle + backAngle) / 2f; TargetAngle = avgAngle + 180; if (!smooth) { return(TargetAngle); } float currentAngle = transform.rotation.eulerAngles.y; float maxAngleChange = MaxAngleChangePerKph * Mathf.Abs(Train.VelocityKph) * Time.deltaTime; if (Mathf.Abs(TargetAngle - currentAngle) <= maxAngleChange) { return(TargetAngle); } else if (TargetAngle > currentAngle) { return(currentAngle + maxAngleChange); } else if (TargetAngle < currentAngle) { return(currentAngle - maxAngleChange); } throw new System.Exception(); }
public void Init(List <RailSegment> lastSegments) { LastSegments = lastSegments; RailPosition = new RailPathPosition(LastSegments[0], 0f); UpcomingSegments = new List <RailSegment>(); UpcomingSegments.Add(RailPosition.Segment); // Init upcoming segments while (UpcomingSegments.Count < UpcomingSegmentsLength) { RailSegment furthest = null; RailSegment secondFurthest = null; if (UpcomingSegments.Count == 1) { furthest = RailPosition.Segment; secondFurthest = LastSegments[1]; } else { furthest = UpcomingSegments[UpcomingSegments.Count - 1]; secondFurthest = UpcomingSegments[UpcomingSegments.Count - 2]; } UpcomingSegments.Add(furthest.GetNextSegment(secondFurthest)); } UpdatePosition(); }
public void InitWagon(Train train, RailPathPosition position) { Train = train; Wheels = new Dictionary <WheelPosition, TrainWheel>() { { WheelPosition.FrontLeft, null }, { WheelPosition.FrontRight, null }, { WheelPosition.RearLeft, null }, { WheelPosition.RearRight, null }, }; RailPosition = position; }
private Quaternion GetWheelRotation(WheelPosition position) { RailPathPosition wheelPosition = null; if (position == WheelPosition.FrontLeft || position == WheelPosition.FrontRight) { wheelPosition = Train.GetBackwardsPathPosition(RailPosition, WheelInset); } if (position == WheelPosition.RearLeft || position == WheelPosition.RearRight) { wheelPosition = Train.GetBackwardsPathPosition(RailPosition, Length - WheelInset); } return(Quaternion.Euler(90f, 90f + wheelPosition.Segment.Angle, 0f)); }
public Wagon AddWagon() { GameObject wagonObject = new GameObject("Wagon"); Wagon wagon = wagonObject.AddComponent <Wagon>(); float wagonDistance = Wagons.Sum(x => Wagon.Length) + (Wagons.Count * WagonConnectionLength); RailPathPosition wagonPosition = GetBackwardsPathPosition(RailPosition, wagonDistance); wagon.InitWagon(this, wagonPosition); Wagons.Add(wagon); wagon.transform.SetParent(transform); wagon.transform.localPosition = Vector3.zero; UpdatePosition(); return(wagon); }
/// <summary> /// Returns the segment and distance at a certain distance forwards from the source position /// </summary> public RailPathPosition GetForwardsPathPosition(RailPathPosition sourcePosition, float distance) { // Target position is on same segment if (distance < RailPathGenerator.RailSegmentLength - sourcePosition.Distance) { return(new RailPathPosition(sourcePosition.Segment, sourcePosition.Distance + distance)); } // Target position is not on same segment float distanceFromSegmentStart = distance - (RailPathGenerator.RailSegmentLength - sourcePosition.Distance); int nSegmentSkips = (int)(distanceFromSegmentStart / RailPathGenerator.RailSegmentLength); float restDistance = ((distanceFromSegmentStart / RailPathGenerator.RailSegmentLength) - nSegmentSkips) * RailPathGenerator.RailSegmentLength; nSegmentSkips++; int sourceSegmentIndex = UpcomingSegments.IndexOf(sourcePosition.Segment); RailPathPosition position = new RailPathPosition(UpcomingSegments[sourceSegmentIndex + nSegmentSkips], restDistance); return(position); }
public void SetPosition(RailPathPosition newPos) { // Train position if (newPos.Segment != RailPosition.Segment) { UpdateAdjacentSegments(newPos.Segment); } RailPosition.SetPosition(newPos); // Wagon positions float tmpDistance = 0f; foreach (Wagon w in Wagons) { RailPathPosition wagonPosition = GetBackwardsPathPosition(RailPosition, tmpDistance); w.SetPosition(wagonPosition); tmpDistance += Wagon.Length + WagonConnectionLength; } UpdatePosition(); }
// Update is called once per frame void Update() { if (VelocityKph != 0) { float mps = VelocityKph / 3.6f; FrameDistance = Time.deltaTime * mps; RailPathPosition nextPosition = null; if (FrameDistance > 0) { nextPosition = GetForwardsPathPosition(RailPosition, FrameDistance); } else if (FrameDistance < 0) { nextPosition = GetBackwardsPathPosition(RailPosition, -FrameDistance); } SetPosition(nextPosition); } else { SetPosition(RailPosition); } }
private Vector3 GetWheelPosition(WheelPosition position) { RailPathPosition wheelPosition = null; if (position == WheelPosition.FrontLeft || position == WheelPosition.FrontRight) { wheelPosition = Train.GetBackwardsPathPosition(RailPosition, WheelInset); } if (position == WheelPosition.RearLeft || position == WheelPosition.RearRight) { wheelPosition = Train.GetBackwardsPathPosition(RailPosition, Length - WheelInset); } Vector3 wheelCenterPosition = wheelPosition.Segment.GetWorldPositionAtDistance(wheelPosition.Distance); RailSettings railSettings = wheelPosition.Segment.Settings; float height = railSettings.TrackHeight + railSettings.PlankHeight + WheelHeight / 2; float wheelDistance = railSettings.TrackGap / 2; float wheelAngle = 0f; if (position == WheelPosition.FrontLeft || position == WheelPosition.RearLeft) { wheelAngle = wheelPosition.Segment.Angle + 90; } if (position == WheelPosition.FrontRight || position == WheelPosition.RearRight) { wheelAngle = wheelPosition.Segment.Angle - 90; } float offsetX = Mathf.Sin(Mathf.Deg2Rad * wheelAngle) * wheelDistance; float offsetZ = Mathf.Cos(Mathf.Deg2Rad * wheelAngle) * wheelDistance; Vector3 wheelOffset = new Vector3(offsetX, height, offsetZ); return(wheelCenterPosition + wheelOffset); }
public void SetPosition(RailPathPosition position) { Segment = position.Segment; Distance = position.Distance; }
public void SetPosition(RailPathPosition position) { FrameMoveVector = position.GetWorldPosition() - RailPosition.GetWorldPosition(); RailPosition.SetPosition(position); UpdatePosition(true); }