Picking fails when TerrainQuad is rotated

I am creating a small program that requires picking multiple points on the screen that collide with terrain. I created a TerrainQuad, translated and rotated it with the following code:

terrain = new TerrainQuad(“my terrain”, patchSize, 513, heightMap);
// We give the terrain its material, position & scale it, and attach it.
terrain.setLocalScale(2f, 2f, 1f);

and I am detecting picks on the terrain with the following code during the appropriate update with this code (taken directly from a JME3 example):

[java] // 1. Reset results list.
CollisionResults results = new CollisionResults();
// 2. Aim the ray from cam loc to cam direction.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
// 3. Collect intersections between Ray and Shootables in results list.
terrain.collideWith(ray, results);[/java]

The problem I am facing is that the pick never works - results.size == 0 always - when I rotate and translate the terrain. However, if I do not rotate and translate the terrain, leaving it near 0,0,0, it works as expected.

My camera is looking at (lookAt) and my player is positioned (setPhysicalLocation) at 128,-128,5, right above the center of the terrain.

Any suggestions? I’ve been banging my head on this one for a day now and I think it must be something embarrassingly obvious. :wink:

Thanks for your time,
Jay Jay

Terrain picking uses the Bresenham algorithm to find the triangle that intersects. This is significantly faster than using the regular triangle picking routine (BIHTree), especially since terrain is often 1 million polygons and is collided with a lot. The downside is it does not work when rotated as it is axis-aligned.
You could subclass the TerrainQuad class and override the collideWith() method and set it to use the BIHTree picking. You will have to drill down and find the TerrainPatch leaves that the ray collides with, and do the picking against them.

Alternately, I wonder if the terrain could somehow rotate the world ray into its local rotated space before picking?

I thought it might be something like that. Thanks very much!