Node Rotation and Collision Problem

I have been stuck on this problem for around a week, so I decided it was time to ask for some help.

Here is what I am trying to do:
-I have a model loaded into the scene (currently, it is a cube)
-When you click and drag with the mouse, the cube rotates in the dragged direction
-I am currently casting a ray from the mouse and displaying an arrow from the collision point and rotated to the contact normal
-Everything works fine if I attach the arrow to the root node, but if I attach it to the node that has the model as a child, it no longer works

I have read all of the tutorials on how nodes work, but I must’ve missed something.

In my app I have a modelNode attached to the rootNode and the actual geometry attached to the nodeModel.

Here is the models rotation code:
[java] if (name.equals(“RotateY”) && isLMBpressed) {
// Quaternion qt = geoModel.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(-rotateAmount, yAxis));
// geoModel.setLocalRotation(qt);

            Quaternion qt = nodeModel.getWorldRotation().multLocal(new Quaternion().fromAngleAxis(-rotateAmount, yAxis));

// nodeModel.setLocalRotation(qt);

        }
        if (name.equals("RotateYNeg") && isLMBpressed) {

// Quaternion qt = geoModel.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(rotateAmount, yAxis));
// geoModel.setLocalRotation(qt);

            Quaternion qt = nodeModel.getWorldRotation().multLocal(new Quaternion().fromAngleAxis(rotateAmount, yAxis));
            nodeModel.setLocalRotation(qt);
        }
        if (name.equals("RotateX") && isLMBpressed) {

// Quaternion qt = geoModel.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(rotateAmount, xAxis));
// geoModel.setLocalRotation(qt);

            Quaternion qt = nodeModel.getWorldRotation().multLocal(new Quaternion().fromAngleAxis(rotateAmount, xAxis));
            nodeModel.setLocalRotation(qt);
        }
        if (name.equals("RotateXNeg") && isLMBpressed) {

// Quaternion qt = geoModel.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(-rotateAmount, xAxis));
// geoModel.setLocalRotation(qt);

            Quaternion qt = nodeModel.getWorldRotation().multLocal(new Quaternion().fromAngleAxis(-rotateAmount, xAxis));
            nodeModel.setLocalRotation(qt);
        }[/java]

And here is how I set up the arrow:
[java] Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f).clone();
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

    direction.subtractLocal(origin).normalizeLocal();

    Ray ray = new Ray(origin, direction);
    CollisionResults results = new CollisionResults();
    nodeModel.collideWith(ray, results);
    if (results.size() > 0) {
        CollisionResult closest = results.getClosestCollision();
        
        mark.setLocalTranslation(closest.getContactPoint());

        Quaternion q = new Quaternion();
        q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y);
        mark.setLocalRotation(q);

        nodeModel.attachChild(mark);[/java]

Any help or suggestions would be much appreciated! Thank you.

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#worldToLocal(com.jme3.math.Vector3f,%20com.jme3.math.Vector3f)

1 Like

I did look into that previously, but I wasn’t sure exactly what to use it on.

I looked at it again, though, and realized that the contactPoint and contactNormal needed to be converted to the node’s local space.

I can’t thank you enough for the nudge in the right direction. I appreciate it. =D

1 Like