Terrain collision between two coordinates

I’m trying to check if there isn’t a terrain collision between two Cartesian3 coordiantes. Is there a way to achieve this?

I tried with

var pos1 = coreEntity.position.getValue();
var pos2 = Cesium.Ellipsoid.WGS84.cartographicToCartesian(this._grid[i]);
var relativeDirection = new Cesium.Cartesian3(pos2.x - pos1.x, pos2.y - pos1.y, pos2.z - pos1.z);
var ray = new Cesium.Ray(pos1, relativeDirection);
var position = viewer.scene.globe.pick(ray, viewer.scene);
if(position) {

I know this is not the solution but this is the closest result I got.

That should work after you normalize the ray:

Cesium.Cartesian3.normalize(relativeDirection, relativeDirection);

Though you may get unexpected results based on the camera position and the terrain LOD being rendered.