[SOLVED] How can I create new objects during running the app by clicking mouse

Hey Guys,
i’m sure its a silly beginner question, but i have the following newbie problem. :}
By clicking my model in the scene, i detect the collision point and mark it with an red sphere.
No problem at this point. (look at code)

public void onAction(String name, boolean isPressed, float tpf) {
    if (name.equals("Shoot") && !isPressed) {
       // Reset results list.
       CollisionResults results = new CollisionResults();
       // Convert screen click to 3d position
       Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y),0f).clone();
       Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y),1f).subtractLocal(click3d).normalizeLocal();
       // Aim the ray from the clicked spot forwards.
       Ray ray = new Ray(click3d, dir);
       // Collect intersections between ray and all nodes in results list.
       modelNode.collideWith(ray, results);
       if (results.size() > 0) {
          // The closest collision point is what was truly hit:
          CollisionResult closest = results.getClosestCollision();
          Vector3f v = new Vector3f();
          v = modelNode.worldToLocal(closest.getContactPoint(), v); \\get local position
          // Let's interact - we mark the hit with a red dot.                 
       } else {
          // No hits? Then remove the red mark.

But how can I get different spheres. One more with every mouse click, without loosing the current sphere.

In the beginner tutorials, allways create all objects in the beginning. Then the scene is initialized and the app started. However, how can i create new objects during running the app and add them permanently to the node .
In my case, different instances of the spehre at different positions, by clicking.

I hope somebody can give me a helpful advice where I can read more about this topic.

Best regards,
EsKay :}

Where you want to create a new sphere, do this:

Sphere sphere = new Sphere(30, 30, 0.2f);
Geometry mark2 = new Geometry("BOOM!", sphere);
Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mark_mat.setColor("Color", ColorRGBA.Red);
mark2.setLocalTranslation(x, y, z);

Edit: oops, small typo
And BTW, mouse picking does not involve physics… :smile:

1 Like

Thanks dude!

This was too easy [head meets wall] error in my thinking :3
Logically, I have to put creation of sphere in the action methode and not at the beginning before starts app.

This help me much :}

And sorry for putting my question in the wrong topic :}
In which topic it would normally belong? ^.^ (maybe generally?)

Thank you very much

It depends on the question :smile:
In this case, probably troubleshooting->graphics or just plain troubleshooting.

You’re welcome! Looking forward to see your game :sunglasses:

1 Like