[Solved] TerrainQuad in the x-y plane can not use pick

Hello. I use the TerrainQuad, but I rotate the terrain so that it’s in the x-y plane (because for me it’s easier to have z axis for zoom). I changed the TerrainTestCollision.java following way:

terrain.rotate(DEG_TO_RAD * 90, 0, 0);
// cam.setLocation(new Vector3f(43f, 121f, 10f));
// cam.setRotation(new Quaternion(0.15824f, -0.79309f, 0.23223f, 0.5404f));
var position = new Vector3f(0, 0, 70);
cam.setLocation(position);

Now the camera is at 0,0,70 and looking down on the terrain. The terrain.collideWith(ray, results) is returning 0 results. Before it was working fine. For testing: TerrainTestCollision2.java · GitHub

In the debugger I can see that it finds the TerrainPatch. But then it fails to find the Triangle.

I see there is BresenhamYUpGridTracer. Do I need to create a BresenhamZUpGridTracer with basically stepXDirection and stepYDirection?

Those are strange origin and direction calculations. The origin should just be the camera location and the direction is the camera rotation multiplied by the direction you want.

E.g. vector3f dir = cam.getRotation.mult(vector3f.unit_z); would be the forward direction (as opposed to the traditional left or up).

My recollection is that terrain picking doesn’t work properly for rotated terrain. It neglects to properly transform the ray into local space or something.

You will probably have an easier time leaving terrain in the x/z plane and just doing your zoom based on y instead of z.

Your code may be strange if this isn’t just a matter of changing z for y. If it’s just a mental block then you may need to ‘get over it’. And I sympathize because I also had to ‘get over it’ after a decade in the vis-sim industry thinking of z as up and x/y as ‘map’. (100+ forgetting swap y for z bugs later and I just made an effort to adapt.)

Hi. Thank you for your replies. It’s not just for zoom but for some other calculations that transform spherical coordinates to x-y-z coordinates. I rewrote the calculations to have y up and now everything works.

Sorry, I don’t understand your comment. This is the example TerrainTestCollision.

Ignore my statement. Paul is right. The terrain meshes have a sort of custom collision check so probably won’t work if they’re rotated. You should follow Paul’s advice.