RayCasting dosn't work correctly after rootNode rotation

Hello people,



i’m very new an just begun to program with jMonkeyEngine.



I use the SimpleApplication and use ray casting to determine the z-position. As long as the rootNode doesn’t rotated the ray casting works fine. After the rotation of the rootNode the ray casting is broken an.



Here the ray casting implementation:

[java]CollisionResults cResults = new CollisionResults();

Vector3f pos = threeDApp.getCamera().getWorldCoordinates(cursorPos, 0f).clone();;

Vector3f dir = threeDApp.getCamera().getWorldCoordinates(cursorPos, 1f).clone();;

dir.subtractLocal(pos).normalizeLocal();

Ray ray = new Ray(pos, dir);

plane.collideWith(ray, cResults);

CollisionResult result = cResults.getClosestCollision();

if (result != null) {

return result.getContactPoint();

}[/java]



Thanks in advance

Why are you rotating the root node?

@zarch said:
Why are you rotating the root node?


Exactly... try adding your geometries to another node, add that to the root node and rotate the child node. This will (at a minimum) avoid this issue... at a maximum point you in the right direction for making sure your scene graph is structured.

Thanks for your advice.

Tried your solution and put my geometries on a sub node of the rootNode. Now i rotate the subRootNode.rotate( 0f , 2*tpf , 0f); . Then i tried to rotate the ray by:

[java]

pos = subRootNode.getWorldRotation().mult(pos);

dir= subRootNode.getWorldRotation().mult(dir);

[/java]

But the ray casting returns still the wrong results. How can i calculate the actual values for the ray?



Thanks and best regards.

You need to give more information. What is the ray? Where is it generated from? Why is it getting rotated? What are you using it for? What results are you expecting and what are you getting?

Yeah, you multiply a rotation with itself, whats the point?

Ok, i solve the problem by transforming the global coordinates to local. The ray casting is working perfectly, it was an understanding error.



[java]

public void onMouseMotionEvent(MouseMotionEvent evt) {



Vector2f pos = new Vector2f(evt.getX(), evt.getY());

mousePicking(plane);

if (results.size() > 0) {

Vector3f movepos = insertion3DNode.mouseMotionGet3DPos(pos); //global coordinates

if (movepos != null) {

Vector3f v = subRootNode.worldToLocal(movepos, null); //local coordinates

setMyLocalTranslation(v); // local translation of an Node

}

}



}

[/java]



Thanks!