コード例 #1
0
        public void SetActiveConnection(bool previous, int index)
        {
            ConnectedSegmentInfo activeInfo = GetActiveConnectionInfo(previous);
            ConnectedSegmentInfo wantedInfo = GetConnectionInfo(previous, index);

            activeInfo.Active = false;
            wantedInfo.Active = true;
        }
コード例 #2
0
        public void ConnectSegment(RailSegment segment, bool asPreviousSegment, bool segmentHasSameTDirection)
        {
            // Add segment to list of connected segments
            var segmentInfo = new ConnectedSegmentInfo
            {
                RailSegment    = segment,
                AsPrevious     = asPreviousSegment,
                SameTDirection = segmentHasSameTDirection
            };

            ConnectedSegmentInfos.Add(segmentInfo);

            // Add me to other segment's list of connected segments
            var otherSegmentInfo = new ConnectedSegmentInfo
            {
                RailSegment    = this,
                AsPrevious     = asPreviousSegment ? !segmentHasSameTDirection : segmentHasSameTDirection,
                SameTDirection = segmentHasSameTDirection
            };

            segment.ConnectedSegmentInfos.Add(otherSegmentInfo);
        }
コード例 #3
0
        public RailSegment MoveSignedDistance(float signedDistance, ref float t, ref int directionAlongT)
        {
            // Move over the rail segment for the given distance, going to a next or previous
            // segment if the distance to move is bigger than the distance to the end of the curve of the segment.
            // If it is, subtract the remaining length on the curve from the distance to travel, set the new
            // segment as the current segment, and repeat.
            // The distance to move is signed indicating the direction to move over the segment(s). Positive means
            // in the same direction as curve parameter t (going from 0 to 1).
            // Segments can be connected in different ways (head to head, head to tail, etc). Variable XXX
            // is used to keep track of the direction on the curve. If true, we are moving from current curve parameter
            // t = <current> to t = 0. If false, we move towards t = 1

            float       remainingDistance   = Math.Abs(signedDistance);
            bool        movePositiveInitial = signedDistance > 0;
            bool        movePositive        = movePositiveInitial;
            RailSegment currentSegment      = this;

            while (remainingDistance > 0)
            {
                float curvePosition = currentSegment.Curve.GetLength(0, t);

                if (movePositive)
                {
                    // Moving over curve towards t = 1 (positive direction)
                    float remainingCurveLength = Curve.Length - curvePosition;
                    if (remainingDistance <= remainingCurveLength)
                    {
                        // More curve remaining than distance to move. Update time and done
                        curvePosition    += remainingDistance;
                        t                 = currentSegment.Curve.GetTime(curvePosition, 32, CurvePositionTolerance);
                        remainingDistance = 0;
                    }
                    else
                    {
                        // More distance to move than curve length remaining.
                        // Move to end of curve, set next curve as current (if it exists), and continue
                        remainingDistance -= remainingCurveLength;
                        ConnectedSegmentInfo connectedSegmentInfo = GetActiveConnectedSegment(false);
                        if (connectedSegmentInfo.RailSegment != null)
                        {
                            if (connectedSegmentInfo.SameTDirection)
                            {
                                t = 0;
                            }
                            else
                            {
                                movePositive = false;
                                t            = 1;
                            }
                            currentSegment = connectedSegmentInfo.RailSegment;
                        }
                        else
                        {
                            // No active next segment: end of track and done
                            currentSegment    = null;
                            remainingDistance = 0;
                            t = 1;
                        }
                    }
                }
                else
                {
                    // Moving over curve towards t = 0 (negative direction)
                    if (remainingDistance <= curvePosition)
                    {
                        // More curve remaining than distance to move. Update time and done
                        curvePosition    -= remainingDistance;
                        t                 = currentSegment.Curve.GetTime(curvePosition, 32, CurvePositionTolerance);
                        remainingDistance = 0;
                    }
                    else
                    {
                        // More distance to move than curve length remaining.
                        // Move to start of curve, set previous curve as current (if it exists), and continue
                        remainingDistance -= curvePosition;
                        ConnectedSegmentInfo connectedSegmentInfo = GetActiveConnectedSegment(true);
                        if (connectedSegmentInfo.RailSegment != null)
                        {
                            if (connectedSegmentInfo.SameTDirection)
                            {
                                t = 1;
                            }
                            else
                            {
                                movePositive = true;
                                t            = 0;
                            }
                            currentSegment = connectedSegmentInfo.RailSegment;
                        }
                        else
                        {
                            // No active previous segment: end of track and done
                            currentSegment    = null;
                            remainingDistance = 0;
                            t = 1;
                        }
                    }
                }
            }
            if (movePositive != movePositiveInitial)
            {
                directionAlongT = -directionAlongT;
            }
            return(currentSegment);
        }