Here is the code:
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix4f;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private static final Trigger TRIGGER_ROTATE =
new MouseButtonTrigger(MouseInput.BUTTON_LEFT);
private static final String MAPPING_ROTATE = "Rotate";
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if(name.equals(MAPPING_ROTATE)){
CollisionResults results = new CollisionResults();
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(
new Vector2f(click2d.getX(), click2d.getY()), 0f);
Vector3f direction = cam.getWorldCoordinates(
new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);
Ray ray = new Ray(click3d, direction);
rootNode.collideWith(ray, results);
if(results.size() > 0){
Geometry target = results.getClosestCollision().getGeometry();
target.rotate(0.0f, value, 0.0f);
}
}
}
};
public static void main(String[] args) {
new Main().start();
}
@Override
public void simpleInitApp() {
rootNode.attachChild(makeCube("Red cube",
new Vector3f(0f,1.5f,0f), ColorRGBA.Red));
rootNode.attachChild(makeCube("Blue cube",
new Vector3f(0f,-1.5f,0f), ColorRGBA.Blue));
attachCenter();
flyCam.setMoveSpeed(50);
flyCam.setDragToRotate(true);
inputManager.setCursorVisible(true);
inputManager.addMapping(MAPPING_ROTATE, TRIGGER_ROTATE);
inputManager.addListener(analogListener, new String[]{MAPPING_ROTATE});
}
@Override
public void simpleUpdate(float tpf) {}
@Override
public void simpleRender(RenderManager rm) {}
public Geometry makeCube(String name, Vector3f location, ColorRGBA color){
Box box = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(name, box);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", color);
geom.setMaterial(mat);
geom.setLocalTranslation(location);
return geom;
}
public void attachCenter(){
Geometry c = makeCube("Center square", Vector3f.ZERO, ColorRGBA.White);
c.scale(4.0f);
c.setLocalTranslation(cam.getWidth()/2, cam.getHeight()/2, 0.0f);
guiNode.attachChild(c);
}
}
And one more video.
https://www.youtube.com/watch?v=vyzkKQoZc3Q
As you can see, when I click on the red cube, the cursor ‘jump’ to the blue cube, and as I continue clicking, the red cube still rotate (not the blue). And vice versa, when I click on the blue cube, the cursor jump to the red cube, and as I continue clicking (and holding for awhile ), the blue cube rotate (not the red cube). This’s so … strange.