Jump to content

Asteroid Gravity


Doyley

Recommended Posts

None that know of, and I don't see how you could do it well. Anything that did add it would be a hack.

Things in this game are either on permanent rails with no way to divert them and have gravity, or they are able to be affected by the player and don't have gravity. There's no middle ground.

Edited by 5thHorseman
Link to comment
Share on other sites

2 hours ago, Doyley said:

Hello again,

Are there any mods which add gravity to asteroids?

Thanks

Actually this is a simple implementation to do with a part module (Hint: Asteroids are actually vessels ... the asteroid prefab is actually a part) ... just add force to any vessel that is within a certain radius of the asteroid in the direction of the asteroid (pretty basic Unity stuff ... Vector3's and rigidbody.addforce)

Although if you want 'gravity' to work while the asteroid is out of physics range then a little bit of hack sauce would be needed although I don't expect this to be too much of a problem

I'll see what I can drum up in a bit

1 hour ago, 5thHorseman said:

None that know of, and I don't see how you could do it well. Anything that did add it would be a hack.

Basic Unity coding ... Challenge accepted 

Link to comment
Share on other sites

22 minutes ago, DoctorDavinci said:

Basic Unity coding ... Challenge accepted 

Make sure 2 asteroids orbiting each other (or a ship orbiting an asteroid) keep(s) doing so when out of physics range, and any ships landed on the asteroids remain landed.

And I never said it wasn't basic, just that it'd be a hack :)

Link to comment
Share on other sites

So here is the beginning of a gravity mod for asteroids .... simple short script for anyone to do what they want with, just attach it to an asteroid (or anything else :confused:) to have it attract nearby vessels

Would need some additional code to calculate the gravity well radius and how much force is applied to nearby vessels (based off asteroid mass perhaps?????)

Anyways, here you go ... I'm currently working on another mod so don't really have the time at the moment to do much more with this so have at it KSP community :wink:

Spoiler

    public class ModuleGravity : PartModule
    {
        Rigidbody rigidBody;
        private bool scanning = false;

        public override void OnFixedUpdate()
        {
            if (HighLogic.LoadedSceneIsFlight && FlightGlobals.ready)
            {
                if (!scanning)
                {
                    scanning = true;
                    Debug.Log("=== GravityScan ===");
                    StartCoroutine(GravityScan());
                }
            }

            base.OnFixedUpdate();
        }

        IEnumerator GravityScan()
        {
            List<Vessel>.Enumerator v = FlightGlobals.Vessels.GetEnumerator();
            try
            {
                while (v.MoveNext())
                {
                    if (v.Current == null) continue;
                    if (v.Current.loaded && v.Current != this.vessel)
                    {
                        double targetDistance = Vector3d.Distance(new Vector3d(FlightGlobals.ActiveVessel.latitude,
                            FlightGlobals.ActiveVessel.longitude, FlightGlobals.ActiveVessel.altitude),
                            new Vector3d(this.vessel.latitude, this.vessel.longitude, this.vessel.altitude));
                        if (targetDistance <= 500)
                        {
                            // Debug.Log("=== ADDING FORCE TO VESSEL '"+ v.Current.vesselName + "' ===");
                            Vector3d direction = new Vector3d(this.vessel.latitude, this.vessel.longitude, this.vessel.altitude)
                                - new Vector3d(FlightGlobals.ActiveVessel.latitude, FlightGlobals.ActiveVessel.longitude, FlightGlobals.ActiveVessel.altitude).normalized;
                            rigidBody = v.Current.GetComponent<Rigidbody>();
                            rigidBody.AddForce(direction * 10);
                        }
                    }
                }
            }
            catch (Exception e)
            {

            }

            yield return new WaitForEndOfFrame();
            scanning = false;
        }
    }

 

 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...