Using mouse movements to move(drag) a node/geom on 2D

Hello people, am sorry if this was already asked before, or there’s a method to do it, but I can’t find it (and I did a lot of goggle searches,and look at wiki tutorials…), I’ m new with JME3, so please really sorry if this seem to be a silly question. I’m working on a project where I have a 2d view, I mean I only work with X and Y axis; I have a cam positioned over the Z axis, so I have a look and feel as 2D;



Now I want to mouse pick/select an Object and move it accordingly with mouse movement; I already learned how to pick and select objects; my problem is trying to move them around.



I’m using this on my SimpleUpdate method:



[java]

public void simpleUpdate(float tpf) {



//

// Move Selected Object!

//

if(isDragging && CurrentNode!=null)

{

Vector3f cursorPos=getMouseMov();



System.out.println(“Debug: Draggin “+CurrentNode.getName()+” X=”+cursorPos.x+ " Y="+cursorPos.y);



CurrentNode.move(cursorPos.x,cursorPos.y,0);



}

}

[/java]



And getMouseMov() method like:



[java]

private Vector3f getMouseMov()

{

Vector3f mousePos=new Vector3f();



Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);



direction.subtractLocal(origin).normalizeLocal();



CollisionResults results = new CollisionResults();

rootNode.collideWith(new Ray(origin, direction), results);



if (results.size() > 0) {

mousePos=results.getClosestCollision().getContactPoint();

}



Vector2f move=mouseCoords.subtract(new Vector2f(mousePos.x,mousePos.y));

System.out.println(“Debug: X0=”+mouseCoords.x+" Y0="+mouseCoords.y);

System.out.println(“Debug: X1=”+mousePos.x+" Y1="+mousePos.y);

if (move.x==0 && move.y==0)

{

mouseInitialClick_X=mousePos.x;

mouseInitialClick_Y=mousePos.y;

System.out.print(“Debug: Mouse Resetting position NO MOUSE MOVEMENT…”);

}

float absX=(float)mousePos.x-mouseInitialClick_X;

float absY=(float)mousePos.y-mouseInitialClick_Y;



mouseCoords.x=mousePos.x;

mouseCoords.y=mousePos.y;

mousePos.x=absX;

mousePos.y=absY;

return mousePos;



}

[/java]



This method, will return only moved units from the last cursor position, will not return a location…



So actually it works, but the actual object that need to be moved always get some offset from mouse pointer, it never stick with the mouse pointer when I’m dragging it… so the result of it, the object is moved a little bit far away from the actual mouse pointer.



Please feel free to modify any wrong coding…



For clarification:

mouseCoords is static and it is used to detect if mouse it’s actually moving or not

mouseInitialClick_X and mouseInitialClick_Y are staitc as well and will hold original coordinates where user clicked it at first time(then after first dragging it will contain the last mouse position)



So basically I need to compensate last object location when is dragged…

I found a thread that I think is trying to do same as me, but they do not publish any solution on it:



http://hub.jmonkeyengine.org/groups/gui/forum/topic/dragging-objects-with-the-mouse



:frowning:

The point of the tutorials is not that you look at them but that you do them, moving objects with the mouse should be a breeze if you actually did them.