Example #1
0
        public void OnTriggerEnter(Collider collider)
        {
            if (collider.attachedRigidbody == null || !collider.CompareTag("Untagged"))
            {
                return;
            }
            if (!IsActivated)
            {
                return;
            }
            if (macTriggerTransform == null)
            {
                return;
            }

            //Get the part that collided with the trigger
            Part collidedPart = collider.attachedRigidbody.GetComponent <Part>();

            if (collidedPart == null)
            {
                return;
            }

            //Calculate the projectile velocity
            double projectileVelocity = Math.Sqrt((maxKineticEnergy * 2) / (1000 * collidedPart.vessel.totalMass));

            //Get velocity
            CelestialBody celestialBody   = collidedPart.vessel.mainBody;
            double        orbitalVelocity = Math.Sqrt(celestialBody.gravParameter / celestialBody.minOrbitalDistance);

            if (projectileVelocity > orbitalVelocity)
            {
                projectileVelocity = orbitalVelocity;
            }

            //Get accelearation.
            double time         = 1.0f;
            double acceleration = projectileVelocity / time;

            //Calculate force: f = mass * acceleration
            double force = (1000 * collidedPart.vessel.totalMass) * acceleration;

            //Account for render frames
            force *= TimeWarp.fixedDeltaTime;

            //Apply force
            collidedPart.vessel.IgnoreGForces(250);
            collidedPart.AddForceAtPosition(macTriggerTransform.forward * (float)force, collidedPart.vessel.CoM);

            //Add recoil
            if (!this.part.vessel.permanentGroundContact)
            {
                force  = (1000 * this.part.vessel.totalMass) * acceleration;
                force *= TimeWarp.fixedDeltaTime * -1.0f;
                this.part.vessel.IgnoreGForces(250);
                this.part.AddForceAtPosition(macTriggerTransform.forward * (float)force, this.part.vessel.CoM);
            }
        }
Example #2
0
    static float DoHoverForce(Transform rcTransform, float hoverHeight, float offset, float hoverDamp, float hoverForce, float rcrelative, float maxlift, float upwardSpeed, Part part)
    {
        RaycastHit hit;
        Ray        rcray  = new Ray(rcTransform.position, rcTransform.forward);
        float      height = 0.0f;
        bool       Ray    = Physics.Raycast(rcray, out hit, hoverHeight, 557057);

        if (FlightGlobals.ActiveVessel.mainBody.ocean) //if mainbody has ocean we land on water before the seabed
        {
            height = FlightGlobals.getAltitudeAtPos(rcTransform.position);
        }
        if (Ray || height < hoverHeight)
        {
            float hoverError = 0.0f;

            if (Ray)
            {
                hoverError = hoverHeight - hit.distance;
            }

            if (!Ray || height < hit.distance)
            {
                hoverError = hoverHeight - height;
            }

            hoverError  += offset;
            upwardSpeed -= hoverError;

            float lift = (hoverError - upwardSpeed * hoverDamp / Time.fixedDeltaTime) * hoverForce * rcrelative;

            if (lift > maxlift * hoverForce)
            {
                lift = maxlift * hoverForce;
            }

            upwardSpeed = hoverError;

            if (hoverError >= 0.0f)
            {
                part.AddForceAtPosition(-lift * rcTransform.forward, rcTransform.position);
            }
            //Debug.Log(hoverError);
            //Debug.Log(lift);
        }
        else
        {
            upwardSpeed = 0f;
        }
        return(upwardSpeed);
    }
        public void FixedUpdate()
        {
            if (magnetTransforms == null || magnetTransforms.Length == 0)
            {
                return;
            }
            if (!HighLogic.LoadedSceneIsFlight)
            {
                return;
            }
            if (!magnetIsActive)
            {
                return;
            }
            if (targetPart == null)
            {
                return;
            }

            /*
             * //Check power requirements
             * if (resourceBroker.AmountAvailable(this.part, kRequiredResource, TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL) >= unitsPerUpdate)
             * {
             *  resourceBroker.RequestResource(this.part, kRequiredResource, unitsPerUpdate, TimeWarp.fixedDeltaTime, ResourceFlowMode.ALL_VESSEL);
             * }
             *
             * else
             * {
             *  magnetIsActive = false;
             *  return;
             * }
             */

            //Apply magnetic forces
            float magneticForce = forcePerTransform * (magnetPercent / 100.0f);

            for (int index = 0; index < magnetTransforms.Length; index++)
            {
                targetPart.AddForceAtPosition(magnetTransforms[index].forward.normalized * -magneticForce, magnetTransforms[index].transform.position);
                this.part.AddForceAtPosition(magnetTransforms[index].forward.normalized * magneticForce, magnetTransforms[index].transform.position);
            }
        }