Jump to content

Need Help Programmatically Setting Spacecraft Position and Velocity


Arrowstar

Recommended Posts

Hello! 

I'm trying to figure out how to programmatically set the position and velocity of a spacecraft.  Say I want to locate the vehicle at 0 deg latitude, 0 deg longitude, and at some altitude.  I want my velocity vector to be due north.  How can I achieve this?  Here's what I have so far, but it doesn't seem to work.   Any help would be appreciated!
 

            double ut = Planetarium.GetUniversalTime();
            CelestialBody cb = this.vessel.orbit.referenceBody;
            Planetarium.CelestialFrame cbFrame = cb.BodyFrame;
            Vector3d pos = cb.GetWorldSurfacePosition(0, 0, 501000);

            this.vessel.SetPosition(pos);

            Vector3 CoM = vessel.localCoM;
            Vector3d up = (CoM - vessel.mainBody.position).normalized;
            Vector3d north = Vector3d.Exclude(up, (vessel.mainBody.position + vessel.mainBody.transform.up * (float)vessel.mainBody.Radius) - CoM).normalized;
            this.vessel.SetWorldVelocity(north * 100);

            this.vessel.UpdatePosVel();

Thoughts?  Thank you!

Link to comment
Share on other sites

7 minutes ago, Gotmachine said:

You need to manipulate the vessel orbit.
Orbit.UpdateFromStateVectors() should be the most straightforward way.

Great, thank you.  I see that method in the API documentation.  Any idea what reference frame those position and velocity Vector3d objects are relative to?  And do I just get the Orbit from the Vessel's OrbitDriver object?

Link to comment
Share on other sites

16 minutes ago, Arrowstar said:

Any idea what reference frame those position and velocity Vector3d objects are relative to?

From memory, they are relative to the current body frame, but really not sure.

18 minutes ago, Arrowstar said:

And do I just get the Orbit from the Vessel's OrbitDriver object?

Yes that should work.

Link to comment
Share on other sites

33 minutes ago, Gotmachine said:

From memory, they are relative to the current body frame, but really not sure.

Yes that should work.

When you say relative to the current body frame, you mean an inertial frame whose center is the center of the celestial body being orbited?

Link to comment
Share on other sites

13 minutes ago, Arrowstar said:

When you say relative to the current body frame, you mean an inertial frame whose center is the center of the celestial body being orbited?

Don't remember and I'm too lazy to check :P
I would suggest doing a github search for "
UpdateFromStateVectors", that should give you plenty of examples of how it should be used.

Link to comment
Share on other sites

6 hours ago, Gotmachine said:

Don't remember and I'm too lazy to check :P
I would suggest doing a github search for "
UpdateFromStateVectors", that should give you plenty of examples of how it should be used.

So I've given this a go, and all that happens when I execute the is the spacecraft twitches a bit.  It's like the orbit gets set and then is immediately reversed.  I'm not sure what to do about it.  Any ideas?

See 0:09 of this video for an example of what I mean. Sorry for all the dinging sounds.

https://vimeo.com/manage/videos/809574842/

Link to comment
Share on other sites

To manipulate the orbit you likely need to pack the vessel first with Vessel.GoOnRails().

This being said, I noticed in the video that the vessel is in-atmo.
I'm not sure what you're trying to achieve, but if you're trying to set the vessel velocity while in atmo, I'm not sure this is actually doable with orbit manipulation.
You will likely have to manually teleport the vessel first, then apply a velocity to all the parts.

I highly suggest that you check the source of existing mods doing similar things, HyperEdit is the main one coming to mind.

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...