Help with coordinate system conversion

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Help with coordinate system conversion

Postby misho » Wed Apr 22, 2015 4:24 pm

Hi Julio, and everyone

I am using Newton Game dynamics to create a spaceflight DLL module for a flight simulator (existing application). The coordinate system which is used in this flight simulator is "local-Topocentric" coordinate system, common to aircraft flying around the globe. In this coordinate system, wherever you are on the globe, attitude is always in relation to the horizon, and pointing North (level wings is 0 roll/bank, level nose is 0 pitch, and nose pointing north is heading/yaw 0 degrees). Position is specified by Lat, Long and Altitude, and attitude is specified in Roll, Yaw and Pitch. In addition, Pitch is allowed to only go from +90 deg to -90 deg. Moreover, the coordinate system is Left-handed, while Newton is right-handed (although I don't believe that is a huge obstacle, there is a simple matrix conversion for that). So, the ONLY way of specifying object position and attitude in FS is by Latitude, Longitude, Altitude (spherical coordinates), with Pitch Bank and Heading in local-topocentric coordinate frame.

So, basically, I need to transfer Newton coordinates to my flight simulator coordinates. In order to do that, I have to calculate spherical position. That's no problem, I already have that, I've set up my Newton Physics so that I have spacecraft rotate properly under Newton's orbital influence. However, I also have to, based on spacecraft's position, calculate for attitude conversion that is accepted by Flight simulator, since roll pitch and yaw are relative to topocentric coordinate system. For example, a pitch of 90 degrees in Newton would be pitch of 90 degrees in Flight simulator, but only on north pole. On the equator, this would be a pitch of 0 degrees, and on south pole, -90 degrees.

I was wondering if you could help me out with math - I'm stuck on it. I had limited success so far, I just need to be pointed in the right direction. I know there is a right combination of rotations out there, that will do the trick and convert from one coordinate system to another. Do you have any thought or pointer on how best to approach this?

Thanks in advance,
Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby Julio Jerez » Wed Apr 22, 2015 10:17 pm

why do you worry about the coordinate system?
The engine does no have coordinate system. just use yours

a matrix in newton is a 4x4 array of floats, the are call right front and up for convenience, by you can use the operations.
elemen[I][j] and use your own system.

The only problem you have to worried about is the construction coordinate system that some of the collision shapes use. for example a capsule; is the first road of a matrix.

see if this work for you, if you still want tow use tow different coordinate systems, I can show a very eassy way to do that,
The method is called Similar transformations, and si basically comes down to apply a rotation matrix
to move from one system to the other.

but first see if you can just use your coordinate system, pretending that new use your coordinate system, trust me it should work without problems
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with coordinate system conversion

Postby misho » Thu Apr 23, 2015 1:22 pm

Thanks Julio,

I must admit I am a bit confused... I thought Newton uses a straight-forward Cartesian x-y-z right handed system. At least, that is what is used in the tutorials as a norm (objects are placed in x-y-z space, rotations are defined around principal axes, and so on...). Is there a place where a coordinate system is defined for Newton world?

I my world set up in a similar way as the tutorials - from what you are saying, the world can be set up in any coordinate system we wish?

In any case, I'd like to explore both approaches - setting up Newton in my native (topocentric) coordinate system, and keeping Newton in Cartesian and converting to topocentric. I must admit that I find Cartesian a lot simpler to deal with, while making Newton work with topocentric would be far more elegant. Can you please shed some more light on this?

Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby Julio Jerez » Thu Apr 23, 2015 4:13 pm

no newton does no have a coordinate system preference.
It could be left or right handed, and the axis can mean what ever you want the to mean.
take for example a hinge joint, It use the for row of a matrix as the axis of rotation.
but you pass that axis yourself.
just try and pretending the coordinate system if you system, it should work.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with coordinate system conversion

Postby misho » Thu Apr 23, 2015 4:26 pm

I have to admit that I thought I had the problem well defined, but now I find it even more confusing. How can Newton know what I'm trying to do if I tell it that my spacecraft is at a certain Latitude, Longitude and Altitude? How can it know what these numbers are? Is there some kind of a structure in Newton that defines the type of coordinate system?

Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby Julio Jerez » Thu Apr 23, 2015 5:06 pm

is a Cartesian space. isn't it?
Or are you using a no linear coordinate system?
even long flight aerospace simulation still assume the terrain in on a flat surface.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with coordinate system conversion

Postby misho » Thu Apr 23, 2015 7:25 pm

No - the flight simulator I am using uses a SPHERICAL coordinate system, taking Latitude, Longitude and Altitude values as position around the Earth. In the simulator, user can go anywhere on Earth, the whole world is represented in a great and very accurate detail, and the world is definitely not flat, but a proper sphere. This flight simulator comes with an API that lets programmers write add-ons for it, and as such I am writing a spaceflight add-on using NGD.

The position is very easy to convert to Cartesian... I have that going and it is working well.

However - the attitude is a problem, because attitude (in relation to geodesic coordinate system) changes depending where in the world the spacecraft is.

Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby Julio Jerez » Thu Apr 23, 2015 8:44 pm

Ok I will post the steps for converting from one coordinate system to another tonight. I am at work now.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with coordinate system conversion

Postby misho » Thu Apr 23, 2015 8:46 pm

Great - thanks! I really appreciate it!

Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby misho » Mon Apr 27, 2015 2:26 am

Hi Julio,

Have you had a chance to take a look at this?

thanks,
Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby Julio Jerez » Mon Apr 27, 2015 9:43 am

Oh I forget this.

to convert a point to one coordinate system to another all you nee to do is to apply the appropriate rotation matrix.
say that p is a point in coordinate system P and q is a point in coordinate system Q
you set the identity

1) p = q * M

where M is the conversion matrix
to get matrix M is very simple

say that coordinate system P is right handed and system Q is left handed this meant the matrix will flip on axis this is M is of the form, let us chose x axis

-1 0 0 0
M = 0 1 0 0
0 0 1 0
0 0 0 1

the above matrix will have a negative determinate, which illegal but this is ok because, the matrix is never use is only use to convert points and matrices,

for a coordinate system like your, you make matrix M by expressing the axis of coordinated system P the in Q, say that in coordinate system P x is to the right, y is to the front and z is down. (is think this is you system) the M is given by

0 0 1 0
M = 1 0 0 0
0 -1 0 0
0 0 0 1

The above transformation will convert a point from Newton to your system.
to convert for the other way around you do the, form equation 1

2) p * M' = q

M' is the inverse of M, by since M is orthonormal then the inverse is the transpose, in the example

0 1 0 0
M' = 0 0 -1 0
1 0 0 0
0 0 0 1

now the only thing left is how to covert a matrix from one system to the other.
say a point in cordenade system q is given by

3) q = ql * Q

where pl is the local point an Q is is the transformation matrix, by substituting equation 3 is 1
you get

4) p = ql * Q * M

but the will make a illegal matrix because even those M is hortognormal, it is not positive, so we will apply some algebraic manipulations to make (Q *M) positive and orthonormal.
we can rotate an identity matrix any where because identity matrix do no change the value they operate on

5) p = ql * I * Q * M

now we can write as, the product of M by it inverse

6) I = M * M'

sinec I is orthirnoimal and Positive, then M * M' is also orhtonormal and positive.

7) p = ql * M * M' * Q * M

8) p = (ql * M) * (M' * Q * M)

an now you see that, that to get form system q to system p
all you do is

for point p = ql * M
for matrices P = M' * Q * M

also note that the product M' * Q * M is orthormal and positive, this is eassy to prove, by expandif teh products, but it si a long exersise.

what you do, is that you do not do the operation, you write the traformamtion manually bu having tow funtions.
Code: Select all
one to converting point from P to Q       [p = p * M
one for converting point form Q to P      [q = q * M']
one for conveting matrix for P ro Q        [P = M' * Q * M]
one for converting matrix form Q to P    [Q = M' * P * M]

you can put everythin into a class.
this is a genral syetem that cna convert any system to any other system.
But remember like I said before, this will work and I used this for writing plugins that export data for Video Games with different cordenates systems form modeling packages, but when using the engine you do not really need it if you simple pretend that the engine is written in your own system.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with coordinate system conversion

Postby JoeJ » Tue Apr 28, 2015 11:53 am

For example, a pitch of 90 degrees in Newton would be pitch of 90 degrees in Flight simulator, but only on north pole. On the equator, this would be a pitch of 0 degrees, and on south pole, -90 degrees.


This passage sounds to me like the flight simulator uses Euler angles to represent rotations.
Doing physics with Euler angles would not make sense, but you can do this with matrices as usual, and convert the matrices to euler to feed the flight simulator.

Newton has a utility for that, see below. What you need to know is the order of the angles.
Tho code is originally from me, and you give the order with 3 4bit numbers encoded in one integer.
Example:
0x012: x axis first, then y, z last
or
0x210: z first, then y, then x
0x120: y,z,x
...
Usually the right one can be found by trial and error.
If you want i can look if i also have the function to convert from Euler to matrix.

dMAtrix.cpp:
void dMatrix::GetEulerAngles(dVector& euler0, dVector& euler1, dEulerAngleOrder order) const
{
int a0 = (order>>8)&3;
int a1 = (order>>4)&3;
int a2 = (order>>0)&3;
...
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Help with coordinate system conversion

Postby misho » Tue Apr 28, 2015 12:55 pm

Julio, thanks very much! I will take a bite of this and digest it for a bit to see where it takes me. I'm sure I'll have more questions as I prod along. First, though, I have to upgrade to the latest Newton - I'm still on 2.35 :roll:

Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby misho » Tue Apr 28, 2015 1:00 pm

Joej, yes it does - but it is not that simple. The Euler angles change (with respect to Newton coordinate system) depending on what position on Earth the aircraft/spacecraft is. For example, if an observer was in Newton coordinate system, looking at the whole Earth, an aircraft flying straight and level above Antarctica would appear to be upside down to a Newton observer. So, for a pilot, the coordinate system is 0 pitch, 0 roll 0 yaw, but for Newton coordinate system, the airplane is inverted (roll 180 or some other combination of rotations)... and therein lies the problem.

Also - the simulator takes the attitude data (Euler angles) in a single API call over which I don't have any control, and applies the transformations internally. It does say that the rotations are performed internally in the order of YAW-PITCH-ROLL, but again, this happens internally. I have verified this by taking the manual control of the attitude, and indeed, only a rotation in first YAW, then PITCH and then ROLL takes me to the desired attitude.

The data I get from Newton simply has to be converted into a topocentric coordinate system and then passed on to API call, which should (hopefully) orient it properly. One caveat is, the PITCH data in simulator has limits -90<=0<=90, and Newton goes full 360 degrees...

I'm attaching an image to illustrate this better:
Image

Thanks for the input though, it is helpful!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 675
Joined: Tue May 04, 2010 10:13 am

Re: Help with coordinate system conversion

Postby JoeJ » Tue Apr 28, 2015 2:23 pm

I assume you have already solved the position, and only the orientation of the spaceship is the problem?

I have no experience with such a coordinate system, but to define orientation relative to latitude / longitude grid over a sphere, you might need a reference orientation depending on ship position, something like:

vector pole = ... // unitvector between poles
vector diff = shipPos - sphereCenter

// build 3 orthonormal axis of reference:
vector up = diff.Unit()
vector tangent = (up.Cross(pole)).Unit() // <-- OOOPS - bug fix here
vector north = tangent.Cross(up) // or vice versa

Then you make a rotation from this reference to Newton space ship,
and from this rotation you calculate the Euler angles that the game wants as input for orientation.

In theory this should be correct, but in practice it is instable at the pole positions (like a compass needle). No idea how the game or you can handle this case :?
Last edited by JoeJ on Tue Apr 28, 2015 4:57 pm, edited 1 time in total.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron