# 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

-Sam

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

Hi,

any more suggestion - how do I recognize that the collisionDetection has detect a collision ?

Do I have to set the flag -it is a default or not ?

Regards

Rüdiger

@Ruediger_Brand

I apologize for the delayed response - it has been a very busy week at Cesium. What objects would you like collision detection to be active for? Remember that you can access the coordinates of the Camera object. This information can be used to limit the camera to altitudes above a given number of feet.

-Sam

-Sam

@sam.rothstein

Hi Sam,

I don’t understand the function of enableCollisionDetection - what are the effects. It default is true.

I can set a camera position, which is under the terrain by editing the position itself. Why do I need this flag? What is the result, in which way does it work - do I get an information if camera is under the trrain ?

Rüdiger

@Ruediger_Brand

These resources should help you limit the camera tilt to prevent the camera from going underneath the terrain. You might find this demo particularly helpful.

-Sam

@sam.rothstein

Hello sam

I’m sorry for asking so insistently about how “enableCollisionDetection” works, but I don’t understand it.

I can manually put the camera under the surface of the earth. Is there a way here that Cesium forbids that?

I have the following use case → my customers sets/moves the camera by clicking on a button in my web application. The customer doesn’t use the mouse. So I would like to prevent him from going under the earth.

I want to gray out the button to prevent movement - I know that I can calculate the height above ground of the camera position, but this is very “expensive” (in case of performance issues). So there are maybe other ways to forbid that ?

I have made a sandcastle from the default camera tutorial using the keys you can walk through a “mountain”.

Rüdiger