how can I prevent that camera goes under terrain or calculate if cameraposition is under or above terrain ?
See the following two screenshots:
starting pos
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;
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 ?
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.
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.
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 ?
Please check out the resources linked in the following thread.
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.
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”.