To add on the possible improvements, there's a few
relatively easy way to calculate collision risks... I've found myself in some precarious situations a few times so far because my target ship had moved behind the planet or moon while I was in transit... For example, warping from Jupiter to ISS directly or from Pluto to KSC, the station or surface base can move during the transit time. One manual way to prevent this is to go at the surface base and use the proper visualization option to track the luminal ship before it goes to warp and verify that it is sufficiently above the horizon prior to jumping to light speed.
However, it could be possible, at least for surface bases, to compute the altazimutal position of the warping ship relative to the target base and issue a warning if the ship is too low or below the horizon, as this would result in contact with the surface or atmosphere prior to arrival at the target base.
Also, it would also be interesting to issue a warning if the target altitude above the planet's surface or the base is within the atmosphere... Reentry at 50 km/s could have nasty effects on some ship.
As for collision detection with moons, planets and such bodies, a check could be performed for any planets and moons which have a bearing close to the target planet bearing, and which are less distant from the ship than the target planet or moon. This would yield a list of possible hazards within a forward warning cone. From this list, after going to warp, you check the closest approach altitude (periapsis + body radius) for every object in the forward hazard list and if it goes below a certain value, say 1000 km, you either drop out of warp (simple) or alter trajectory with a new heading to clear the conflict.
To lighten the load on the CPU and make the MFD operate well even with slower computers, you can do the suggestions I made and along Mogeley's proposal with this type of process:
Pre-Warp:
- If the target is a base:
+Calculate the current altazimutal coordinate of the warping ship relative to it's target
+ If the ship is too low or below the horizon of the target base, set a warning flag
+ If the target altitude is below the atmosphere thickness of parent object, set a warning flag
- Get the bearing to target object
- Get distance to target object
- If the target is a planet or moon:
+ If the target altitude is below the atmosphere thickness, set a warning flag
+ Calculate the orbital velocity for target object at stopping distance from target with sqrt(gravitational constant * planet Mass / stopping distance)
+ Calculate the bearing offset angle (atan(stopping distance/distance to planet)
+ Calculate the offset sign (prograde/retrograde)
+ Update the target bearing with offset
- For every planet and moons in current star system:
+ Get bearing to planet
+ Get distance to planet
+ If Bearing to planet is close to target bearing AND distance to planet is less than the target object, add to the stack of Nav Hazards
- Reorder the Nav Hazards stack in increasing order of distance from the ship.
- Set a collision avoidance pointer to the first (and nearest) hazard in the stack
For every Orbiter frame during near-luminal operation:
- Check the Nav hazard object on the list specified by the collision avoidance pointer for closest approach distance (current periapsis + object radius)
- If closest approach is below the safety margin, either alter course or do an emergency stop
- If bearing to current Nav hazard object is out of the warning cone, remove it from the Nav hazard list
- Set the collision avoidance pointer to the next nearest object on the nav hazard list, or if at the end of the list, set it back to the nearest nav hazard object...
Doing so splits up the collision avoidance and detection work to a little every frame instead of everything every frame, removes objects that no longer could pose a threat and increase time available for collision avoidance of objects near the destination as you get closer...