// Create a principalSpace3D and PrincipalTrajectory from a given Trajectory3D
        public static PrincipalSpace3D Create(
            Trajectory3D trajectory3D,                      // trajectory in world space
            out PrincipalTrajectory principalTrajectory)    // in principal space
        {
            PrincipalProjectile principalProjectile;

            // The following position should always lie on the principal plane as well
            Vector3 worldP1 = trajectory3D.p0 + trajectory3D.v0; 

            PrincipalSpace3D principalSpace3D = Create(trajectory3D, trajectory3D.p0, 
                worldP1, out principalProjectile);

            Vector2 principalVelocity = principalSpace3D.ToPrincipalVelocity(trajectory3D.v0);
            principalTrajectory = new PrincipalTrajectory(principalProjectile, principalVelocity);

            return principalSpace3D;
        }
        // in principal space
        // Create a principalSpace3D and PrincipalTrajectory from a given Trajectory3D
        public static PrincipalSpace3D Create(
            Trajectory3D trajectory3D,                      // trajectory in world space
            out PrincipalTrajectory principalTrajectory)
        {
            PrincipalProjectile principalProjectile;

            // The following position should always lie on the principal plane as well
            Vector3 worldP1 = trajectory3D.p0 + trajectory3D.v0;

            PrincipalSpace3D principalSpace3D = Create(trajectory3D, trajectory3D.p0,
                worldP1, out principalProjectile);

            Vector2 principalVelocity = principalSpace3D.ToPrincipalVelocity(trajectory3D.v0);
            principalTrajectory = new PrincipalTrajectory(principalProjectile, principalVelocity);

            return principalSpace3D;
        }
    void FixedUpdate()
    {
        remainingReloadTime = Mathf.Max(remainingReloadTime - Time.deltaTime, 0.0f);

        UpdateTrackedPositionsAndVelocities();

        // get the latest plan for the latest data
        bool planIsReadyForLaunch;
        Vector3 absoluteProjectileVelocity = TurretHelper.UpdatePlan(
            _planners, _projectileKinematics, launchTransform.position, _velocity,
            _targetPosition, _targetVelocity, remainingReloadTime, ref _plannedFlightTime,
            out planIsReadyForLaunch);

        // output debug widgets if requested
        if (drawDebugTrajectoryPlan)
        {
            Trajectory3D trajectory3D = new Trajectory3D(
                _projectileKinematics.Projectile3D, launchTransform.position +
                _velocity * remainingReloadTime, absoluteProjectileVelocity);

            TurretHelper.DebugDrawLastTrajectory(trajectory3D,
                _plannedFlightTime);
        }

        // if a plan has been found, update the turret's aim
        bool hasPlan = absoluteProjectileVelocity.sqrMagnitude > 0;
        Vector3 relativeProjectileVelocity = absoluteProjectileVelocity -
            _velocity;

        if (hasPlan)
        {
            UpdateTurretAim(relativeProjectileVelocity);

            // if the plan and aim is good enough, and there's no more remaining
            // weapon reload time, then launch.
            bool stillImprovingAimAccuracy = UpdateTurretOrientation();
            if (planIsReadyForLaunch && !stillImprovingAimAccuracy &&
                Vector3.Dot(relativeProjectileVelocity, launchTransform.forward) > 0)
            {
                Launch(relativeProjectileVelocity);
            }
        }
    }
        // Draw an animated trajectory with the given parameters using Debug.DrawLines.
        public static void DebugDrawLastTrajectory(Trajectory3D trajectory3D,
                                                   float timeToTarget)
        {
            int numSegments = 20;
            int numSamples = numSegments * 2 + 4;
            float dt = timeToTarget / (numSamples - 4);
            float t = Time.timeSinceLevelLoad / (dt * 4);
            t = (t - Mathf.Floor(t)) * dt * 4 - dt * 4;
            Color black = new Color(0, 0, 0, 0.75f), white = new Color(1, 1, 1, 0.75f);

            for (int i = 0; i < numSamples; ++i)
            {
                float fromTime = Mathf.Clamp(t, 0.0f, timeToTarget);
                float toTime = Mathf.Clamp(t + dt, 0.0f, timeToTarget);
                Debug.DrawLine(trajectory3D.PositionAtTime(fromTime),
                               trajectory3D.PositionAtTime(toTime),
                               i % 4 < 2 ? black : white);
                t += dt;
            }
        }
Example #5
0
        // Draw an animated trajectory with the given parameters using Debug.DrawLines.
        public static void DebugDrawLastTrajectory(Trajectory3D trajectory3D,
                                                   float timeToTarget)
        {
            int   numSegments = 20;
            int   numSamples  = numSegments * 2 + 4;
            float dt          = timeToTarget / (numSamples - 4);
            float t           = Time.timeSinceLevelLoad / (dt * 4);

            t = (t - Mathf.Floor(t)) * dt * 4 - dt * 4;
            Color black = new Color(0, 0, 0, 0.75f), white = new Color(1, 1, 1, 0.75f);

            for (int i = 0; i < numSamples; ++i)
            {
                float fromTime = Mathf.Clamp(t, 0.0f, timeToTarget);
                float toTime   = Mathf.Clamp(t + dt, 0.0f, timeToTarget);
                Debug.DrawLine(trajectory3D.PositionAtTime(fromTime),
                               trajectory3D.PositionAtTime(toTime),
                               i % 4 < 2 ? black : white);
                t += dt;
            }
        }