WebWorldWind Renderable Position Update
Asked Answered
R

2

14

I am trying to "move" renderables around the web worldwind globe on an interval. To illustrate the issue I am having, I made a small example.

This works (but is inefficient):

    var myVar = setInterval(myTimer, 5000);

    function myTimer() {


        shapesLayer.removeRenderable(shape);
        shape = new WorldWind.SurfaceCircle(new WorldWind.Location(shape.center.latitude+1, shape.center.longitude), 200e3, attributes);
        shapesLayer.addRenderable(shape);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }

This is what I'd like to do, but the shape doesn't move:

    var myVar = setInterval(myTimer, 5000);

    function myTimer() {

        shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }

Is there a flag I need to set on the renderable to make it refresh?

Here is the full SurfaceShapes.js file I've been playing with (based on this http://worldwindserver.net/webworldwind/examples/SurfaceShapes.html):

/*
 * Copyright (C) 2014 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration. All Rights Reserved.
 */
/**
 * Illustrates how to display SurfaceShapes.
 *
 * @version $Id: SurfaceShapes.js 3320 2015-07-15 20:53:05Z dcollins $
 */

requirejs(['../src/WorldWind',
        './LayerManager'],
    function (ww,
              LayerManager) {
        "use strict";

        // Tell World Wind to log only warnings.
        WorldWind.Logger.setLoggingLevel(WorldWind.Logger.LEVEL_WARNING);

        // Create the World Window.
        var wwd = new WorldWind.WorldWindow("canvasOne");

        /**
         * Added imagery layers.
         */
        var layers = [
            {layer: new WorldWind.BMNGLayer(), enabled: true},
            {layer: new WorldWind.BingAerialWithLabelsLayer(null), enabled: true},
            {layer: new WorldWind.CompassLayer(), enabled: true},
            {layer: new WorldWind.CoordinatesDisplayLayer(wwd), enabled: true},
            {layer: new WorldWind.ViewControlsLayer(wwd), enabled: true}
        ];

        for (var l = 0; l < layers.length; l++) {
            layers[l].layer.enabled = layers[l].enabled;
            wwd.addLayer(layers[l].layer);
        }

        // Create a layer to hold the surface shapes.
        var shapesLayer = new WorldWind.RenderableLayer("Surface Shapes");
        wwd.addLayer(shapesLayer);

        // Create and set attributes for it. The shapes below except the surface polyline use this same attributes
        // object. Real apps typically create new attributes objects for each shape unless they know the attributes
        // can be shared among shapes.
        var attributes = new WorldWind.ShapeAttributes(null);
        attributes.outlineColor = WorldWind.Color.BLUE;
        attributes.drawInterior = false;
        attributes.outlineWidth  = 4;
        attributes.outlineStippleFactor = 1;
        attributes.outlineStipplePattern  = 0xF0F0;    

        var highlightAttributes = new WorldWind.ShapeAttributes(attributes);
        highlightAttributes.interiorColor = new WorldWind.Color(1, 1, 1, 1);


        // Create a surface circle with a radius of 200 km.
        var shape = new WorldWind.SurfaceCircle(new WorldWind.Location(35, -120), 200e3, attributes);
        shape.highlightAttributes = highlightAttributes;
        shapesLayer.addRenderable(shape);

        // Create a layer manager for controlling layer visibility.
        var layerManger = new LayerManager(wwd);

        // Now set up to handle highlighting.
        var highlightController = new WorldWind.HighlightController(wwd);

        var myVar = setInterval(myTimer, 5000);

        function myTimer() {

            //Shape doesn't move

            shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

            //Shape "moves" but is inefficient

            //shapesLayer.removeRenderable(shape);
            //shape = new WorldWind.SurfaceCircle(new WorldWind.Location(shape.center.latitude+1, shape.center.longitude), 200e3, attributes);
            //shapesLayer.addRenderable(shape);

            console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

            wwd.redraw();
        }
    }
);
Regazzi answered 16/8, 2017 at 16:9 Comment(1)
Does you test Renderable.setPosition(Position position) ? I think it work for your case. If your shape is a Renderable you could use shape.setPosition(...).Diatessaron
R
1

If anyone else is looking at this, I found a better solution to force it to recompute the center position by setting isPrepared to false and _boundaries to undefined.

   var myVar = setInterval(myTimer, 5000);

    function myTimer() {

        shape.isPrepared = false;
        shape._boundaries = undefined;

        shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }
Regazzi answered 29/8, 2017 at 22:24 Comment(1)
Happy that you got one line answer, shape._boundaries = undefined, why it is needed?Nascent
N
3

  Document says renderables variable is readonly but I think it can be possible.
change code

shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

To

index = ShapesLayer.renderables.indexOf(shape);
ShapesLayer.renderables[index].center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

I think ShapesLayer.addRenderable create anther shape.

If you think it's not good way to do you can use this way

    RenderableLayer.prototype.changeRenderable= function (prevRenderable, nextRenderable) {

      index = ShapesLayer.renderables.indexOf(prevRenderable);
      ShapesLayer.renderables[index].center = nextRenderable;
    };

main code

 ShapesLayer.changeRenderable(shape, new WorldWind.Location(shape.center.latitude+1, shape.center.longitude));

Document : https://nasaworldwind.github.io/WebWorldWind/layer_RenderableLayer.js.html

Nascent answered 22/8, 2017 at 10:57 Comment(1)
I'm not seeing ShapesLayer in the API for WebWorldWind (nasaworldwind.github.io/WebWorldWind). I believe you may be looking at WorldWind Java, not WebWorldWind as I am in the question.Regazzi
R
1

If anyone else is looking at this, I found a better solution to force it to recompute the center position by setting isPrepared to false and _boundaries to undefined.

   var myVar = setInterval(myTimer, 5000);

    function myTimer() {

        shape.isPrepared = false;
        shape._boundaries = undefined;

        shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }
Regazzi answered 29/8, 2017 at 22:24 Comment(1)
Happy that you got one line answer, shape._boundaries = undefined, why it is needed?Nascent

© 2022 - 2024 — McMap. All rights reserved.