Hi!

My guess: Could the TerrainPatch.collideWith() have added the hit mesh triangle index information to the results? so we can collect its normal!

In the below code, the col.getGeometry() is a TerrainPatch.

[patch] CollisionResult col = results.getClosestCollision();

Triangle tri = new Triangle();

col.getGeometry().getMesh().getTriangle(col.getTriangleIndex(),tri);

tri.calculateNormal();

Vector3f normal = tri.getNormal();[/patch]

The triangle index col.getTriangleIndex() always returns 0, no matter where the ray is cast at.

The returned triangle has all its values zeroed, so the normal is too, so I cant use it.

I tried this trick but it didnt work, it now finds a valid (non zeroed) triangle, but for some reason the returned triangle index returns a zeroed triangle (debugging I saw it was valid when found at BIHNode.intersectWhere(), but the stored triangle index doesnt return the same triangle):

[patch] Geometry geom = new Geometry();

geom.setMesh(col.getGeometry().getMesh());

geom.setLocalTransform(col.getGeometry().getWorldTransform());

results = new CollisionResults();

geom.collideWith(ray, results);

col = results.getClosestCollision();[/patch]

I need this code to position a moving light (lantern) just above the ground.

Btw, if there is another way to this, I would like to know

thx!

You can get the collision normal from the CollisionResult.

collisionResult.getContactNormal()

You can also get the contact point:

collisionResult.getContactPoint()

This is already calculated for you.

I didnt check the returned value for it… so, I didnt see it was relative and not world!

Works perfect, Thx!

Sploreg said:

You can get the collision normal from the CollisionResult.

collisionResult.getContactNormal()

You can also get the contact point:

collisionResult.getContactPoint()

This is already calculated for you.

By the way, I noticed the result of the collision might not be in world space, if for example you have a rotation applied to the terrain? Or is rotation not allowed for terrain?

Rotation isn’t supported by it.

I didn’t know that the result was supposed to be in world space. It makes sense though. I will make the change.

I just want to point out that 3 years later, it still does that. If I ray cast down to a TerrainQuad, it collides with a TerrainPatch and the CollisionResult.getTriangleIndex() *always* returns 0 (which is pointless). Thank you @teique for your trick, it works, but I think this should be fixed in the core to avoid unnecessary computations like this to obtain the collision triangle, or can anybody from the development team explain to me if this is expected behavior why it’s doing that? Thanks.