Custom Terrain Intersection Question

In my game I use a custom terrain (generated from heightmap) intersection method that performs very well. Intersecting the terrain geometry itself is extremely slow so I built this as an alternative.

Basically what I’m doing is determining which terrain “face index” an object is on, grabbing the correlating vertices from the terrain geometry and constructing a THREE.Plane object that is set via the referenced co-planar points and intersecting with that.

In most cases this method works great, but on more extreme slopes it kind of breaks down as demonstrated below:

https://jsfiddle.net/titansoftime/bh9d5g6t/

These are a couple in-game screens showing this effect. The triangle patterns on the slopes indicates to me that I may be making a mistake. Only half the “face” is working.

test

test2

Am I doing something wrong?

I’ve been actually looking for a slope terrain sliding function, based on X-Rotation Angle. Would be great if a very simple example could be put out. Thanks alot & have a great easter! :slight_smile:

I refactored your fiddle a bit for a simple (possible) solution: https://jsfiddle.net/683wL4qe/

The idea is to compute the distance from the mesh position to the plane and then subtract this value from the y-component of ball.position. But before doing this, you multiply the distance with a value lower than 1 (e.g. 0.2) in order to have a smooth transition.

Note: At the beginning, the ball moves from (0, 0, 0) onto the plane. This can be avoided if you directly assign a valid start position when the mesh is added to the scene.

I’m not sure I understand your raycasting code in intersectTerrain() so I just compute the distance to the plane via Plane.distanceToPoint() which should be a good approximation. It’s also faster than a raycast. The approach is not a perfect solution but it might be sufficient for certain use cases.

1 Like

Very nice! Perfectly smooth and much more performant!

There are certainly use cases that I will use this for.

Unfortunately for my grass positioning, I really needed that accuracy (which is too bad because load time was much better with your method).

The triangle pattern clue checked out and ultimately revealed the incredibly obvious mistake I was making. I was only checking half the face and completely ignoring the fact the the other triangle within the face could (and likely does) have a much different orientation. Now I make sure that the indices I use to set the intersect plane are from the triangle that the object is actually within.

https://jsfiddle.net/titansoftime/qw13oe8r/

I am going to take another stab at implementing your solution but this time with the triangle check as well.

Thank you very much for helping me out =]

1 Like

@titansoftime Can you add a small example of a camera that is controlled by the keys can slide down a slope if it is too steep?

If you build a fiddle with an attempt at it, I will be more than happy to check it out.

ok, cool! i will do that :slight_smile: