• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Programming the user aircraft body reference system

Messages
42
Country
wales
This is an extension to the original topic:
http://www.fsdeveloper.com/forum/showthread.php?t=11906

I am interested in programing the flight of the user aircraft to match an ai aircraft such that it occupies the same space, wiht the same pitch, roll and yaw with a given tolerance, on a visual frame by frame basis.

Help is needed to write the maths to do this. Foiter wrote: "Whatever you do with your aircraf the easiest and the most efficient way to compute its velocities is to use simple vector algebra. I assume your goal is to hold user craft at a certain minimal distance to the AI craft following AI craft's orientation. As you already noticed the most reasonable way to do that is to make your computations in the body reference sistem. So, the first and foremost thing to achive is to make your aircraft to follow the AI craft's orientation. If you want to make it intuitively clear avoiding quaternions, than I'd suggest to compute cross product of both aircraft's attitude vectors and normalize it. This will give you the rotation for the pitch and yaw. The roll rotation can be computed using scalar product of both Bank vectors. As long as you resolve this task you can proceed with the distance calculation. BTW, for the test purposes I suggest to use Slew mode."

From a smoothness point of view, simply copying the roll, pitch and yaw from the AI aircraft to the user aircraft was a very effective method. However that does not sound like the right thing to do achieve the altitude and position coordinates.

I'm afraid even simple vector algebra is beyond me, school days are a long time ago for me now. Perhaps if you were able to write a formula in terms of FSX simulation variables, I could implement that.

Thanks
Simon
 
Perhaps if you were able to write a formula in terms of FSX simulation variables, I could implement that.

Simon, I'm afraid the accomplishement of the task like that does require at least elementary knowledge of linear algebra. Providing you with the formulas won't help you much, because the coding as well as the debugging process will quickly turn all your efforts into hell, if you don't know what values to expect.

On the other hand giving you the code is no solution either, because it would require to give away nearly all dependency classes which have a certain commercial value. I'm afraid I can't distribute this even under GPL agreements.

What I can do for you is to give this piece of code of what I already explained you earlier.

////////////////////////////////////////////////////////////////////////////////
// BODY ROTATION VELOCITY (PITCH, YAW)
////////////////////////////////////////////////////////////////////////////////
fCosQx = cos(pkUserAC->m_kData.m_kBodyAttitude.x);
fSinQx = sin(pkUserAC->m_kData.m_kBodyAttitude.x);
fCosQy = cos(pkUserAC->m_kData.m_kBodyAttitude.y);
fSinQy = sin(pkUserAC->m_kData.m_kBodyAttitude.y);
D3DXVECTOR3 kLocalUserAcAttitudeVec = D3DXVECTOR3(fCosQx*fSinQy, -fSinQx, fCosQx*fCosQy);

fCosQx = cos(m_kData.m_kBodyAttitude.x);
fSinQx = sin(m_kData.m_kBodyAttitude.x);
fCosQy = cos(m_kData.m_kBodyAttitude.y);
fSinQy = sin(m_kData.m_kBodyAttitude.y);
D3DXVECTOR3 kLocalAcAttitudeVec = D3DXVECTOR3(fCosQx*fSinQy, -fSinQx, fCosQx*fCosQy);

D3DXVECTOR3 kLocalAcRotationVec = *D3DXVec3Cross(&kLocalAcRotationVec, &kLocalAcAttitudeVec, &kLocalUserAcAttitudeVec);
D3DXVECTOR3 kLocalAcRotationNormalizedVec = *D3DXVec3Normalize(&kLocalAcRotationNormalizedVec, &kLocalAcRotationVec);
D3DXVECTOR3 kBodyAcRotationVec = *D3DXVec3TransformCoord(&kBodyAcRotationVec, &kLocalAcRotationNormalizedVec, &m_kLocalToBodyTransformMatrix);
FLOAT fAngularVelocity = 132.1f*(1.0f - D3DXVec3Dot(&kLocalUserAcAttitudeVec, &kLocalAcAttitudeVec));
kBodyAcRotationVec = fAngularVelocity*kBodyAcRotationVec;

////////////////////////////////////////////////////////////////////////////////
// BODY ROTATION VELOCITY (ROLL)
////////////////////////////////////////////////////////////////////////////////
D3DXVECTOR3 kBodyUserAcBankVec = D3DXVECTOR3(cos(pkUserAC->m_kData.m_kBodyAttitude.z), sin(pkUserAC->m_kData.m_kBodyAttitude.z), 0);
D3DXVECTOR3 kBodyAcBankVec = D3DXVECTOR3(cos(m_kData.m_kBodyAttitude.z), sin(m_kData.m_kBodyAttitude.z), 0);
D3DXVECTOR3 kBodyAcRollVec = *D3DXVec3Cross(&kBodyAcRollVec, &kBodyAcBankVec, &kBodyUserAcBankVec);
D3DXVECTOR3 kBodyAcRollNormalizedVec = *D3DXVec3Normalize(&kBodyAcRollNormalizedVec, &kBodyAcRollVec);
FLOAT fRollVelocity = 132.1f*(1.0f - D3DXVec3Dot(&kBodyUserAcBankVec, &kBodyAcBankVec));
kBodyAcRollVec = fRollVelocity*kBodyAcRollNormalizedVec;

kSimWriteData.m_kBodyRotationVelocity = (bHoldAttitude) ? D3DXVector3<DOUBLE>(kBodyAcRotationVec.x, kBodyAcRotationVec.y, kBodyAcRollVec.z) : D3DXVector3<DOUBLE>(0, 0, 0);

kSimWriteData.m_kBodyVelocity = pkUserAC->m_kData.m_kBodyVelocity + D3DXVector3<DOUBLE>(kBodyAcToFormVelocity.x, kBodyAcToFormVelocity.y, kBodyAcToFormVelocity.z);
SimConnect_SetDataOnSimObject(m_hSimConnect, AIP_SIM_PRIMARY_CONTROL_WRITE_DATA_ID, m_dwObjectID, 0, 1, sizeof(AIP_SIM_PRIMARY_CONTROL_WRITE_DATA), &kSimWriteData);



If you have serious intentions in accomplishing your project, than I'd highly recommend to invest some time in the following:
  1. Basic vector arithmetics: Addition, Multiplication, Cross and Scallar products, Normalization.
  2. Basic linear algebra: Matrix arithmetics, Vector transformation from one coordinate system into another.
  3. Spherical coordinates. Transformation into cartesian and back to spherical.

All of this could be found in internet, Wiki or Wolfram sites http://mathworld.wolfram.com/
 
Back
Top