Jump to content

[KSP 1.11.1] LaserDist, a distance measuring laser part (for kOS, kRPC, etc)


Dunbaratu

Recommended Posts

LaserDist

LaserDist v1.4.0 for KSP 1.10x. and KSP 1.11.x

The only real change here is compiling against KSP 1.10.x DLLs and support for putting LaserDist parts inside the new KSP inventory system.

The KSP 1.11.x updates added an EVA inventory system for parts and they
require parts have a new "ModuleCargoPart" section added to its
Part.cfg files to use it.

This release also works on the previous KSP version, KSP 1.10.x too, but it
may cause a startup complaint (which it is safe to ignore) in the logs on
KSP 1.10.x when it sees the new "ModuleCargoPart" section in the config files.

LaserDist v1.3.0 for KSP 1.8.1
 

Why does this Mod exist?

The intended purpose of this part is to be used in conjunction with scripted autopilots like [kOS](https://github.com/KSP-KOS/KOS/releases), to provide a way to for you to write scripted pilot software that can see the distance to the ground (or anything else like a ship) along the laser line.  See the image in the picture album to see what its meant by this.The reason this can be useful is so you can detect things like terrain slope and mountains in the way.  The default radar altimiter in KSP only shows you the distance directly under the craft.

 

Hey, where'd the pictures go?

This post used to contain some embedded images illustrating what LaserDist looks like.  They no longer appear because I no longer want to support the IMGUR site due to their awful willingness to let advertisers run whatever Javascript they feel like without properly vetting it for safety.  So I pulled my albums off that site.  Too many random ad redirects and browser-hijackings on that site for me.

If KSP forums support some other image hosting site, I may put the images back but I don't know at the moment how to get the forum to use anything other than IMGUR.\

KSP 1.8 update.

No changes to Laserdist except API usage with KSP 1.8.

KSP 1.8 deprecated some API calls, replacing them with alternate versions.

In particular, the shader used with LineRenderer to draw the laser lines needed to be
built with a new kind of constructor.

The V1.1.0 update:

Another go at fixing the phantom hits bug.  See: https://github.com/Dunbaratu/LaserDist/pull/24

The V1.0.0 update:

I believe I have finally fixed the phantom hit problems (I found that Squad changed a few of the layer numbers in 1.2.2.  My raytracing calls were giving phantom hits to the UI elements and the map view's planets because the layermask that worked fine in 1.1 wasn't right for 1.2.2.)

Since the phantom hits problem was the main thing making me consider the mod "not ready", and tha'ts been fixed - I now am moving it to version 1.0.

(1.0 also includes a few minor tweaks to the laser visual animation you should barely notice.)

 

Releases, from major download sites:

ZIP Download (See release page above):

Source Code Hosting site:

* https://github.com/Dunbaratu/LaserDist

License:

* GPL v3

Copyright (C) 2014,2015,2016 Steven Mading (aka Dunbaratu on Github)
[email protected]

100x model


This is a very small plugin.  It makes a KSP Part that measures straight line distances by laser.

The "Beamer 100x Disto-o-meter" Part aims a laser in a lin and then measures the distance in meters to the first object the laser hits.  The result is displayed in the right-click menu for the part and can be read by kOS (and other?) script mods.

200x model

The "Beamer 200x Bendable Disto-o-meter" Part is the same as
the 100x part described above, but it lets you choose a
deflection angle with the "Bend X" widget on the slider,
(or by using SETFIELD in kOS on it), and the laser can dynamically
change it's aim along this one axis.

500x model

The "Beamer 500x Aimable Disto-o-meter" Part is the same as
the 200x part described above, but it lets you bend the laser
in two different axes, to aim it whereever you like within
a zone, using both "Bend X" and "Bend Y" settings.


All models:

The laser can work over long distances - here it's measuring the
distance from a Kerbin orbit vessel to the Mun:

 

 

The direction of the laser is whichever way the laser gun is pointed when you mounted it on the craft.

### Information about the data fields the part displays:

![LaserDist Rightclick panel 1](rightpanel.png)

  • KSPField: 'Distance' is a float - the number of meters being shown in the display.  It's -1 if there is currently no hit.
  • KSPField: 'Hit' is a string - the name of the object being hit.
  • KSPField: 'Enabled' is a bool - true if the measuring device is on.
  • KSPField: 'Visible' is a bool - true if the laser should be drawn when it's activated or false if it should be (realistically) invisible because, hey, it's coherent light and it's not supposed to be seen from the side.
  • KSPField: 'CPUGreedyPercent' is a float (called "CPU hog" in the GUI) ranging from 0.0 to 20.0.  It's how much of a single physics tick of time this mod will allow itself to consume during a single Update.  If it hasn't gotten a good enough answer within that much time, it will wait until the next update to continue the calculation.
  • KSPField: 'UpdateAge' is an integer - It's how many Unity Updates (animation frames, basically) it's been since the value you are seeing was calculated.  Because of the logic of CPUGreedyPercent (see above) sometimes the value being displayed is stale by a few update ticks and shouldn't be trusted until the next time that UpdateAge becomes zero again.  If you're in a situation where this mod needs to spend more than 1 update of time to get to a good answer for the distance, you'll see this value spinning a bit, quickly going 0,1,2,3,0,1,2,3,0,1,2,3...etc.  When you see that, only when it hit the zeros was the distance value perfectly correct at THAT moment.
  • KSPField: 'Bend X' is a float - the number of degrees the laser is deflected in its relative Yaw direction, if it's the type of laser that can be deflected.

  • KSPField: 'Bend Y' is a float - the number of degrees the laser is deflected in its relative Pitch direction, if it's the type of laser that can be deflected.

  • KSPField: 'Max Bend X' is a float - the range of bending the laser can do in its relative Yaw direction.  If this is zero, then the laser cannot bend that way.  The range is always centered at zero, plus or minus this number.

  • KSPField: 'Max Bend Y' is a float - the range of bending the laser can do in its relative Pitch direction.  If this is zero, then the laser cannot bend that way.  The range is always centered at zero, plus or minus this number.

     

Note: The higher that CPUGreedyPercent ("CPU hog") is, the less likely it is that UpdateAge will ever be nonzero, but the bigger hit your framerate might take.

### How to Mount it.

![icon of Electronics node](http://wiki.kerbalspaceprogram.com/w/images/d/dd/Electronics.png). The Beamer 100x Dist-o-Meter is located in the "Electronics" tech node of the career tech tree. It's a 300-point node on the tree so you might not have it yet in a new fresh career game.

It appears in the science tab of the parts bin.

The Laser can be mounted anywhere as a surface-mount item.  Take care to note the orientation of the laser emiiter.  (KSP lets you fine-tune the rotation of a part by using the SHIFT key while you hit the WASDQE keys.)

The Laser will bounce back and give you a distance measurement when it encounters *ANY* object, including parts of your own craft.  So take care to mount it somewhere where the laser beam will have a clear line of sight without obstruction.

To ensure a good mounting point, you can use "tweakables" to enable the laser and make it show up in the VAB to look and see if you have it aimed well.

### Lightspeed

Note that if you use it to measure the distance to a far away body (i.e. like aiming it at Duna from Kerbin), the mod does take into account lightspeed.  You have to hold the laser on an object steady and unchange for the entire duration of time it takes for lightspeed delay to bounce the signal back or you won't get a measurement, so using it at that great distance will be very hard.

### How do I use it from my script then?

Quick synopsis:

Getting a handle on the laser, then turning on the laser using that module:

SET laser_module TO SHIP:MODULESNAMED("LaserDistModule")[0].
IF not laser_module:GETFIELD("Enabled") {
  laser_module:SETFIELD("Enabled",true).
}

Getting a reading from the laser:
 

SET laser_module TO SHIP:MODULESNAMED("LaserDistModule")[0].
PRINT "Laser distance is measuring " + laser_module:GETFIELD("Distance") + " meters."
PRINT "Laser is hitting: " + laser_module:GETFIELD("Hit").

Transforming the laser reading into a 3-D co-ordinate:

The part model is designed such that emitter of the laser line is located exactly at the part's local transform origin position, aimed along the part's ``facing:vector`` unit vector, allowing you to get its 3D vector position from a script like so, *Provided you haven't bent the laser with the ``"Bend X"``
or ``"Bend Y"`` settings.  If you have bent the laser, then you need to apply
these offset angles yourself with an ANGLEAXIS rotation in kOS*:

SET laser_module TO SHIP:MODULESNAMED("LaserDistModule")[0].
set dist to laser_module:GETFIELD("Distance").
set emitter_position to laser_module:part:position.
set emitter_unit_vec to laser_module:part:facing:vector.
set laser_hit_position to emitter_position + (dist * emitter_unit_vec).

Laser_hit_position is now a 3D vector position of where the laser hit something

To do the same general thing but when the ``Bend X`` and ``Bend Y`` fields are nonzero,
rotate the vector around the part's facing unit vectors:

SET laser_module TO SHIP:MODULESNAMED("LaserDistModule")[0].
set dist to laser_module:GETFIELD("Distance").
set emitter_position to laser_module:part:position.
set emitter_center_vec to laser_module:part:facing:vector.
set x_bend to laser_module:GETFIELD("Bend X").
set y_bend to laser_module:GETFIELD("Bend Y").
set bending_rotation to
      angleaxis(x_bend, laser_module:part:facing:topvector) *
      angleaxis(y_bend, laser_module:part:facing:starvector).
set emitter_unit_vec to
      bending_rotation * emitter_center_vec.
set laser_hit_position to emitter_position + (dist * emitter_unit_vec).

.

### Part modeling help?

I am aware that the artwork on the model isn't pretty.  I'm a programmer, not a graphic artist, and I don't have experience with things like Maya and Blender.  In fact I just made the model by slapping together some stretched Cube and Cylnder objects in Unity itself, without the aid of a modeling program.  The model is good enough to work with, but I'd be happy to have someone better at art redesign the model.  I included the model in the github directory if you want to have a look.

Edited by Dunbaratu
update that works on KSP 1.10.x and KSP 1.11.x
Link to comment
Share on other sites

1 hour ago, rasta013 said:

Thank you so much for resurrecting this.  I had really hoped to see this part come back around as I've used it way too many times to count for so many little things.

ditto. great to see this updated and continued! Especially now that scatter can be collided with using the recent versions of Kopernicus :P

Link to comment
Share on other sites

On 6/12/2016 at 11:35 PM, Gaiiden said:

ditto. great to see this updated and continued! Especially now that scatter can be collided with using the recent versions of Kopernicus :P

I tried looking into installing Kopernicus when I found out that it makes scatter rocks solid, but as near as I can tell it's a mod that mainly replaces the stock solar system with different ones.  Is there a way to install it JUST to get the solid scatter rocks effect, but have it do that in the stock KSP solar system?  Or does it only work for solar systems that explicitly enabled the solid scatter option with some additional data that stock KSP doesn't have?

 

Link to comment
Share on other sites

7 hours ago, Steven Mading said:

Or does it only work for solar systems that explicitly enabled the solid scatter option with some additional data that stock KSP doesn't have?

I think it's this, but I'm not sure. That or enhancement packs like Proot's KSPRC

Link to comment
Share on other sites

This looks very useful especially for lander and rover scripts.

@Steven Mading Can I make a suggestion? Two extra KSP tweakable fields for X and Y deflection of the laser pointing vector (limited to say ±30° - there would be a trade-off between max deflection and precision of dragging a tweakable slider) would be useful for writing scanning code or for adjusting the aim of the laser "in the field" rather than using the (fiddly) rotate gizmo in the VAB/SPH. From a quick peek at the code it should just be a matter of adding the two fields then applying them as a rotation when calculating the pointing vector in the PhysicsRaycaster method.

Link to comment
Share on other sites

1 hour ago, Kartoffelkuchen said:

This is awesome! :)

Why isn't this directly integrated into kOS?

Because it can be useful outside of kOS too.

3 hours ago, Aelfhe1m said:

This looks very useful especially for lander and rover scripts.

@Steven Mading Can I make a suggestion? Two extra KSP tweakable fields for X and Y deflection of the laser pointing vector (limited to say ±30° - there would be a trade-off between max deflection and precision of dragging a tweakable slider) would be useful for writing scanning code or for adjusting the aim of the laser "in the field" rather than using the (fiddly) rotate gizmo in the VAB/SPH. From a quick peek at the code it should just be a matter of adding the two fields then applying them as a rotation when calculating the pointing vector in the PhysicsRaycaster method.

Infernal Robotics is why I didn't do that.  I figured you can attach the part to a swivel mount and tell IR to move it.

Link to comment
Share on other sites

Regarding the above suggestion, I would like to see the ability for the laser to have a 'tweakable' that would allow the beam to 'sweep' automatically a set amount.  If the parameter was 30 deg, then the unit would basically scan from +15 to -15 deg from the installed zero point.  The readout could be as simple as aim angle and dist.  Nothing fancy, just would be nice to be able to use single unit for scanning.  I dont know how many scans per second it is currently doing, but if a second part could be devised, the scanning laserdist, it would be sufficient to divide the number of scans into the total sweep angle desired.

 

Just a thought.

This would mimic equipment that I have used in the past and would certainly be much used and appreciated.


Cheers and again, thanks for keeping this going.

Link to comment
Share on other sites

Okay, so there's a lot of interest in the aimable laser.  I sort of didn't want to because moving parts aren't something I want to animate, and we already have IR for that.  There's also the difficulty of taking time to get the laser moved to the desired position rather than it moving instantly, and again, IR already would deal with that.

But what about this:  A second laser part maybe a bit higher up the tech tree that comes with a presumed prism inside that can bend the light somewhere in a small arc like 20 or 30 degrees or so.  I'd avoid the issue of having to animate it by just drawing the part as a sort of black box with the laser light coming out of the box.  You assume there's some sort of prisms inside bending the light, but don't get to see it happening.  All you see is that the laser line emits out of the boxat a skewed angle when you set the deflectors to a nonzero value.

Link to comment
Share on other sites

Sounds great.  All movement animations would be 'in our mind' hidden in the black box.

That is 'sorta' the way that some lidar works anyway.

yup, sounds like a good start and make life easier.

Thanks and looking forward to any and all parts coming our way.

Cheers.

Link to comment
Share on other sites

Don't know how I missed this mod. I was just wanted to ask what happened with this mod, it was introduced quite long time ago in some video showing using laser measurements to level out landing gears when you need to land on uneven terrain. That was created with help of IR parts.
And found out that is released by accident, looking for something else in release thread.

This small part offer endless of possible usage, can be useful without kOS too. Also, I agrre with decision, better to spend free time on other stuff/features than to develop animated part. Although animated parts look cool, it is not of highest importance for overall usage of mod. Having ability to change laser angle in range of +/-30 degree sounds reasonable. Description that angle change is done with mirrors/prisms inside laser part is immersive enough for a game even without animations.

Also, there is decent number of other animated parts that can be used on craft and each of them is possible performance eater and kraken caller.

Link to comment
Share on other sites

Just pushed an update to the hosting sites, for that bendable laser idea people discussed above.

v0.9.1.0


New:
* Added 2 new laserdist parts that are capable of bending their lasers.  One only bends horizontally, and the other bends in 2 axes.


Known issues:
* Some of the textures on the new parts refuse to render in the little icons in the parts bins in the VAB, but they show up correctly once picked up and moved into the assembly area to attach to a vessel.  In the icon view, the part looks all white.
* All the laser parts that are capable of bending lasers must bend them with the same max range for x as each other, and the same max range for y as each other.  You can't have one laser that bends from, say, -20 to +20 and another one that bends from -10 to +10. If you edit the part.cfg to violate this rule, the context menus get screwy.  See the large comment in the part.cfg files about this if you want to edit the part.cfg files, or use ModuleManager to edit them.

 

Here is a short video clip showing one of the aim-able lasers being script-controlled:

 

Link to comment
Share on other sites

1 hour ago, Steven Mading said:

Known issues:

* Some of the textures on the new parts refuse to render in the little icons in the parts bins in the VAB, but they show up correctly once picked up and moved into the assembly area to attach to a vessel.  In the icon view, the part looks all white.

I'm certainly no expert, but when I imported these into Blender, it looks like you do not have proper KSP shaders assigned... ??
 

Link to comment
Share on other sites

Encountered small glitch as album shows. Not a big deal, workaround is easy, just avoid de-attaching parts in symmetry while laser beam is visible and turned on. Don't know if matters but I'm forcing DX11 on Windows platform. Also using stock bug fixes that could be related with this. Sorry, I didn't have a time to recreate this with few mods as possible to be sure that there is no mod conflict somewhere.

 

 

Link to comment
Share on other sites

4 hours ago, Stone Blue said:

I'm certainly no expert, but when I imported these into Blender, it looks like you do not have proper KSP shaders assigned... ??
 

So is there a rule that I'm not allowed to use my own shaders?  They have shaders, but not the ones provided by KSP.

13 minutes ago, WildLynx said:

Relative to pre KSP 1.x.x model, laser shines backwards.

Not a bug.  A deliberate change so that now part:facing:vector is in the laser direction.  Before, it wasn't.

It would be more accurate to say it *used* to be backward, and isn't anymore.  It should only affect you if you're using an old ship from before, which I figured shouldn't be a problem since it had gone almost 2 years being unusable.

2 hours ago, kcs123 said:

Encountered small glitch as album shows. Not a big deal, workaround is easy, just avoid de-attaching parts in symmetry while laser beam is visible and turned on. Don't know if matters but I'm forcing DX11 on Windows platform. Also using stock bug fixes that could be related with this. Sorry, I didn't have a time to recreate this with few mods as possible to be sure that there is no mod conflict somewhere.

 

 

This was a known problem, but I forgot to mention it.  The problem is that when I tell it to trap all attach/detach events so I can turn off the laser lights when it happens, it doesn't generate an event for each detached part in the set of symmetry parts - it just sends me the one event for the one part you detached.  I'm not sure if there's a way to find all the other parts that go with that event.

Link to comment
Share on other sites

6 minutes ago, WildLynx said:

Looks like I have to use KAS/KIS to re-position them now.

Actually it was working for me (as laser pointer for small tugs, etc ...) until 1.0 hit us. Not that long ago.

Also a few questions:

Those *.meta and *.mat files are used or it's leftover form making a model?

laserdist500* also used?

laserdist500.tga is used for rendering the laserdist 500x part.  The other ones are probably not needed.

For some unknown reason, when I only put laserdist500.tga in the one directory for the one part that uses it, the other two threw an error on loading.  It don't get it, since they didn't use that texture - only the 500x part does.

 

meta might not be needed.  It's not clear to me which of the files that PartTools spits out is actually needed and which aren't,

 

Sorry about turning the part around.  I seriously didn't think anyone was succeeding at using it before (the raycasting at other ship parts really was unreliable) so I didn't think it would matter if I finally fixed the part orientation.

Edited by Steven Mading
Link to comment
Share on other sites

11 minutes ago, Steven Mading said:

This was a known problem, but I forgot to mention it.  The problem is that when I tell it to trap all attach/detach events so I can turn off the laser lights when it happens, it doesn't generate an event for each detached part in the set of symmetry parts - it just sends me the one event for the one part you detached.  I'm not sure if there's a way to find all the other parts that go with that event.

Not a big deal with it, parts do their jobs regardless. Maybe is is good idea to ask @Claw about it ? He already solved a lot of symmetry issues trough stock bug fixes, maybe he knows more about it.

Link to comment
Share on other sites

The .meta & .mat files are just metadata files left over from Unity... These do not need to included in public release packages... They arent necessary for KSP itself, and can be deleted... All KSP needs is: a model (.mu), textures (tga/png/mbm/dds), and a .cfg...I'm not sure if the .meta & .mat should be kept with the Unity files in a source package, however...??

Link to comment
Share on other sites

I think it's not throwing a detach because the part doesn't get detached, it just gets deleted.

I did notice in your source that you don't have an OnDestroy method. You could also put code in there to delete/disable anything that should no longer exist when the part is destroyed. The game event firings aren't always called everywhere (or aren't always what you expect), but OnDestroy is always called by Unity. There are also some editor unique calls that happen that may give you what you want. In any case, you could also throw a debug log statement in there just to be sure it's calling things when you want/expect, depending on your desired outcome.

Additionally, you might want to unregister your game events somewhere when the part is deleted, otherwise those game events will start to pile up (or leak, if you want to call it that) and the game will try to call them.

Link to comment
Share on other sites

Okay, after playing with it a while longer, I've come to the conclusion that when displaying the parts in the parts bins, the game turns off pretty much *all* the lighting effects on the rendering, leaving you with only one kind of render effect left - the image texture render.  Everything else is disabled.  That's why my parts show as all-white in the parts bin.  It's NOT because they're using materials outside the KSP-provided materials because the problem persisted even after I switched to using the KSP-provided material shaders.  It's because I was trying to render by light effects alone with no texture image selected. (i.e. flat grey, which is then tinted by selecting colors for specular, diffuse, etc effects.  But in the parts bin, all those effects are gone and all that is left is *just* the texture image effect.  So if you don't color your part by use of a texture image, it doesn't get colored at all in that view.)

 

Edited by Steven Mading
Link to comment
Share on other sites

On 6/18/2016 at 2:12 PM, Claw said:

I did notice in your source that you don't have an OnDestroy method. You could also put code in there to delete/disable anything that should no longer exist when the part is destroyed.

Adding an OnDestroy() that calls OnLaserDestroy(this.part) seems to have caught all the places where it wasn't caught before.  Thanks.  It also seemed like a good place to remove the game event hooks.

 

Link to comment
Share on other sites

Small fix update released.  Non-essential little visual tweaks mostly.


v0.9.2  Just Small Visual Bug Fixes
-----------------------------------

Fixed:
* The laser beam lines now properly disappear when you remove symmetry parts in the VAB or SPH.
* Parts now render with color in the VAB/SPH parts bin.  Appearently even if all you want is a simple flat monocolor shader, you sti
ll need to use a texture image file for it in the way KSP works.  For example, to make a simple bland flat surface in color RGB(1,0,
0), you'd have to do it by making a small image file consisting of 1 single pixel in color RGB(1,0,0), and using that in a texture.
 Apparently the only shader effects that KSP enables when rendering the part icons is the texture image mapper.  All other aspects o
f shaders besides that one feature are turned off in the icon view, even for the shaders that come from KSP itself in PartTools.

 

Edited by Steven Mading
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...