Model movement, dynamic positions from server

Hi,

I am using SampledPositionPorperty to get a 3d model movement over time. If the positions all are loaded from a file, it works well.
The problem comes when I want to load the positions dynamically from a server (i.e. kafka deployment) that sends messages (positions) let’s say every 5 seconds. What happens is that the clock starts before getting any position and the model doesn’t show up. How could I achieve the behavior of the model showing up and the clock waiting till next position is added to SampledPositionProperty? Am I mistaken in my approach?

This is an example of my code so far:

function runSimulationWebSocketKafka(){

    const timeStepInSeconds = 1;
    const totalSeconds = timeStepInSeconds * (100 - 1);
    const start = Cesium.JulianDate.fromIso8601("2020-03-09T12:10:00Z");
    const stop = Cesium.JulianDate.addSeconds(start, totalSeconds, new Cesium.JulianDate());
    // viewer clock at desired time
    viewer.clock.startTime = start.clone();
    viewer.clock.stopTime = stop.clone();
    viewer.clock.currentTime = start.clone();
    viewer.timeline.zoomTo(start, stop);
    // Speed up the playback speed 1x.
    viewer.clock.multiplier = 1;
    // Loop at the end
    //viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
    // Clock pauses till next position is given
    viewer.clock.clockRange = Cesium.ClockRange.CLAMPED;
    // Start playing the scene.
    viewer.clock.shouldAnimate = true;
    
    // The SampledPositionedProperty stores the position and timestamp for each sample along the radar sample series.
    const positionProperty = new Cesium.SampledPositionProperty();
    
    
    
    // add positions from kafka
    ws.onmessage = function (event) {
        kafkaData = JSON.parse(event.data);
        console.log('kafka_data: ', kafkaData.latitude)
                               
        const time = Cesium.JulianDate.addSeconds(start, count * timeStepInSeconds, new Cesium.JulianDate());
        const position = Cesium.Cartesian3.fromDegrees(kafkaData.longitude, kafkaData.latitude, 0);
        positionProperty.addSample(time, position);           
        

    }

Thank you for your help.

Iñaki

Hi @icejudo

I’ve tried to simulate your data streaming setup using a setInterval call that will generate a new position for a model and add it to the SampledPositionProperty. Does this Sandcastle demonstrate the behavior you are looking for? Please note that I have explicitly set the forwardExtrapolationType to enable extrapolation. You may want to set this to a different value for your application’s requirements.

1 Like

Hi @sanjeetsuhag ,

Thank you for your help. It is a good example the one of the sandcastle. The problem is in this example setInterval and cesium clock are synchronized at the same pace (every second is received a new position). In my case, we do not know when a new position will be received (could be 1 sec, 3 secs…). So what happens is that the clock goes at real time and the model makes an infinite extrapolation, and new positions are not loaded.
How can I make the clock (or the 3d model) waits till new position is received?

Thank you.

How can I make the clock (or the 3d model) waits till new position is received?

I believe that if you set the forwardExtrapolation to ExtrapolationType.HOLD, it will stop the model at the last known “valid” position on the timeline. You could also just pause the clock by setting shouldAnimate to false.

Awesome! I think I can achieve now the wanted behavior. Thank you for your help.

1 Like