Converting magnitude to vector components?

Recommended Posts

Don't know if this is the right place to ask this, but I couldn't find any better.

A spacecraft is travelling at given velocity. Velocity vectors are = [-72.25886975259107, -2209.998264874299, 1.6282022342777325]. When I calculate the magnitude of it, I get 2211.18m/s. That's easy to do. But what if I have to calculate all 3 vector components when I only have magnitude?

Spacecraft bearing/heading is 90.389° so it's heading south-east, and its pitch is -1.725° which means it's going slightly "below the horizon"/towards the ground. I tried this formula: X=(magnitude)*(cos(heading)) to get one of the vectors but I know solution is incorrect because it's not equal to any of the vectors I wrote at the start. X=(2211.18)*(cos(90.389)=-15.01 .

I also tried a formula from here which goes x=ρsinφcosθ.

I used magnitude (2211.18m/s) as "ρ" value, and 90° for both θ and φ, since direction is 90° south of north pole, and 90° towards east too. So, x=2211.18*sin(90)*cos(90)*=-15,

the result was -15, which still doesn't match any of the vector components [-72.25886975259107, -2209.998264874299, 1.6282022342777325]

So how do I calculate it then?

I'm trying to create something with krpc mod, and a certain function requires velocity vectors as input, instead of velocity's magnitude (2211.18m/s).

Edited by Cannon
Share on other sites

Sounds like you have enough information to make a unit direction vector using the given angles, and multiply it by your magnitude.

Share on other sites

25 minutes ago, cubinator said:

Sounds like you have enough information to make a unit direction vector using the given angles, and multiply it by your magnitude.

I tried using those 2 formulas but it's not giving me the correct results

```X=(magnitude)*(cos(heading))
OR
X=(magnitude)*(cos(pitch))
OR
OR
OR
OR
x=(magnitude)*sin(pitch)*cos(pitch)```

I've tried all of the above combinations and none of them produced any of the correct numbers  [-72.25886975259107, -2209.998264874299, 1.6282022342777325]

Share on other sites

be sure to convert degrees to radians and vise versa as needed. most trig functions operate on radians.

also a vector cannot have a negative magnitude. so if you get one you might have a problem.

you need to compute each component separately.

i want to say z would be the sine of the pitch but im not 100% sure right now.

Edited by Nuke
Share on other sites

1 hour ago, Nuke said:

be sure to convert degrees to radians and vise versa as needed. most trig functions operate on radians.

also a vector cannot have a negative magnitude. so if you get one you might have a problem.

you need to compute each component separately.

i want to say z would be the sine of the pitch but im not 100% sure right now.

Yup, converted all to radians, but it still gives wrong numbers.
For example x=sin(heading) would be x=sin(90.389) or for radians x=sin(1.57), and the result of that is 0.99, doesn't match any of the extracted velocity vectors in  [-72.25886975259107, -2209.998264874299, 1.6282022342777325]

Here's part of my code too, it might help if you notice any errors:

```Triplet<Double,Double,Double> velocity = vessel.flight(refDelta).getVelocity();
System.out.println("vectors="+velocity); //Prints out  [-72.25886975259107, -2209.998264874299, 1.6282022342777325]

double vMag = Math.sqrt(velocity.getValue0() * velocity.getValue0() + velocity.getValue1() * velocity.getValue1() + velocity.getValue2() * velocity.getValue2()); //Calculate magnitude of velocity
float pitchA = vessel.flight(vessel.getSurfaceReferenceFrame()).getPitch();
System.out.println("pitch="+pitchA);
pitchA *= (Math.PI/180); //Conversion to radians

double vLat = vessel.getOrbit().getBody().latitudeAtPosition(vessel.position(vessel.getOrbit().getBody().getReferenceFrame()), vessel.getOrbit().getBody().getReferenceFrame()); //Get Latitude
double vLon = vessel.getOrbit().getBody().longitudeAtPosition(vessel.position(vessel.getOrbit().getBody().getReferenceFrame()), vessel.getOrbit().getBody().getReferenceFrame()); //Get Longitude

vLat *= (Math.PI/180); //Conversion to radians
vLon *= (Math.PI/180); //Conversion to radians

//Formulas for x,y,z vector calculation (I've tried with previously mentioned formulas too, didn't work)

System.out.println("pitch="+pitchA);
System.out.println("xx="+xxx);
System.out.println("yy="+yyy);
System.out.println("zz="+zzz);
System.out.println("xyz="+xyz);
System.out.println("vLat="+vLat);
System.out.println("vLon="+vLon);```

Edited by Cannon
Share on other sites

ignore my previous post, that was for 2d. i had to go do something and had to finish it up in a hurry, not to mention i had a cat in the way and i didn't have time to test it.

z = cos(pitch)

z = sin(pitch)

ignore the sine and cosine being reversed, i think im using a different handedness. this will convert heading and pitch to a unit vector indicating direction, then multiply that by magnitude to get velocity.

Edited by Nuke
Share on other sites

58 minutes ago, Nuke said:

ignore my previous post, that was for 2d. i had to go do something and had to finish it up in a hurry, not to mention i had a cat in the way and i didn't have time to test it.

z = cos(pitch)

ignore the sine and cosine being reversed, i think im using a different handedness. this will convert heading and pitch to a unit vector indicating direction, then multiply that by magnitude to get velocity.

z = sin(pitch)

Also, keep in mind that this assumes that you are using the local XYZ coordinates with respect to which pitch and heading are given. It will mean that your Y axis points North, X points East, and Z points to Zenith. This is fine, if these are the coordinates required, but might need to be adjusted for a different convention.

If your coordinates are relative to SoI, however, with Z or Y always pointing along the planetary axis, then you'll have to adjust the transformation based on where you are located. It's significantly more math to transform from local pitch and heading to SoI XYZ coordinates, so @Cannon if that's the conversion you need, please reply or mention, and I'll give you the steps. It's just too much to type out if it's not what you're looking for.

Share on other sites

8 hours ago, K^2 said:

z = sin(pitch)

Also, keep in mind that this assumes that you are using the local XYZ coordinates with respect to which pitch and heading are given. It will mean that your Y axis points North, X points East, and Z points to Zenith. This is fine, if these are the coordinates required, but might need to be adjusted for a different convention.

If your coordinates are relative to SoI, however, with Z or Y always pointing along the planetary axis, then you'll have to adjust the transformation based on where you are located. It's significantly more math to transform from local pitch and heading to SoI XYZ coordinates, so @Cannon if that's the conversion you need, please reply or mention, and I'll give you the steps. It's just too much to type out if it's not what you're looking for.

Z typically is defined to point downward in this convention, otherwise you're using a reversed coordinate system

Share on other sites

14 minutes ago, cubinator said:

Z typically is defined to point downward in this convention, otherwise you're using a reversed coordinate system

Note the sin/cos flip on X/Y coordinates and the fact that 0° heading points along Y because of it. This allows it to be right-handed with Z-up while heading still runs clockwise.

Effectively, the reflection across X=Y plane compensates for the reflection across the XZ plane that you get from heading running clockwise, which introduces the sign change for the Y component. Net result of these two reflections is a 90° rotation, so that heading of 0° ends up along Y instead of X. Which, I mean, Y might as well be North, right? So this is a bit unconventional in terms of notation, but perfectly serviceable.

Share on other sites

3 minutes ago, K^2 said:

Note the sin/cos flip on X/Y coordinates and the fact that 0° heading points along Y because of it. This allows it to be right-handed with Z-up while heading still runs clockwise.

Effectively, the reflection across X=Y plane compensates for the reflection across the XZ plane that you get from heading running clockwise, which introduces the sign change for the Y component. Net result of these two reflections is a 90° rotation, so that heading of 0° ends up along Y instead of X. Which, I mean, Y might as well be North, right? So this is a bit unconventional in terms of notation, but perfectly serviceable.

Ah, I got it - I missed that you had Y pointing north.

Share on other sites

9 hours ago, K^2 said:

z = sin(pitch)

Also, keep in mind that this assumes that you are using the local XYZ coordinates with respect to which pitch and heading are given. It will mean that your Y axis points North, X points East, and Z points to Zenith. This is fine, if these are the coordinates required, but might need to be adjusted for a different convention.

If your coordinates are relative to SoI, however, with Z or Y always pointing along the planetary axis, then you'll have to adjust the transformation based on where you are located. It's significantly more math to transform from local pitch and heading to SoI XYZ coordinates, so @Cannon if that's the conversion you need, please reply or mention, and I'll give you the steps. It's just too much to type out if it's not what you're looking for.

Well, here's what I'm actually doing, I make the rocket that's about to go through reentry and eventually land, with its apoapsis at 85k and periapsis at 20k. So I orient it towards prograde and then I extract  heading and pitch once it's facing prograde, because I assume that is also the direction of velocity vectors. But then the tricky part seems to be in choosing the right reference frame just as you suggested. Before I explain any further, here's the explanation of what each reference frame means:

So, my velocity in the program matches the in-game velocity if I use celestial body orbital reference frame, but my pitch and heading will only match the in-game pitch and heading if I use vessel surface reference frame.
I have tried to play around with those reference frames now, to see if I can get different results for my vectors.

If I extract pitch value using celestial body orbital reference frame and heading value using vessel surface reference frame I get 2 of the vectors very close to the correct values.
Here's what I get:

```pitch=-10.908204  //It doesn't match the in-game pitch which is about -1 or 0 at most.
vMag=2219.828828308464 //Matches the in-game orbital velocity
xx=3.2752230972366845
yy=2179.717671720176
zz=-420.07162662308826
vectors=[-416.6260029541221, -2180.3798267286315, 2.5713511279416803]```

Notice how "zz" and "yy" values are very close to the first and second value of vectors, even though "yy" is lacking the negative sign. But the "xx" is still not that close to the third number.
These are the formulas I used to get those results:
zz=|vMagnitude| * (sin(pitch))

Objectively, they're still not 100% accurate results, but are very close now. I'm running out of ideas what to try next though

Share on other sites

13 hours ago, K^2 said:

z = sin(pitch)

Also, keep in mind that this assumes that you are using the local XYZ coordinates with respect to which pitch and heading are given. It will mean that your Y axis points North, X points East, and Z points to Zenith. This is fine, if these are the coordinates required, but might need to be adjusted for a different convention.

If your coordinates are relative to SoI, however, with Z or Y always pointing along the planetary axis, then you'll have to adjust the transformation based on where you are located. It's significantly more math to transform from local pitch and heading to SoI XYZ coordinates, so @Cannon if that's the conversion you need, please reply or mention, and I'll give you the steps. It's just too much to type out if it's not what you're looking for.

i was all kinds of distracted yesterday. that was supposed to be a sin() i even edited it, but i guess i didnt post the corrections, will do now.

i usually break out a quaternion or matrix when i want to rotate something. eulers introduce all kinds of gimbal lock. im also not as familiar with orbital reference frames as i should be.

Edited by Nuke
Share on other sites

I finally figured it out. The problem was, I was taking my ship's current orientation as a prograde value, but it's never perfectly facing prograde in-game, there are always some micro movements and variations, that's why I couldn't get exact vectors.
Now I took the actual prograde vectors and converted them to heading and pitch like this:

And then just used the formulas I mentioned in my last comment and got velocity vectors with 100% accuracy

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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.