JME : Move Node object when shot

Hello friends,

I am trying my hands on a Basic Project in JME, where from one of the tutorial, a map is loaded, and from other tutorial, shooting functionality is taken. Now, I combined both of them, and what I am trying to do now is to move the Node object when someone shoots it. Can someone tell me how I can accomplish that?

Code

 shootables.attachChild(makeCube("a Dragon", +2f, 0f, 1f));
        shootables.attachChild(makeCube("a tin can", +5f, +2f, 0f));
        shootables.attachChild(makeCube("the Sheriff", +8f, 1f, +2f));
        shootables.attachChild(makeCube("the Deputy", +11f, 0f, +4f));

   private ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("Shoot") && !keyPressed) {
                // 1. Reset results list.
                CollisionResults results = new CollisionResults();
                // 2. Aim the ray from cam loc to cam direction.
                Ray ray = new Ray(cam.getLocation(), cam.getDirection());
                // 3. Collect intersections between Ray and Shootables in results list.
                // DO NOT check collision with the root node, or else ALL collisions will hit the skybox! Always make a separate node for objects you want to collide with.
                shootables.collideWith(ray, results);
                // 4. Print the results
                System.out.println("----- Collisions? " + results.size() + "-----");
                for (int i = 0; i < results.size(); i++) {
                    // For each hit, we know distance, impact point, name of geometry.
                    float dist = results.getCollision(i).getDistance();
                    Vector3f pt = results.getCollision(i).getContactPoint();
                    String hit = results.getCollision(i).getGeometry().getName();


                    System.out.println("* Collision #" + i);
                    System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
                }
                // 5. Use the results (we mark the hit object)
                if (results.size() > 0) {
                    // The closest collision point is what was truly hit:
                    CollisionResult closest = results.getClosestCollision();
                    // Let's interact - we mark the hit with a red dot.
                    mark.setLocalTranslation(closest.getContactPoint());
                    rootNode.attachChild(mark);
                } else {
                    // No hits? Then remove the red mark.
                    rootNode.detachChild(mark);
                }
            }
        }
    };


    protected Geometry makeCube(String name, float x, float y, float z) {
        Box box = new Box(1, 1, 1);
        Geometry cube = new Geometry(name, box);
        cube.setLocalTranslation(x, y, z);
        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.randomColor());
        cube.setMaterial(mat1);
        return cube;
    }

    protected Spatial makeCharacter() {
        // load a character from jme3test-test-data
        Spatial golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        golem.scale(0.5f);
        golem.setLocalTranslation(+3.0f, +3.5f, +2.6f);

        // We must add a light to make the model visible
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        golem.addLight(sun);
        return golem;
    }

If someone wants to access the entire source-code, its here : http://pastebin.com/dhr30v0H

It’s not quite as simple as a few lines. the .getGeometry() method of the collisionresult will get you the geometry, which you can compare with the geometries in your scene to determine what it actually was that was hit.

Basically you need to get the closest node and then call move on it.

Try like this:
//When you have your array of collision results
Geometry geo = results.getClosestCollision().getGeometry();
geo.move( something );

Not sure though since it’s been a while I last did anything serious in jME…

How can I get the current position and move from there, not just some random values. Thank you.

geo.getLocalTranslation();

Or world translation if you need that.

1 Like

http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html

http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#getLocalTranslation()

http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#move(float,%20float,%20float)

http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#move(com.jme3.math.Vector3f)

Javadoc is always a good place to start.

1 Like

This worked better than directly getting worldTranslation and adding more distance to it if required.