Hi,
I am trying to move geometry by picking it and dragging by mouse.
not sure whats wrong with this code… I am able to pick the geometry I want to move, but when I drag it, its not working as expected. see the video, code and output log below. (Ignore cross hair in video, it has nothing to do with function).
!! Cubes in Red, Green Yellow and Blue are geometrys I want to move. !!
inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
if (name.equals("RIGHT_CLICK") && isPressed) {
Material m = geom2.getMaterial();
m.getAdditionalRenderState().setWireframe(!m.getAdditionalRenderState().isWireframe());
}
if (name.equals("LEFT_CLICK")) {
Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());
Ray ray = new Ray(cam.getWorldCoordinates(mouseCoords, 0),
cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(
cam.getWorldCoordinates(mouseCoords, 0)).normalizeLocal());
CollisionResults results = new CollisionResults();
int resCount = rootNode.collideWith(ray, results);
if (isPressed) {
if (resCount > 0) {
Geometry geo = results.getClosestCollision().getGeometry();
if (geo != null && geo.getName().startsWith("Point")) {
System.out.println(geo.getName() + " Selected.");
selectedGeometry = geo;
movePoint = true;
}
}
} else {
movePoint = false;
}
}
if (name.equals("RECORD") && isPressed) {
recorderAppState.setQuality(.5f);
recorderAppState.setEnabled(true);
stateManager.attach(recorderAppState); //start recording
}
}, "RIGHT_CLICK", "LEFT_CLICK", "RECORD");
inputManager.addListener(analogListener, "TRIGGER_ROTATEDRAG", "TRIGGER_UP", "TRIGGER_DOWN", "TRIGGER_LEFT", "TRIGGER_RIGHT");
}
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float intensity, float tpf) {
movePoint(tpf);
// if (name.equals("TRIGGER_LEFT")) {
// movePoint(tpf);
// } else if (name.equals("TRIGGER_RIGHT")) {
// movePoint(tpf);
// } else if (name.equals("TRIGGER_UP")) {
// movePoint(tpf);
// } else if (name.equals("TRIGGER_DOWN")) {
// movePoint(tpf);
// }
}
private void movePoint(float tpf) {
if (movePoint && selectedGeometry != null) {
Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());
Vector3f p1 = selectedGeometry.getLocalTranslation().clone();
Vector3f p2 = cam.getWorldCoordinates(mouseCoords, 0.0f);
p2.setZ(0.0f);
Vector3f p3 = selectedGeometry.worldToLocal(p2, null);
p3.setZ(p1.z);
System.out.println("Finl Trans: " + p3 + ", Mous Loc: " + p2 + ", Old Gom Loc: " + p1);
selectedGeometry.setLocalTranslation(p3);
}
}
};
Here is output from “movePoint(…)” method.
Point Red Selected.
Finl Trans: (1.7533128, -1.7625176, 2.0), Mous Loc: (-0.24668714, 0.23748247, 0.0), Old Gom Loc: (-2.0, 2.0, 2.0)
Finl Trans: (-2.0, 1.9990796, 2.0), Mous Loc: (-0.24668714, 0.23656197, 0.0), Old Gom Loc: (1.7533128, -1.7625176, 2.0)
Finl Trans: (1.7533128, -1.7634381, 2.0), Mous Loc: (-0.24668714, 0.23564151, 0.0), Old Gom Loc: (-2.0, 1.9990796, 2.0)
Finl Trans: (-2.0, 1.9981592, 2.0), Mous Loc: (-0.24668714, 0.23472105, 0.0), Old Gom Loc: (1.7533128, -1.7634381, 2.0)
Finl Trans: (1.7533128, -1.765279, 2.0), Mous Loc: (-0.24668714, 0.23288009, 0.0), Old Gom Loc: (-2.0, 1.9981592, 2.0)
Finl Trans: (-2.0, 1.9972386, 2.0), Mous Loc: (-0.24668714, 0.23195958, 0.0), Old Gom Loc: (1.7533128, -1.765279, 2.0)
Finl Trans: (1.7533128, -1.7661995, 2.0), Mous Loc: (-0.24668714, 0.23103912, 0.0), Old Gom Loc: (-2.0, 1.9972386, 2.0)
Finl Trans: (-1.9990795, 1.9963181, 2.0), Mous Loc: (-0.2457667, 0.23011866, 0.0), Old Gom Loc: (1.7533128, -1.7661995, 2.0)
Finl Trans: (1.7533128, -1.7661995, 2.0), Mous Loc: (-0.2457667, 0.23011866, 0.0), Old Gom Loc: (-1.9990795, 1.9963181, 2.0)
Finl Trans: (-1.9990795, 1.9972386, 2.0), Mous Loc: (-0.2457667, 0.23103912, 0.0), Old Gom Loc: (1.7533128, -1.7661995, 2.0)
Finl Trans: (1.7533128, -1.765279, 2.0), Mous Loc: (-0.2457667, 0.23195958, 0.0), Old Gom Loc: (-1.9990795, 1.9972386, 2.0)
Finl Trans: (-1.9990795, 1.9990796, 2.0), Mous Loc: (-0.2457667, 0.23380055, 0.0), Old Gom Loc: (1.7533128, -1.765279, 2.0)
Finl Trans: (1.7533128, -1.7634381, 2.0), Mous Loc: (-0.2457667, 0.23564151, 0.0), Old Gom Loc: (-1.9990795, 1.9990796, 2.0)
Finl Trans: (-1.9990795, 2.0, 2.0), Mous Loc: (-0.2457667, 0.23656197, 0.0), Old Gom Loc: (1.7533128, -1.7634381, 2.0)
Finl Trans: (1.7533128, -1.761597, 2.0), Mous Loc: (-0.2457667, 0.23840293, 0.0), Old Gom Loc: (-1.9990795, 2.0, 2.0)
Finl Trans: (-2.0, 2.0009205, 2.0), Mous Loc: (-0.24668714, 0.23932339, 0.0), Old Gom Loc: (1.7533128, -1.761597, 2.0)
Finl Trans: (1.7533128, -1.7615972, 2.0), Mous Loc: (-0.24668714, 0.23932339, 0.0), Old Gom Loc: (-2.0, 2.0009205, 2.0)
Finl Trans: (-2.0009205, 2.001841, 2.0), Mous Loc: (-0.24760765, 0.2402439, 0.0), Old Gom Loc: (1.7533128, -1.7615972, 2.0)
Finl Trans: (1.7533128, -1.7615972, 2.0), Mous Loc: (-0.24760765, 0.2402439, 0.0), Old Gom Loc: (-2.0009205, 2.001841, 2.0)
Finl Trans: (-2.0018408, 2.0027616, 2.0), Mous Loc: (-0.2485281, 0.24116436, 0.0), Old Gom Loc: (1.7533128, -1.7615972, 2.0)
Finl Trans: (1.7533127, -1.7615973, 2.0), Mous Loc: (-0.2485281, 0.24116436, 0.0), Old Gom Loc: (-2.0018408, 2.0027616, 2.0)
Finl Trans: (-2.0027614, 2.0027616, 2.0), Mous Loc: (-0.2494486, 0.24116436, 0.0), Old Gom Loc: (1.7533127, -1.7615973, 2.0)
Finl Trans: (1.7523923, -1.7615973, 2.0), Mous Loc: (-0.25036904, 0.24116436, 0.0), Old Gom Loc: (-2.0027614, 2.0027616, 2.0)