1

I am struggling with camera functionality that (I think) would provide a way to force my polygon to stick to the top of my house on zoom-out, zoom-in, and rotation (or camera move).

This question follows an earlier question that was resolved. Now I need a little help resolving my next issue.

The sample code I am trying to follow is located in the gold standard that appears to be baked into the existing camera controller here.

pickGlobe is executed with the parameters of the viewer, the correct mousePosition in world coordinates and a result parameter, which I don't care about right now. scene.pickPosition takes the c2position (Cartesian2) and should return the scratchDepthIntersection (Cartesian3). Instead, the returned value is undefined.

Here is my code:

function clickAction(click) {
    var cartesian = scene.camera.pickEllipsoid(click.position, ellipsoid);
    if (cartesian) {
        var setCartographic = ellipsoid.cartesianToCartographic(cartesian);
        collection.latlonalt.push(
            Cesium.Math.toDegrees(setCartographic.latitude).toFixed(15),
            Cesium.Math.toDegrees(setCartographic.longitude).toFixed(15),
            Cesium.Math.toDegrees(setCartographic.height).toFixed(15)
        );
        lla.push(Cesium.Math.toDegrees(setCartographic.longitude), Cesium.Math.toDegrees(setCartographic.latitude));
        if (lla.length >= 4) {
            console.log((lla.length / 2) + ' Points Added');
        }
        enableDoubleClick();
        enableDraw();

        testMe(click.position);  <--------------------- straight from the mouse click
    }
}

var pickedPosition;
var scratchZoomPickRay = new Cesium.Ray();
var scratchPickCartesian = new Cesium.Cartesian3();
function testMe(c2MousePosition) {  <--------------------- straight from the mouse click
    if (Cesium.defined(scene.globe)) { 
        if(scene.mode !== Cesium.SceneMode.SCENE2D) {
            pickedPosition = pickGlobe(viewer, c2MousePosition, scratchPickCartesian);
        } else {
            pickedPosition = camera.getPickRay(c2MousePosition, scratchZoomPickRay).origin;
        }
    }
}

var pickGlobeScratchRay = new Cesium.Ray();
var scratchRayIntersection = new Cesium.Cartesian3();    
var c2position = new Cesium.Cartesian2();
function pickGlobe(viewer, c2MousePosition, result) {   <--------------------- straight from the mouse click
    c2position = c2MousePosition;   <--------------------- setting to Cartesian2

    var scratchDepthIntersection = new Cesium.Cartesian3();
    if (scene.pickPositionSupported) {
        scratchDepthIntersection = scene.pickPosition(c2MousePosition);  <--------------------- neither works!
    }

}

Here are my variables:

enter image description here

Here is the result:

enter image description here

Here are my questions to get this code working:

1. Why is scratchDepthIntersection not getting set? c2position is a Cartesian2 and c2MousePosition is straight from the mouse.click.position and scratchDepthIntersection is a new Cartesian3.

Community
  • 1
  • 1
Patricia
  • 5,019
  • 14
  • 72
  • 152

1 Answers1

1

The correct value for mousePosition is a Cartesian2 containing window coordinates, not a Cartesian3. Such mouse coordinates usually come from a callback from Cesium.ScreenSpaceEventHandler, but can also be constructed from native JavaScript mouse/touch events.

If you inspect the contents of mousePosition, you should find x and y values in window pixel coordinates.

I see you edited the question to include the contents of mousePosition, and it looks like the mouse coordinates have already been converted into ellipsoid Cartesian3 coordinates, which will prevent this code from working. You want original mouse coordinates going directly into scene.pickPosition for this to work.

emackey
  • 11,818
  • 2
  • 38
  • 58
  • Well, CRAP! It worked once. Can I just take the click.position? Or do I have to declare it as a Cartesian2()? They look the same. I've tried it both ways. Rebooted my machine.....I am ready to scream. I'll post code again. – Patricia Mar 25 '16 at 20:39
  • Sounds like you're close. Yes, if Cesium gives you a `click.position`, that is already the correct data structure to pass in here. – emackey Mar 25 '16 at 20:46
  • Yay! I got it working then it stopped working then I got it working again and pushed it to git! Good thing, because it stopped working again. So I deleted the application and pulled it back down from git and it started working again. Temperamental project! – Patricia Mar 25 '16 at 22:38
  • Hey emackey! I have a new question here: http://stackoverflow.com/q/36271816/1735836 – Patricia Mar 28 '16 at 21:52