Selecting Objects with Crosshair

Hi, i need a method to select objects with the crosshair of my application (its in the middle ofc).
i mean, if theres ingame an object on the ground that i can choose stuff like “colloect” or “exaime/ discribe” or also if i have blocks like in minecraft, that i can harvest them. any ideas? maybe example classes or links?

Might be worth going through all of the tutorials:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3

Well, i can explain you how to do it. But follow the tutorials above, because you REALLY need to do it.

To make that happen, JMonkey has a Ray class (com.jme3.math.Ray). As the name suggests, it’s a ray that collides with the objetcs. Then, with the help of the CollisionResults class (com.jme3.collision.CollisionResults), you can get information of that Spatial or Geometry.

So, here is how it works:

[java]
CollisionResults results = new CollisionResults(); //The results of the collision
Ray ray = new Ray(cam.getLocation(), cam.getDirection()); //The ray to determine collisions.
rootNode.collideWith(ray, results); //It combines the two of them, I think… but you don’t need to know it well.
if(results.size() > 0) { //If the size of the results is more than zero, he collided with something
Geometry target = results.getClosestCollision().getGeometry(); //Creates a Geometry using the closest Geometry detected by the ray.
if(target.getName().equals(“Cube”)) { //Here you get the name of the Geometry to do specific actions. Eg. A “Wheat” cube can be harvested, and the “Chest” cube needs to store and take itens from it.
//Here you use the target geometry to do what you want!
// By the way, you set the name of the Geometry on its construtor(new Geometry(box, “name of your geometry”)) or by using the setName(String name) method.
}
}
}
[/java]

There you have, hope i helped you!

PS: Im not english, so im sorry if there are mistakes.

1 Like

Sorry, i forgot something. You need to put that piece of code in the simpleUpdate method:

[java]@Override
public void simpleUpdate(float tpf) {
CollisionResults results = new CollisionResults(); //The results of the collision
Ray ray = new Ray(cam.getLocation(), cam.getDirection()); //The ray to determine collisions.
rootNode.collideWith(ray, results); //It combines the two of them, I think… but you don’t need to know it well.
if(results.size() > 0) { //If the size of the results is more than zero, he collided with something
Geometry target = results.getClosestCollision().getGeometry(); //Creates a Geometry using the closest Geometry detected by the ray.
if(target.getName().equals(“Cube”)) { //Here you get the name of the Geometry to do specific actions. Eg. A “Wheat” cube can be harvested, and the “Chest” cube needs to store and take itens from it.
//Here you use the target geometry to do what you want!
// By the way, you set the name of the Geometry on its construtor(new Geometry(box, “name of your geometry”)) or by using the setName(String name) method.
}
}
}[/java]

Sorry.

I agree that it is VERY important to do the tutorials.
It teaches many things that you might think are more complicated than they are.

For example selecting objects (https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking)