Jump to content

[SOLVED] Drawing orbits with no attached vessel


Recommended Posts

I'm trying to use the built-in orbit renderer system to draw an arbitrary orbit in the mapview without any vessel attached to it. The method shown below (by @TMarkos) used to work pre-1.1, but the orbit renderer was changed in that update. This method doesn't throw any errors, but neither does any orbit show up.

Any suggestions for how to make this work with the new orbit renderer or what I'm doing wrong (I'm pretty new to Unity)?

Vector3d exitTraj = getJumpOffset(near, far, model);	// orbit velocity
oPredictDriver = new OrbitDriver();
oPredictDriver.orbit = new Orbit();
oPredictDriver.orbit.referenceBody = far.mainBody;
oPredictDriver.referenceBody = far.mainBody;
oPredictDriver.upperCamVsSmaRatio = 999999;				// Setting zoom limits for showing the orbit
oPredictDriver.lowerCamVsSmaRatio = 0.0001f;
oPredictDriver.orbit.UpdateFromStateVectors(far.orbit.pos, exitTraj, far.mainBody, Planetarium.GetUniversalTime());
oPredictDriver.orbit.Init();

Vector3d p = oPredictDriver.orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime());
Vector3d v = oPredictDriver.orbit.getOrbitalVelocityAtUT(Planetarium.GetUniversalTime());
oPredictDriver.orbit.h = Vector3d.Cross(p, v);
oPredict = MapView.MapCamera.gameObject.AddComponent<OrbitRenderer>();
oPredict.upperCamVsSmaRatio = 999999;
oPredict.lowerCamVsSmaRatio = 0.0001f;
oPredict.celestialBody = far.mainBody;
oPredict.driver = oPredictDriver;
oPredictDriver.Renderer = oPredict;

oPredict.driver.drawOrbit = true;
oPredict.driver.orbitColor = Color.red;
oPredict.orbitColor = Color.red;
oPredict.drawIcons = OrbitRenderer.DrawIcons.OBJ_PE_AP;
oPredict.drawMode = OrbitRenderer.DrawMode.REDRAW_AND_RECALCULATE;

 

Edited by Booots
Link to comment
Share on other sites

Not sure this is all you need but OrbitDriver is a MonoBehaviour so it needs to be attached to a GameObject and not created with a "new"

GameObject myOrbitObject = new GameObject("myOrbitObject");
OrbitDriver myOrbitDriver = myOrbitObject.AddComponent<OrbitDriver>();

I doubt this is all you need to change but I never drew orbits with those classes...

Link to comment
Share on other sites

  • 2 years later...
  • 6 months later...

I've finally solved this, so I'm posting the solution here for any future forum-trawlers looking to do something like this. It turns out that I was so, so close initially.

There are two similar options depending on what you want, exactly. If a single patch is enough you can draw it with OrbitRenderer (you can also change the colour this way), or you can use the PatchedConicRenderer if you want multiple patches for encounters and escape. Working code that includes both is included in the ESLD Beacons code, found here.

The key steps are:

  1. Create a new GameObject: this will hold all the necessary components.
  2. AddComponent<OrbitDriver> to the GameObject.
  3. Set the OrbitDriver.orbit to the orbit you want.
  4. Set the OrbitDriver.updateMode to OrbitDriver.UpdateMode.TRACK_Phys;
  5. AddComponent<OrbitRenderer> to the GameObject.
  6. If you want a simple orbit, populate the OrbitRenderer fields as needed, and you're done!
  7. Set the OrbitDriver.vessel to an Vessel object (I know, I know, we'll break this link later. See the comment in the code as to why we need it.)
  8. Otherwise, AddComponent<PatchedConicSolver> and AddComponent<PatchedConicRenderer> to the GameObject.
  9. Disable the OrbitRenderer by setting .drawIcons = OrbitRendererBase.DrawIcons.NONE and .drawMode = OrbitRendererBase.DrawMode.OFF;
  10. Set the OrbitDriver.vessel to null at the earliest chance AFTER the PatchedConicRenderer has run its Start() method. I use a Coroutine to check until PatchedConicRender.relativeTo != null and then set the vessel to null.
Link to comment
Share on other sites

  • 5 months later...

@Booots I found this thread after searching for how to hide orbit lines for a vessel. I tried setting the orbitRenderer.drawMode of the active vessel to OrbitRendererBase.DrawMode.OFF but it doesn't seem to have any effect. Setting drawIcons = OrbitRendererBase.DrawIcons.NONE works though. Was wondering if you have any insight as to why this might be the case?

edit: figured it out, needed to use the patchedConicRenderer, not the orbitRenderer.

Edited by subyng
Link to comment
Share on other sites

  • 1 year later...
On 3/7/2019 at 10:38 PM, Booots said:
  1. Set the OrbitDriver.vessel to null at the earliest chance AFTER the PatchedConicRenderer has run its Start() method. I use a Coroutine to check until PatchedConicRender.relativeTo != null and then set the vessel to null.

One fun discovery that was new to me, at least:  You can make Start() itself a coroutine by having it return IEnumerator, then re-unite all the startup code in one function:

	private IEnumerator Start()
	{
		// Early setup stuff

		yield return new WaitForEndOfFrame();

		// Middle setup stuff

		yield return new WaitForEndOfFrame();

		// Late setup stuff
	}
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...