Refresh position of model according the true air speed

Good morning all,
I am facing an issue related to position calculation in Cesium.
My project use ADS-B data to display Aircraft models position and they are updated with a rate of one second or more if data is not received periodically. To display the new position I use the preupdate event handler in this way:
viewer.scene.preUpdate.addEventListener(function (scene, time) {

var Diff = new Date().getTime() - TimestampRefresh;
if (RenderStarted) {
    for (var plane of ListOfFlights.values()) {
        if (typeof plane.Entity !== "undefined" && ListOfFlights !== null) {
            if (plane.Longitude != 0) {
                var speed = (plane.Speed * 1.852 ) / 3.6;
                var altitude = plane.Altitude / CONSTFEET;
              
                
                var hpRoll = new Cesium.HeadingPitchRoll();
                hpRoll.heading = Cesium.Math.toRadians(plane.Heading);
                hpRoll.pitch = Cesium.Math.toRadians(plane.Pitch / 100);
                var position = Cesium.Cartesian3.fromDegrees(plane.Longitude, plane.Latitude, altitude);
                
                var CoeFF = 1000 / Diff;
                CoeFF  = 1/CoeFF;
                 //if airplane is select by user I display more accurate position according speed
                if (currentObject3d == plane.ID) {
                    var spdVector = new Cesium.Cartesian3();
                    spdVector = Cesium.Cartesian3.multiplyByScalar(Cesium.Cartesian3.UNIT_X, (speed/10 ) +CoeFF , spdVector);
                    position = Cesium.Matrix4.multiplyByPoint(plane.Entity.modelMatrix, spdVector, position);
                    
                    Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform, plane.Entity.modelMatrix);
                    $('#Refreshspeed').text(Math.round(CoeFF).toString());
                    $('#geoSpeed').text(Math.round(plane.Speed).toString());
                    $('#geoAltitude').text(Math.round(plane.Altitude).toString());
                    $('#geoHeading').text(Math.round(plane.Heading).toString());
                    $('#geoLatitude').text(plane.Latitude.toPrecision(5));
                    $('#geoLongitude').text(plane.Longitude.toPrecision(5));
                    viewer.scene.selectedEntity = plane.Entity;
                    TimestampRefresh = new Date().getTime();

                }
                else {
                    Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform, plane.Entity.modelMatrix);
                }
                if (plane.Label != null) {
                    plane.Label.position = position;
                }
                else {

                }
                
                }
            }
            else {
                // alert(plane.Callsign);
            }
        }
    }
}

//}
});
when an airplane is select by clicking on it (with pick event) , the code should correctly calculate the position and display the model moving as in reality. The result is not correct and in base at frame per second rate the model move too fast or to slow. I tried to insert a coeffecient correction due to a difference in rate refresh.without success. Do you have any suggestion?.
Thank you

Hey, welcome to the Cesium community!

Have you tried using CesiumJS’s built-in time-dynamic features? You should be able to create a SampledPositionProperty, add position samples with a timestamp, and then let CesiumJS interpolate or extrapolate as well as automatically configure the orientation based on the velocity. There’s a Sandcastle code example here that you might find useful:

Here this creates all the position and timestamps up front, but in your app you can continually add more samples as you receive them from your server.

Hi Omar,
I work with “realtime” data and for this reason it’s not possible to use SampledPosition to obtain orientation and speed based on previous timestamp/position reports but I need to calculate as a predicted position what will be the supposed future next ADS-B position message. Immagine an airplane during climbing/descending or heading changes, its trajectory does not respect a linear line but appears like a curve. I tried of course to use an algorithm of prediction, it works fine with linear paths but during the specific cases described before, it generates small errors in the expected positions and Cesium doesn’t process correctly the path and the result on the screen is a zig-zag behaviour or a turn back of the airplane. Also, and it was the initial point of my question, to process correctly the 4d prediction, I need to know how Cesium is taking in consideration the speed (m/s) to calculate its own prediction in the following lines of code used in the preupdate event manager:
speedVector = Cesium.Cartesian3.multiplyByScalar(Cesium.Cartesian3.UNIT_X, speed/10 , speedVector);
position = Cesium.Matrix4.multiplyByPoint(planePrimitive.modelMatrix, speedVector, position);
Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform, planePrimitive.modelMatrix);
because the factor time is not present and it’s not predictable the next iteration of the code because it depends on refresh rate and frame rate values. I tried also to put the code with a timer function and also here for many reasons (cpu and Gpu load and JS limits) I have a fluctuation of the calculated position.
Any other suggestion?

I believe the sampled positions properties are designed to work with realtime data like you describe. Each time you receive an update you insert that new position, and you can play the simulation a few seconds in the past so it can interpolate between the last few received timestamps.

Otherwise, it sounds like what you’re doing so far in your application is moving the plane model to its new location each time you receive an update, which should work if computed correctly. You might need to compare what your code is doing with what CesiumJS’s built-in functions that compute velocity do.

If you can show a Sandcastle that I can run to reproduce the issues you describe I may be able to take a look.