I am very new to this JMonkey engine and currently working on my project. I have been trying to figure out to how new geometries could be added when the user clicks on the screen.
So what I am trying to implement is that the user clicks on the screen and a shape appears on the position the user clicks. Any hints would be very helpful. Thank you
Use the picking tutorial code for finding the place they clicked, then:
[java]
Box b = new box(1,1);
Geometry geom = new Geometry();
geom.setMesh(b);
Node n = new Node();
n.attachChild(geom);
n.setMaterial(mat);
n. setLocalTranslation(vectorUserClicked); // contact point returned from picking
rootNode.attachChild(n);
[/java]
or use enqueue if you are doing this from a separate thread.
I have tried the same way but the shape does not display on the screen.
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals(MAPPING_ENEMIES)&& !isPressed) {
System.out.println("Added enemies");
Vector3f location = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0);
System.out.println(inputManager.getCursorPosition().toString());
Predators predator = new Predators (location.x,location.y,location.z);
enemies.add(predator);
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
System.out.println(enemies.get(enemies.size()-1).vector.toString());
Dome cone = new Dome(enemies.get(enemies.size()-1).vector,2,4,5f);
Geometry geom3 = new Geometry("Cone", cone);
mat2.setColor("Color", enemies.get(enemies.size()-1).getColour());
geom3.setMaterial(mat2);
enemeiesgeometries.add(geom3);
enemy.attachChild(geom3);
}
}
};
@Ryuzaki said: I have tried the same way but the shape does not display on the screen.private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals(MAPPING_ENEMIES)&& !isPressed) {System.out.println("Added enemies"); Vector3f location = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0); System.out.println(inputManager.getCursorPosition().toString()); Predators predator = new Predators (location.x,location.y,location.z); enemies.add(predator); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); System.out.println(enemies.get(enemies.size()-1).vector.toString()); Dome cone = new Dome(enemies.get(enemies.size()-1).vector,2,4,5f); Geometry geom3 = new Geometry("Cone", cone); mat2.setColor("Color", enemies.get(enemies.size()-1).getColour()); geom3.setMaterial(mat2); enemeiesgeometries.add(geom3); enemy.attachChild(geom3); } } };</blockquote>
This isn’t doing what you think it is.
cam.getWorldCoordinates is not the same as casting a ray into the scene and finding the location on a geometry that the user clicked. The picking tutorial above should cover how to do that… if not, then there is another picking tutorial later in the series that definately does.
Other question: Is enemiesgeometries attached to your scene graph? If not, they won’t show up. (i.e. is it attached to rootNode or nested in another node that is attached to rootNode)
I have only used enemies to store different geometries but the enemy is the node thats attached to rootNode in the simpleInitApp() function.