Get model current position and angles


I want to extract the position and orientation of a model which is placed somewhere in the world,

So far I am able to get lat, lon and height like this:

var position = Cesium.Matrix4.getTranslation(model.modelMatrix, new Cesium.Cartesian3());

var cartographicPosition = viewer.scene.globe.ellipsoid.cartesianToCartographic(position);

var longitude = Cesium.Math.toDegrees(cartographicPosition.longitude);

var latitude = Cesium.Math.toDegrees(cartographicPosition.latitude);

var height = Cesium.Math.toDegrees(cartographicPosition.height);

now, I want to get the heading, pitch and roll angles,

Any suggestions ?

This is a bit more involved than one would think. You can you Cesium.Matrix4.getRotation to get the Matrix3 rotation from the modelMatrix and then use Cesium.Quaternion.fromRotationMatrix to get a Quaternion from that. You can then use some code from this unmerged Pull Request to get heading/pitch/roll from a quaternion:

However, Those HPR angles will be in earth-fixed, and I assume you want them in a more common frame like east/north/up. I think you need to use the position and Transforms.eastNorthUpToFixedFrame and then Matrix4.transpose on that. You then multiply that matrix to the modelMatrix before doing any of the operations above.

Of course this is just all off the top of my head, I’ll try and see if I can throw some sample code together next week to do it, but if you figure it out in the meantime, let me know.

Hello Matthew,
Thank you for the help,

I have tried both suggestions and didn’t get the right result.

For the test, I have tried to set a model in a known position and angles like that:

var origin = Cesium.Cartesian3.fromDegrees(longitude_deg, latitude_deg, altitude_m);

var heading_rad = Cesium.Math.toRadians(heading_deg);

var pitch_rad = Cesium.Math.toRadians(pitch_deg);

var roll_rad = Cesium.Math.toRadians(roll_deg);

var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(origin, heading_rad, pitch_rad, roll_rad);

model.modelMatrix = modelMatrix;

and then I have tried getting the angles back with your help and did not get the same result…