# Locking entities to the terrain

**URL:** <https://community.cesium.com/t/locking-entities-to-the-terrain/3591>\
**Category:** CesiumJS\
**Created:** [January 18, 2016, 8:14pm UTC](https://community.cesium.com/t/locking-entities-to-the-terrain/3591 "2016-01-18T20:14:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenachelis](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@stevenachelis](https://community.cesium.com/u/stevenachelis)\
**Post date:** [January 18, 2016, 8:14pm UTC](https://community.cesium.com/t/locking-entities-to-the-terrain/3591/1 "2016-01-18T20:14:08Z")

</div>

I lock entities (i.e., billboards with labels and images) to the ground using sampleTerrain() (similar to the Everest sample). It almost works, except some of the entities float above the terrain whereas others "sink" into the terrain. When I zoom in/out, the entities sink/float more.

I recently read that setting HeightReference to Cesium.HeightReference.CLAMP\_TO\_GROUND" is now supported. However when I try to clamp the entities to the ground, I still have the same sink/float problems.

The following simplified sandcastle illustrates the problem (a "sunken" entity). Did I implement the clamp to ground functionality correctly? Should I still be calling sampleTerrain() (as I am) when using clamp to ground? Thanks!

var positions = [Cesium.Cartographic.fromDegrees( -111.572177, 40.582083 )];  
&nbsp;&nbsp;&nbsp;&nbsp;  
var viewer = new Cesium.Viewer(‘cesiumContainer’, {  
&nbsp;&nbsp;&nbsp;&nbsp;animation: false,  
&nbsp;&nbsp;fullscreenButton: false,  
&nbsp;&nbsp;geocoder: false,  
&nbsp;&nbsp;homeButton: false,  
&nbsp;&nbsp;sceneModePicker: false,  
&nbsp;&nbsp;selectionIndicator: true,  
&nbsp;&nbsp;timeline: false,  
&nbsp;&nbsp;scene3DOnly: true,  
&nbsp;&nbsp;baseLayerPicker : false,  
&nbsp;&nbsp;&nbsp;&nbsp;terrainProvider : new Cesium.CesiumTerrainProvider({ url : ‘[http://assets.agi.com/stk-terrain/world](http://assets.agi.com/stk-terrain/world)’ })  
});

function addEntities() {  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.scene.globe.depthTestAgainstTerrain = true;  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.entities.suspendEvents();

&nbsp;&nbsp;&nbsp;&nbsp;var theEntity = viewer.entities.add({  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;position : Cesium.Ellipsoid.WGS84.cartographicToCartesian( positions[0]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;billboard :{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image : '../images/facility.gif',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verticalOrigin : Cesium.VerticalOrigin.BOTTOM  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeightReference : Cesium.HeightReference.CLAMP\_TO\_GROUND  
&nbsp;&nbsp;&nbsp;&nbsp;});  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.entities.resumeEvents();

&nbsp;&nbsp;&nbsp;&nbsp;viewer.flyTo(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theEntity, {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset : new Cesium.HeadingPitchRange( Cesium.Math.toRadians( 165 ), Cesium.Math.toRadians(-20), 1500

)  
&nbsp;&nbsp;&nbsp;&nbsp;});  
}

Cesium.when( Cesium.sampleTerrain( viewer.terrainProvider, 9, positions ), addEntities );

---

<div class="post-metadata">

**Author:** ![stevenachelis](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@stevenachelis](https://community.cesium.com/u/stevenachelis)\
**Post date:** [January 23, 2016, 6:41pm UTC](https://community.cesium.com/t/locking-entities-to-the-terrain/3591/2 "2016-01-23T18:41:44Z")

</div>

Based on other forum posts, I discovered that CLAMP\_TO\_GROUND works with BillboardCollections, but not entities. The solution was to add the billboards to a collection. This works:

var position = Cesium.Cartesian3.fromDegrees(-111.572177, 40.582083);

var viewer = new Cesium.Viewer('cesiumContainer');

var cesiumTerrainProviderMeshes = new Cesium.CesiumTerrainProvider({  
&nbsp;&nbsp;&nbsp;&nbsp;url : '[//assets.agi.com/stk-terrain/world](https://assets.agi.com/stk-terrain/world)'  
});  
viewer.terrainProvider = cesiumTerrainProviderMeshes;  
viewer.scene.globe.depthTestAgainstTerrain = true;

var ellipsoid = viewer.scene.globe.ellipsoid;  
var billboardCollection = viewer.scene.primitives.add(new Cesium.BillboardCollection({  
&nbsp;&nbsp;&nbsp;&nbsp;scene : viewer.scene  
}));

billboardCollection.add({  
&nbsp;&nbsp;&nbsp;&nbsp;position : position,  
&nbsp;&nbsp;&nbsp;&nbsp;image : '../images/facility.gif',  
&nbsp;&nbsp;&nbsp;&nbsp;heightReference : Cesium.HeightReference.CLAMP\_TO\_GROUND  
});
