Jump to content

I want to draw holographic lines in space to aid in debugging vectors.


Recommended Posts

I've just started helping out in small bits contributing code to the kOS mod ( http://forum.kerbalspaceprogram.com/threads/68089-0-23-kOS-Scriptable-Autopilot-System-v0-11-2-13/ ), and I've come across an idea that would be very handy to help me (and maybe others) to debug problems in their kOS scripts. Before I consider it, I'd like to ask if what I outline below is feasible and easy to do:

Some math problems in kOS involve doing a bit of vector math. But the XYZ axes in KSP can be hard to visualize and most players are playing "blind" as to what those axes are, and therefore have a hard time trying to work out what way a vector is actually pointing, given it's definition.

If you see that a vector contains the following parts:

x=103.1

y=2.10

z=912.12

Most players have no clue which way that's pointing in the space you're viewing at in the game.

What I'm wondering is this: is there a good way via the Unity engine or KSP API to make a holographic representation of the vectors in the 3D space view so a player can look at them, but they have zero effect on anything in the game and as far as KSP cares they may as well not be there? I'm picturing drawing a vector as a type of very narrow cylinder with a "hat" on the tip end, so it looks vaguely like this mockup:

eObALEG.png

These would be 3D objects (you could spin the camera around to get a better view of them) that the ship can just pass straight through with no effect (like the scatter rocks on the Mun).

If someone can guide me to what would be the needed API calls to put such an object into space (and then remove it later) that would be awesome. I've got experience with graphics and programming, but not specifically with the implementation of them that uses Unity, or KSP, or C#.

Link to comment
Share on other sites

LineRenderer is the right tool for the job here. You can create an arrowhead by creating a segment with a large starting width and zero ending width.

Yeah this is definitely easier than I thought it would be. The class already has everything I need for this. Technically it should be called coneRenderer, because it makes cylinders and cones where I specify the width, not just lines, and it also makes the object a thing that can be shifted and moved after placement by moving vertices - so it's got everything I need for this.

Thanks for the advice, both of you.

Link to comment
Share on other sites

  • 3 weeks later...

Okay, so I've gotten quite a long ways along with this but I'm still running into a problem. When I'm on the flight view, then this seems to work as a way to show the vector centered on the ship's center of mass:


GameObject lineObj = new GameObject("Line");
GameObject hatObj = new GameObject("Line");

if (onMap)
{
lineObj.layer = 9;
hatObj.layer = 9;
}

_line = lineObj.AddComponent<LineRenderer>();
_hat = hatObj.AddComponent<LineRenderer>();

_line.useWorldSpace = false;
_hat.useWorldSpace = false;

// Default the vector's origin to the current ship.
// (user can modify this using VECTOR:SHOWSTART)
Vector3d shipPosFromOrigin = FlightGlobals.ActiveVessel.findWorldCenterOfMass();
_line.transform.localPosition = shipPosFromOrigin;
_hat.transform.localPosition = shipPosFromOrigin;

_line.material = new Material(Shader.Find("Particles/Additive"));
_hat.material = new Material(Shader.Find("Particles/Additive"));

When I do that, I cause the _line and the _hat objects to be LineRenderers who's frame of reference matches the ship's position in its local XYZ space, If I for example use them to draw a line from (0,0,0) to (10,0,0) then I'll get a 10 meter line along the x axis sticking out of the ship. That's great.

But now I want to do the same thing in map view, but the map view uses some other sort of coordinate system in which the XYZ space moves with the camera. When I use the same code then the origin is wherever the camera is, not wherever the ship is.

How do I get the vector to be drawn with its origin at the current vessel instead of where the camera is in map view?

Link to comment
Share on other sites

But now I want to do the same thing in map view, but the map view uses some other sort of coordinate system

Yep. Convert the ship's local space world position into scaled space world position.

var vesselPos = ScaledSpace.LocalToScaledSpace(FlightGlobals.ActiveVessel.findWorldCenterOfMass())

Link to comment
Share on other sites

Yep. Convert the ship's local space world position into scaled space world position.

var vesselPos = ScaledSpace.LocalToScaledSpace(FlightGlobals.ActiveVessel.findWorldCenterOfMass())

Thank you that works great.

Another question is - how do I query the scale of the map view? For example in the in-flight view a line that is exactly 1.0 units long appears as 1 meter long. But how long is a "unit" of 3-D space in the map view? 10 kilometers? 100? etc.

(Also, is there any sort of API documentation SQUAD provides for modders for things like this?)

Link to comment
Share on other sites

Another question is - how do I query the scale of the map view? For example in the in-flight view a line that is exactly 1.0 units long appears as 1 meter long. But how long is a "unit" of 3-D space in the map view? 10 kilometers? 100? etc.

(Also, is there any sort of API documentation SQUAD provides for modders for things like this?)

1:6000 (ScaledSpace.Instance.scaleFactor, or ScaledSpace.InverseScaledFactor for the inve-well, you get it). So a line 10,000 units long in local space would be 1.67 units long in scaled space. Kerbin's radius is about 1,000 units for comparison. It might be better to have two different scales else you might have to zoom in pretty close to your mapview vessel to even see the lines

Link to comment
Share on other sites

1:6000 (ScaledSpace.Instance.scaleFactor, or ScaledSpace.InverseScaledFactor for the inve-well, you get it). So a line 10,000 units long in local space would be 1.67 units long in scaled space. Kerbin's radius is about 1,000 units for comparison. It might be better to have two different scales else you might have to zoom in pretty close to your mapview vessel to even see the lines

Well the intent is to show the user the same scale in both so it looks clear and obvious (so small vectors don't show on the map view, deliberately. switching to map view is for seeing things like "is this position vector really pointing all the way to the Mun from my ship like I thought it was?" Since the vectors are sometimes used as positional coordinates it's vital that people be able to see them in the right scale so that where it looks like the vector's tip is in the map view is really where it is in the world.

Any idea why ScaledSpace.LocalToScaledSpace() isn't giving the right scale? I'm using that to ensure the right orientation and translation, but it dosn't seem to be getting the right scale.

Link to comment
Share on other sites

Any idea why ScaledSpace.LocalToScaledSpace() isn't giving the right scale? I'm using that to ensure the right orientation and translation, but it dosn't seem to be getting the right scale.

What do you mean, scale? ScaledSpace.LocalToScaledSpace() merely converts one coordinate point to a different coordinate point; it doesn't worry about any kind of scale or rotation.

Link to comment
Share on other sites

What do you mean, scale? ScaledSpace.LocalToScaledSpace() merely converts one coordinate point to a different coordinate point; it doesn't worry about any kind of scale or rotation.

Okay thanks. Hey I'm just guessing what these things do since the API is undocumented. I was just guessing from the name that it might be a full-blown matrix transform with all the things in it - translate, rotate, scale.

Link to comment
Share on other sites

  • 2 weeks later...
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...