I mean, it’s your choice, but I really think that you’re overcomplicating things in this case.
Bear in mind that you need only keep one such temporary NodePath, so garbage collection needn’t be significantly affected.
You don’t have to: just set your NodePath to have the relevant HPR values (and position, if called for), tweak those values as you like (such as by setting the pitch to zero), and then call “getQuat().getForward()”.
However, as I said, it is your choice, and I’ve made my case, so let me try to help with what you’re asking:
I think that there are essentially two steps to add to your process:
- First, the calculation of the z-coordinate of your vector.
- And second, the calculation of the length of the horizontal component of your vector.
The first is fairly straightforward: it should be much as you already have for x and y, but using the pitch instead of the heading. Presuming that a pitch of zero corresponds to a horizontal vector, I think that this would mean that the z-coordinate would be calculated using sine.
The second may call for some explanation:
Consider an arbitrary vector:
(ASCII art time! :P)
z | /:
| / :
The more vertical the vector–the higher the z-value–the less horizontal it is. A fully-vertical vector has a horizontal length of zero, and a fully-horizontal vector has a vertical length of zero.
The horizontal component of the vector is, of course, composed of the x- and y- coordinates of the vector.
So, to account for this matter we can adjust the length of the x- and y- components of our vector.
Simply put, as we calculate the length of our vertical (z-) component via sine, we calculate (if I have it aright) the horizontal component via cosine.
This gives us a length for the entire horizontal component, not the individual x- and y- components. But since the previously-calculated x- and y- components are essentially the proportions that those components make up from a unit-length, we can just multiply each by the new horizontal length and get the final result!
Now, let me note: I haven’t double-checked any of this, so I may have made mistakes.