Camera goes under terrain!

Hi,

how can I prevent that camera goes under terrain or calculate if cameraposition is under or above terrain ?

See the following two screenshots:

  1. starting pos
  2. rotate around the “cross” in the middle

My use case is thefollowing:
I look at a point in the middle of the screen. Using Buttons I place the camera around the middle on a sphere and look at themiddle point. In the above situation the camera on the sphere moves under the terrain.

We use a routine to look from camera to point

viewer.scene.camera.setView({
destination: from,
orientation: {
heading: Cesium.Math.TWO_PI, // east, default value is 0.0 (north)
pitch: -1 * Cesium.Math.PI_OVER_TWO, // default value (looking down)
roll: 0.0
}
});

var camera = viewer.scene.camera;
var cameraPosition = camera.positionWC.clone();
var direction = Cesium.Cartesian3.subtract(
to,
from,
new Cesium.Cartesian3()
);

direction = Cesium.Cartesian3.normalize(direction, direction);
camera.direction = direction;

// get an “approximate” up vector, which in this case we want to be something like the geodetic surface normal.
var approxUp = Cesium.Cartesian3.normalize(
cameraPosition,
new Cesium.Cartesian3()
);

// cross viewdir with approxUp to get a right normal
var right = Cesium.Cartesian3.cross(
direction,
approxUp,
new Cesium.Cartesian3()
);
right = Cesium.Cartesian3.normalize(right, right);
camera.right = right;

// cross right with view dir to get an orthonormal up
var up = Cesium.Cartesian3.cross(
right,
direction,
new Cesium.Cartesian3()
);
up = Cesium.Cartesian3.normalize(up, up);
camera.up = up;

viewer.scene.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);

Regards

viewer.scene.screenSpaceCameraController.enableCollisionDetection = true;

@ZhefengJin

Thank you for posting the solution! @Ruediger_Brand give this a try and let us know how it goes :rocket: :grinning:

-Sam

@sam.rothstein @ZhefengJin

Hi,

I posted this example because I saw the discussions in former posts. I will try the flag, but also have programmed a solution, in which I compare the camera position height to the height of the terrain and set the camera position height in the caes, where it is under the terrain.

I wondered that it is so easy to position the camera under the terrain - is this due to the new features of displaying sitution under the terrain or in the sea ?

Rüdiger

@sam.rothstein

Hi,

the parameter doesn’t work - I think it is also a default.

Regards

Rüdiger

1 Like

@Ruediger_Brand

How did you update enableCollisionDetection? Sharing a sandcastle example of what you have so far might help me get a better understanding of your overall implementation.

-Sam

@sam.rothstein

I set the parameter after the initialization. What Do you mean with update. On the other side I think it is set to true as default.

Rüdiger

1 Like