Ejemplo n.º 1
0
        /// <summary>
        /// Draws a prediction of the planet's trajectory
        /// </summary>
        private void DrawPrediction()
        {
            float timeStep = PredictionTime / PredictionSteps;

            LineRenderer lr = GetComponent <LineRenderer>();

            if (lr == null)
            {
                return;
            }

            lr.numPositions = PredictionSteps;
            // First point is current position
            lr.SetPosition(0, transform.position);

            // These will hold last prediction's values
            Vector3 lastVelocity = Velocity;
            Vector3 lastPosition = transform.position;

            for (int i = 1; i < PredictionSteps; i++)
            {
                // Get all the forces
                Vector3 forcesThisStep = GetSumForcesApplyingToMe(lastPosition);
                // Calculate velocity at given time
                Vector3 velocityThisStep = lastVelocity + forcesThisStep * timeStep * i; // (timeStep * i) is the equivalent of Time.deltaTime in the prediction
                // Deduce positin
                Vector3 positionThisStep = lastPosition + velocityThisStep * timeStep * i;

                lr.SetPosition(i, positionThisStep);

                // Raycast from last point to new point to check if the prediction is crossing a planet
                RaycastHit hitInfo;
                Ray        rayhit = new Ray(lastPosition, positionThisStep - lastPosition);
                Physics.Raycast(rayhit, out hitInfo, Vector3.Distance(positionThisStep, lastPosition) * 2f, 1 << LayerManager.ToInt(LAYER.PLANET), QueryTriggerInteraction.Ignore);
                // If we hit something stop drawing the prediction
                if (hitInfo.collider != null)
                {
                    lr.numPositions = i + 1;
                    i = PredictionSteps - 1;
                }

                lastPosition = positionThisStep;
                lastVelocity = velocityThisStep;
            }
        }