Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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)
share|improve this answer
    
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
up vote 2 down vote accepted

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.