javaworldwind

CompassLayer not showing up in WorldWind


I have a WorldWindow with various RenderableLayers on it. I'd like to add a CompassLayer at run-time.

try {

    String compassPath = "images" + File.separator + "CompassRoseWhite.png";
    String compassImg  = new ClassPathResource(compassPath).getURL().toString();
    compass            = new CompassLayer(compassImg);
    worldWindow.getModel().getLayers().add(compass);

} catch (IOException e) { e.printStackTrace(); }

Unfortunately, I do not see the compass anywhere on-screen. I have tried:

I got back:

Cur layers = Stars, Atmosphere, Bing Imagery VASCustom, Scale bar, Compass, View Controls, Renderable, Renderable, Renderable, Renderable, Compass,

How can I effectively debug my invisible compass problem? Thank you!

Update

I've simplified my code to use the existing compassLayer, and I've determined the problem is my use of setLocationCenter, i.e.,

compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");

// this works ...           
// compass.setPosition(AVKey.SOUTHEAST);
// compass.setLocationOffset(new Vec4(0, 20));

// this does not work ...  
compass.setLocationCenter(worldWindow.getView().getCenterPoint());

// this part works fine
String compassPath = "images" + File.separator + "CompassRoseWhite.png";
String compassImg  = new ClassPathResource(compassPath).getURL().toString();
compass.setIconFilePath(compassImg);
compass.setEnabled(true);

So what I need to determine is what exactly is wrong with the setLocationCenter logic.


Solution

  • There were two primary problems with my code.

    1. I already had a compassLayer and should have used it instead of making a new one, ie,

      compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");

    2. When you use computePointFromPosition() you get back absolute Cartesian coordinates. Then you need to convert these to pixel coordinates using View.project(). Finally you need to offset by the current View. Ie,

    Vec4 vecOwnship = worldWindow.getModel()
                                 .getGlobe()
                                 .computePointFromPosition(ownshipPosition);
    Vec4 vecScreen  = worldWindow.getView().project(vecOwnship);
    Rectangle viewPort = worldWindow.getView().getViewport();
    compass.setLocationCenter(new Vec4(viewPort.x + vecScreen.x, viewPort.y + vecScreen.y, 0));
    

    Fixing these issues solved the problem and now the compass shows up.