[Solved] Moving a node to a clicked position

Hi,

How can I move a node to a clicked position?
Something similar to “the sims” like gameplay.
So you click on a spot (on the terrain) and the model will graduatly move to this position.

What would be the best way to accomplish this?
I was thinking:
-catch the click event, to get the X and Z coordinates (the object does not have to move up).
-set the animation to walking
-use the game loop to move the object a bit closer to the clicked position on every iteration.
-if position is reached, stop walking.

I get the general idea, but I’m not sure how I can implement this.
I’m new to game programming so please be gentle…

ps: How do I activate the mouse cursor? I tried:
[java]inputManager.setCursorVisible(true);[/java]
But that does not seem to work.

Thanks in advance!

First things first. You should look in the Tutorials, the Hello Picking one. Actually, you should go for all tutorials, then start coding while reading the intermediate stuff, then the advanced ones.

Obviously yes.
That is what I am doing.

I’m not saying I’ve read all the tutorials, but I’m working on it.
In the meanwhile, I’m creating mini projects that all tackle one problem at a time (baby steps) like the problem I’m having here.

Going over tutorials is nice, but where is the fun in that? I want to learn new things while I am working on my game.
And I learn a ton from every problem I handle.

But sometimes I just need a little push in the right direction.

No, first you read all easy tutorials, then you move doing something while reading the intermediate stuff and the javadocs.

[java]
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();

        Ray ray = new Ray(origin, direction);

        CollisionResults results = new CollisionResults();

        if (collideableNode.collideWith(ray, results) > 0)[/java]

I never said I didn’t read the easy tutorials. What I meant was that I am learning all the basics and I’m also working on a game (just because I find only reading tutorials is pretty boring).

Anyways, the code above looks similar to the picking tutorial. (Like I said, I did go through the tutorials)
I Managed to get the player to move to the clicked position (I tried it before using picking, but didn’t manage to get it to work properly, so thank you!)
But now when I click the player moves to the clicked location at once.
This is the code I’ve used (in the analogListener):
[java]
if (name.equals(“LeftClick”)) {
CollisionResults results = new CollisionResults();
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
Ray ray = new Ray(origin, direction);
scene.collideWith(ray, results);//scene contains the terrain (for now)
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
player.setLocalTranslation(closest.getContactPoint());//player = my main character
}
}[/java]

I know I have to increment the movement using something like the simpleUpdate() method.
But I’m a bit confused on how to do this. The clickEvent is caught in the analogListener. and as far as I know this is only called once every time you click.
How can I incrementally update the players position so it will move slowly to the clicked position?
This question is probably a stupid one, but at the moment I have no idea on how I can accomplish this.

Thanks again for the help.

There are some examples how to move a node:
http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fintermediate%3Fstate%3Dclosed

Well thank you very much!
Exactly what I needed!

My player now walks to a clicked position in a normal way.
Only problem I have is that the player rotates 90° so it is basically walking on it’s ass :stuck_out_tongue:
But I’m sure I can get that fixed :slight_smile:

Thank you!

What tutorials did you look at? The tutorials I see each make you write at least 3 little apps if you go through them properly ^^

Also, have a look here: http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fbasics
SpatialMotionsLinear.java, SpatialMotionsNonLinear.java, SpatialMotionsSmooth.java.
There are different styles of motions (linear/nonlinear). It will also help you to unedrstand all things of motion and rotation. :slight_smile:

@mifth:

@mifth said:Also, have a look here: http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fbasics

Thanks for this. This is quite interersting, but is there any documentation attached? It’s not that easy to interprete java code…

There was some talk about one year ago with core devs to include simple examples project to JME-tests. But they declined it. And i had no time to make documentation. These are examples of different JME users. And if you want to contribute or make docs, so you are welcome. :slight_smile:

@normen said:What tutorials did you look at? The tutorials I see each make you write at least 3 little apps if you go through them properly ^^

I went trough most of the beginner tutorials here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3 .

@mifth said:Also, have a look here: http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fbasics SpatialMotionsLinear.java, SpatialMotionsNonLinear.java, SpatialMotionsSmooth.java. There are different styles of motions (linear/nonlinear). It will also help you to unedrstand all things of motion and rotation. :)

Thank you, I will surely look into them!

@pinaise said:
@normen said:What tutorials did you look at? The tutorials I see each make you write at least 3 little apps if you go through them properly ^^

I went trough most of the beginner tutorials here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3

Like read the headlines? I see tasks at the bottom of each in my browser?