Calling ship:bearing/heading causes "Look rotation viewing vector is zero" Unity error.
ngx-ree opened this issue · 7 comments
Calling ship:bearing or ship:heading causes "Look rotation viewing vector is zero" Unity error, which is written to console log. Not a problem unless it is called in loop, then it spams and slows down quite noticeably.
Just wondering what did you expect SHIP:BEARING
or SHIP:HEADING
to return because generating an error is indeed what should happen kOS simply isn't passing this error fully back to the user and crashing there script.
I don't really understand your question, please explain. I expect those functions to return current ship bearing and/or heading values, which they actually do. However, they also produce Unity error which, I believe, is not intentional. If I run those functions in loop, spamming console log with error messages slows the whole process down.
I don't understand, why do you write that "generating an error is indeed what should happen"? Why they should generate error?
The documentation on vessels it notes that the BEARING
and HEADING
suffixes return the bearing/heading from the ship's current position to the thing you are calling the suffix on. So by doing SHIP:BEARING
or SHIP:HEADING
you are asking kOS in which directly do I need to go to get from where my ship currently is to where my ship currently is. This involves trying to compute the heading of a vector of zero magnitude which in turn causes unity to error though not fatally and that unity error is something kOS should catch and then pass on to the user in the form of crashing there script.
To get the current compass heading as measured on the navball there are three ways to do this. The first would be to generate a geocoordinate reference on the north pole and then use the BEARING
suffix on that as an indirect way to query in which direction the vessel is currently pointed. The second would be to calculate your current heading based on SHIP:FACING:FOREVECTOR
. The third method is to make use of KSlib's lib_navball.ks which contains functions to compute the current compass heading of the ship among other things.
So it sounds like the real error is just that kOS doesn't protect against the chance someone could call :BEARING
or :HEADING
in a way that gives a zero length vector. It should still error, but it should detect the problem and give a meaningful error.
It makes more sense to me now. I always automatically thought bearing is referenced to north pole of current body and heading is where the ship is facing. Will try library you linked, thank you for explanation.
I just still don't understand one thing. If I open kOS terminal of some ship and simply write "print ship:bearing" or "print ship:heading" it returns some numbers (along with error message in console). How are those returned numbers computed? ship:heading seems to depend on current camera angle. Sorry if this is already explained elsewhere, I couldn't find anything so far.
From what I remember SHIP:HEADING
returns a number close enough to zero you can simply treat it as returning zero, the slight differences in the non-zero value from call to call are likely either due to the vessel rotating or time passes. as to why you get near-zero value from kOS for SHIP:HEADING
that likely the result of floating point noise in some of the operations that are generating a near-zero result as in spite of the error unity is still trying to supply an answer.
Where as SHIP:BEARING
more or less returns something that with a bit of effort you could convert into the compass heading but that is down to it comparing the bearing to the north pole against the zero result from SHIP:HEADING
though despite being mostly correct you shouldn't use it on account of the error it you get when called.
There are several different ways to compute those values one such method is found in the above link I supplied from which you can get to both the library (code) and documentation for lib_navball.ks. But that is not the only method I want to say internally kOS uses a unity function for these operations supplying a quaternian that among other things define the local north and up values for SHIP
along with the vector pointing from the ship's current position to the position of the thing you called the suffix on how that math works is beyond me.
As to what kOS should do I see two options each with there own pros and cons. The first option as I noted above is to detect the error and crash the script with a helpful error message. The other option is to detect the error and then return zero. The advantage of the crash is that we are able to provide direct help in the error message, the down side is not many people new to kOS appear to read or understand the errors so possibly more complaints. Returning zero on the other hand means no crashes though it would force people likely new to programing to do some debugging if they made assumptions about those suffixes with out checking first. Either way this will be a breaking change as I know of at least one person who was making use of SHIP:BEARING
to get the current compass heading of there vessel though they stopped after noticing the errors in the log.
I understand this is much deeper problem rather than some simple bug. I'll try to investigate and avoid using plain heading and bearing functions as they seem to cause tricky troubles.
About handling the error, maybe some warning message (similar to that one about SAS and steering lock struggle) could be helpful. It wouldn't break the script and wouldn't return confusing zero value either.
Many thanks for your help!