Jump to content

Auto-lights at night


DJ Reonic

Recommended Posts

I know solar panels have a mechanic for detecting sunlight exposure, so maybe that mechanic could be used to activate the lights. (if Sun exposure = 0, then activate lights) or something like that. If I had the time, I'd take a crack at it myself. Thanks for looking into it.

Link to comment
Share on other sites

Solar panels, wouldn't work, that was my first thought, panels obscured would think it was night. 

I suppose you could look at the increase in Electriccharge, 

 but even that is tricky, how do you decide if you are in darkness?  charge is negative, In sunlight charge is positive.

I might try this.

if  charge is negative and ec > 25% turn on lights

if charge is negative and ec < 25% turn off lights (to conserve power)

if charge is positive and ec > 25% turn off lights ( in sunlight )

Of course that doesn't account for RTG's

 

 

The other alternative, is to make a light that has a solar panel attached, if the solar panel has flow turn the light off, if it doesn't turn the light on. 

But that requires creating a special part, and that I'm no good at. I'll try the module thing though.

Edited by genericeventhandler
Link to comment
Share on other sites

The issue with the inverted solar panel design is that solar panel activity occurs based on craft orientation and occlusion, not just which side of the planet you are on.   That means lights could turn on even on the light side of the planet, depending on craft orientation.

You would have to develop occlusion detection between the sun and the craft, which is possible but it's not as simple as re-using existing code in the game.  The various antenna mods do this to determine connectivity, so rest assured it is possible if someone is willing to attempt it.

Edited by Alshain
Link to comment
Share on other sites

49 minutes ago, Alshain said:

The issue with the inverted solar panel design is that solar panel activity occurs based on craft orientation and occlusion, not just which side of the planet you are on.   That means lights could turn on even on the light side of the planet, depending on craft orientation.

You would have to develop occlusion detection between the sun and the craft, which is possible but it's not as simple as re-using existing code in the game.  The various antenna mods do this to determine connectivity, so rest assured it is possible if someone is willing to attempt it.

I quite like the idea of individual occluded lights turning on though,  sometimes it's tricky doing eva with the sun behind the craft.

Link to comment
Share on other sites

18 hours ago, DJ Reonic said:

Are there any mods out there that include the ability to set lights to activate when a ship or station goes into a planet's shadow? I've been looking, but have yet to find one.

It's called planetshine so basically you have light when you are in the dark side but you might want to adjust the light levels.

Link to comment
Share on other sites

35 minutes ago, AlmostNASA said:

It's called planetshine so basically you have light when you are in the dark side but you might want to adjust the light levels.

PlanetShine doesn't do that.  PlanetShine renders reflected light from the daylight side of the planet (on the side of the craft facing the planet).  It's a good mod but it doesn't generate light on the dark side of the planet.

Edited by Alshain
Link to comment
Share on other sites

If you want you can use these two functions for the raytracing:

Spoiler

// return true if the ray 'dir' starting at 'p' doesn't hit 'body'
// - p: ray origin
// - dir: ray direction
// - body: obstacle
public static bool Raytrace(Vector3d p, Vector3d dir, CelestialBody body)
{
  // ray from origin to body center
  Vector3d diff = body.position - p;

  // projection of origin->body center ray over the raytracing direction
  double k = Vector3d.Dot(diff, dir);

  // the ray doesn't hit body if its minimal analytical distance along the ray is less than its radius
  return k < 0.0 || (dir * k - diff).magnitude > body.Radius;
}

// return true if the body is visible from the vessel
// - vessel: origin
// - body: target
// - return: true if visible, false otherwise
public static bool RaytraceBody(Vessel vessel, CelestialBody body)
{
  // shortcuts
  CelestialBody mainbody = vessel.mainBody;
  CelestialBody refbody = mainbody.referenceBody;

  // generate ray parameters
  Vector3d vessel_pos = vessel.GetWorldPos3D();
  Vector3d dir = (body.position - vessel_pos).normalized;

  // raytrace
  return (body == mainbody || Raytrace(vessel_pos, dir, mainbody))
      && (body == refbody || refbody == null || Raytrace(vessel_pos, dir, refbody));
}

// example usage
if (RaytraceBody(vessel, FlightGlobals.Bodies[0]))
{
  // sun is visible, turn off all the lights
}
else
{
  // sun is occluded, turn on all the lights
}

 

 

Link to comment
Share on other sites

22 hours ago, ShotgunNinja said:

If you want you can use these two functions for the raytracing:

  Reveal hidden contents


// return true if the ray 'dir' starting at 'p' doesn't hit 'body'
// - p: ray origin
// - dir: ray direction
// - body: obstacle
public static bool Raytrace(Vector3d p, Vector3d dir, CelestialBody body)
{
  // ray from origin to body center
  Vector3d diff = body.position - p;

  // projection of origin->body center ray over the raytracing direction
  double k = Vector3d.Dot(diff, dir);

  // the ray doesn't hit body if its minimal analytical distance along the ray is less than its radius
  return k < 0.0 || (dir * k - diff).magnitude > body.Radius;
}

// return true if the body is visible from the vessel
// - vessel: origin
// - body: target
// - return: true if visible, false otherwise
public static bool RaytraceBody(Vessel vessel, CelestialBody body)
{
  // shortcuts
  CelestialBody mainbody = vessel.mainBody;
  CelestialBody refbody = mainbody.referenceBody;

  // generate ray parameters
  Vector3d vessel_pos = vessel.GetWorldPos3D();
  Vector3d dir = (body.position - vessel_pos).normalized;

  // raytrace
  return (body == mainbody || Raytrace(vessel_pos, dir, mainbody))
      && (body == refbody || refbody == null || Raytrace(vessel_pos, dir, refbody));
}

// example usage
if (RaytraceBody(vessel, FlightGlobals.Bodies[0]))
{
  // sun is visible, turn off all the lights
}
else
{
  // sun is occluded, turn on all the lights
}

 

 

Cheers for that, I have a semi working implementation now :) unfortunately I can't test until tomorrow night, the source is available at https://github.com/genericeventhandler/AutomaticLights if anyone feels brave enough to compile it and try it out. 

GE.

 

Link to comment
Share on other sites

On July 25, 2016 at 10:26 AM, Alshain said:

PlanetShine doesn't do that.  PlanetShine renders reflected light from the daylight side of the planet (on the side of the craft facing the planet).  It's a good mod but it doesn't generate light on the dark side of the planet.

Theoretically yes, but if you abuse the sliders it's simply light everywhere, always. KSP streamers using PlanetShine seem to do that a lot. And I sort of get it - it's one thing to play while fumbling around in pitch black, but something else to watch someone else do it

Edit - er, maybe I'm thinking of Scatterer? If only we could delete posts

Edited by fourfa
Link to comment
Share on other sites

5 minutes ago, fourfa said:

Theoretically yes, but if you abuse the sliders it's simply light everywhere, always. KSP streamers using PlanetShine seem to do that a lot. And I sort of get it - it's one thing to play while fumbling around in pitch black, but something else to watch someone else do it

Edit - er, maybe I'm thinking of Scatterer? If only we could delete posts

KSP Streamers use this, it was made for that purpose:

 

Link to comment
Share on other sites

Hi, 

I've compiled a version and have been testing it all weekend,   https://github.com/genericeventhandler/AutomaticLights/archive/master.zip   find the gamedata folder in there and extract it as usual. - compiled against 1.1.2 - but should be compatible with 1.1.3.

Add one of the copied lights to your crat, right click it and turn it on with the new button, if the EC is above 25% they will turn on when obscured from the sun, thanks ShotgunNinja,

Tested on the ground, underwater, behind kerbin and some extensive off roading on The Mun. 

GE

 

Link to comment
Share on other sites

  • 6 months later...

If anyone is interested, I've updated the code to 1.2.2

https://github.com/genericeventhandler/AutomaticLights

The lights now work correctly, and I've add an autominer and autogenerator modules as we.. 

  • AutoLights turn off and on when hidden from the sun, checks to see if there is enough ElectricalCharge left. 
  • AutoGenerator - turns the generator off if EC <50%  turns on if resource < 99%
  • AutoMiner - Bit buggy, but will turn resource harvesters on and off if the watched resource is low.  Add to an ore drill and watch ore. It will mine until full then stop, or EC is low.

In the Electrical folder remove the autogenerator and autominer if not required.

 

GE

 

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