public RecordedTrajectoryPoint(RecordedTrajectoryPoint point)
 {
     position    = new float3(point.position.x, point.position.y, point.position.z);
     velocity    = new float3(point.velocity.x, point.velocity.y, point.velocity.z);
     orientation = new float3(point.orientation.x, point.orientation.y, point.orientation.z);
     futureTime  = point.futureTime;
 }
 public void Set(RecordedTrajectoryPoint point)
 {
     this.position    = point.position;
     this.velocity    = point.velocity;
     this.orientation = point.orientation;
     this.futureTime  = point.futureTime;
 }
        public static RecordedTrajectoryPoint Lerp(RecordedTrajectoryPoint point1, RecordedTrajectoryPoint point2, float factor)
        {
            float3 dir = math.lerp(point1.orientation, point2.orientation, factor);

            if (dir.x != 0f && dir.y != 0f && dir.z != 0f)
            {
                dir = math.normalize(dir);
            }
            else
            {
                dir = point1.orientation;
            }
            return(new RecordedTrajectoryPoint(
                       math.lerp(point1.position, point2.position, factor),
                       math.lerp(point1.velocity, point2.velocity, factor),
                       dir,
                       math.lerp(point1.futureTime, point2.futureTime, factor)
                       ));
        }
        public void RecordPastTimeTrajectory(
            List <float> pointsTimes,
            float updateTime,
            float timeDeltaTime,
            ref float recordTimer,
            ref List <RecordedTrajectoryPoint> recordedTrajectory,
            float3 objectPosition,
            float3 objectForward
            )
        {
            if (recordedTrajectory == null)
            {
                recordedTrajectory = new List <RecordedTrajectoryPoint>();
            }
            float maxRecordedTime = pointsTimes[0] - 0.05f;

            if (maxRecordedTime >= 0)
            {
                return;
            }
            int goalPointIndex = 0;
            RecordedTrajectoryPoint buffor;

            for (int i = 0; i < recordedTrajectory.Count; i++)
            {
                if (pointsTimes[goalPointIndex] < 0)
                {
                    if (recordedTrajectory[i].futureTime >= pointsTimes[goalPointIndex])
                    {
                        if (i == 0)
                        {
                            this.SetPoint(
                                recordedTrajectory[i].position,
                                recordedTrajectory[i].velocity,
                                recordedTrajectory[i].orientation,
                                goalPointIndex
                                );
                        }
                        else
                        {
                            float factor =
                                (pointsTimes[goalPointIndex] - recordedTrajectory[i - 1].futureTime)
                                /
                                (recordedTrajectory[i].futureTime - recordedTrajectory[i - 1].futureTime);
                            RecordedTrajectoryPoint lerpedPoint = RecordedTrajectoryPoint.Lerp(recordedTrajectory[i - 1], recordedTrajectory[i], factor);
                            this.SetPoint(
                                lerpedPoint.position,
                                lerpedPoint.velocity,
                                lerpedPoint.orientation,
                                goalPointIndex
                                );
                        }


                        goalPointIndex++;
                    }
                }
                buffor                = recordedTrajectory[i];
                buffor.futureTime    -= timeDeltaTime;
                recordedTrajectory[i] = buffor;
                if (recordedTrajectory[i].futureTime < maxRecordedTime)
                {
                    recordedTrajectory.RemoveAt(i);
                    i--;
                    continue;
                }
            }

            recordTimer += Time.deltaTime;
            if (recordTimer < updateTime)
            {
                return;
            }

            float3 velocity;

            if (recordedTrajectory.Count > 1)
            {
                velocity = (objectPosition - recordedTrajectory[recordedTrajectory.Count - 1].position) / recordTimer;
            }
            else
            {
                velocity = new float3(0, 0, 0);
            }
            recordTimer = 0f;
            recordedTrajectory.Add(
                new RecordedTrajectoryPoint(
                    objectPosition,
                    velocity,
                    objectForward,
                    0f
                    ));
        }