Beispiel #1
0
        bool GetBulletHitPosition(Vector3 targetPosition, out Vector3 position)
        {
            position = Vector3.zero;
            if (parentBlock == null)
            {
                return(false);
            }
            if (RadarTarget == null)
            {
                return(false);
            }
            //if (target.block != null)
            //{
            //    if (target.block == parentBlock) return false;
            //}

            //Get an initial velocity
            Vector3 targetVelocity = RadarTarget.Velocity;
            Vector3 initialBulletV = ForwardDirection * cannonBallSpeed;

            //Get an initial position
            Vector3 initialPosition = parentBlock.transform.position;

            //Assume no air resistance
            int   noSol;
            float time;

            if (targetVelocity.magnitude > 0.25f)
            {
                noSol = InterceptionCalculation.SolveBallisticArc(initialPosition, cannonBallSpeed, targetPosition, targetVelocity, Physics.gravity.magnitude, out aimDir, out time);
            }
            else
            {
                noSol = InterceptionCalculation.SolveBallisticArc(initialPosition, cannonBallSpeed, targetPosition, Physics.gravity.magnitude, out aimDir, out time);
            }

            //dir = (direction + parentBlock.transform.position).normalized;
            //position = initialPosition + initialBulletV * (1 - drag * time) * time + 0.5f * gravity * time * time - relVelocity * time;
            position = initialPosition + initialBulletV * time + 0.5f * Physics.gravity * time * time - targetVelocity * time;
            return(noSol > 0);
        }
        private IEnumerator AddGuideForce()
        {
            //if (blockRadar.target.transform != preTargetTransform)
            //{
            previousPosition = Vector3.zero;
            //preTargetTransform = blockRadar.target.transform;
            integral  = 0;
            lastError = 0;
            //}

            Vector3 addedForce;

            // Calculating the rotating axis
            Vector3 positionDiff = blockRadar.target.Position - parentBlock.transform.position;
            //Vector3 targetVelocity = blockRadar.target.rigidbody == null ?
            //    (blockRadar.target.Position - previousPosition) / Time.fixedDeltaTime : blockRadar.target.Velocity;
            Vector3 targetVelocity = blockRadar.target.Velocity;

            previousPosition = blockRadar.target.Position;
            Vector3 relVelocity = targetVelocity - parentBlock.Rigidbody.velocity;

            //float speed;
            bool turretMode;

            if (blockRadar.RadarType == RadarScript.RadarTypes.ActiveRadar)
            {
                turretMode = blockRadar.ShowBulletLanding;
                //speed = turretMode ? blockRadar.cannonBallSpeed : parentRigidbody.velocity.magnitude;
            }
            else
            {
                turretMode = blockRadar.passiveSourceRadar == null ? false : blockRadar.passiveSourceRadar.ShowBulletLanding && sourceSpeedPower < 0.1f;
                //speed = turretMode ? blockRadar.passiveSourceRadar.cannonBallSpeed : parentRigidbody.velocity.magnitude;
            }

            // Get the predicted point
            float   time;
            Vector3 positionDiffPredicted;

            if (turretMode)
            {
                Vector3 aimDir;
                if (blockRadar.RadarType == RadarScript.RadarTypes.ActiveRadar)
                {
                    aimDir = blockRadar.aimDir;
                }
                else
                {
                    aimDir = blockRadar.passiveSourceRadar == null ? Vector3.zero : blockRadar.passiveSourceRadar.aimDir;
                }
                positionDiffPredicted = parentBlock.transform.position + aimDir * parentBlock.transform.position.magnitude * 10000;
            }
            else
            {
                time = InterceptionCalculation.FirstOrderInterceptTime(parentRigidbody.velocity.magnitude, positionDiff, relVelocity);
                positionDiffPredicted = positionDiff + relVelocity * time;
            }
            positionDiffPredicted = positionDiffPredicted.normalized;

            // Get the angle difference
            float   dotProduct          = Vector3.Dot(ForwardDirection, positionDiffPredicted);
            Vector3 towardsPositionDiff = (dotProduct * positionDiffPredicted - ForwardDirection) * Mathf.Sign(dotProduct);

            towardsPositionDiff = towardsPositionDiff.normalized;

            if (constantForce)
            {
                addedForce = torque * maxTorque * towardsPositionDiff * 2000f;
            }
            else
            {
                float angleDiff = Vector3.Angle(ForwardDirection, positionDiff) + Vector3.Angle(positionDiff, positionDiffPredicted);
                integral += angleDiff * Time.fixedDeltaTime;
                float derivitive = (angleDiff - lastError) / Time.fixedDeltaTime;
                lastError = angleDiff;
                float coefficient = angleDiff * pFactor + integral * iFactor + derivitive * dFactor;
                addedForce = torque * maxTorque * coefficient * towardsPositionDiff;
            }

            // Add force to rotate rocket
            parentRigidbody.AddForceAtPosition(addedForce, parentBlock.transform.position + ForwardDirection);
            parentRigidbody.AddForceAtPosition(-addedForce, parentBlock.transform.position - ForwardDirection);
            yield break;
        }