# Mouse-driven Movement

I'm trying to write a game where player picks where they are going by just mouse-clicking on that position and it goes there (just like RuneScape's point-and-click mouse interface).

I'm trying to get it working on the jme3test.bullet.TestWalkingChar example.

I've tried:

character.setLocalTranslation(
cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f));


Also tried:

Vector3f worldCoordinates = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f localCoordinates = null;

character.worldToLocal(worldCoordinates, localCoordinates);
character.setLocalTranslation(localCoordinates);


and:

Vector3f worldCoordinates = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f localCoordinates = null;

character.setLocalTranslation(character.worldToLocal(worldCoordinates, localCoordinates));


But it's just teleporting the player to some unrelated coordinates.

Any ideas?

BTW: If there's a complete example on how to do that, it'd be great. I don't care about game engine - I prefer jME3 but it doesn't really matter for me.

You're taking absolute world coordinates and feeding them to setLocalTranslation which, being a translation, sounds like it should take relative coordinates.

i.e. if your character is at (10,10) and you click (10,20), I'll bet your character will move to (20,30) - it moved 10 horizontally and 20 vertically, not to (10,20).

Instead you should find a method which takes absolute coordinates.

If that method doesn't exist (I will be genuinely surprised if it doesn't), you can instead determine the relative coordinates and feed them to setLocalTranslation instead. However this approach involves a lot of unnecessary calculations.

How to determine those relative coordinates, as pseudocode:

Vector clickedPos
Vector characterPos
Vector translation
translation.X = clickedPos.X - characterPos.X
translation.Y = clickedPos.Y - characterPos.Y
translation.Z = et cetera...
...
character.setLocalTranslation(translation)

• There is an absolute-to-local coordinates convertion method, please look at my second and third example of code - same result. – Alon Gubkin Oct 25 '10 at 13:25
• My mistake. All I can imagine is that the methods you're using don't do what you think they're supposed to do, or your coordinates are at some point being fetched or used in the wrong way. I would run some simple tests, e.g. put your character at (0,0), try to move it to (1, 1) and see what happens to its coordinates, and other simple operations like that (not all from the origin of course). Simple, small numbers ought to make the nature of the problem easier to determine. – doppelgreener Oct 25 '10 at 18:20

Turns out that there is built-in functionality for this in the jMonkeyEngine framework:

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) {
character.setLocalTranslation(results.getClosestCollision().getContactPoint());
}


Thanks anyway!