Help something wrong with collideWith()

I use jme3 sdk to pratice this example: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking, but I got the result different from the explanation.

This is my result:

----- Collisions? 2-----

  • Collision #0
    You shot a Dragon at (-2.5846875, 0.5756055, 2.0), 8.426858 wu away.
  • Collision #1
    You shot a Dragon at (-3.0000002, 0.66809493, 0.7145443), 9.780902 wu away.
    ----- Collisions? 4-----
  • Collision #0
    You shot a Dragon at (-1.1484121, -0.5137089, 2.0), 8.098318 wu away.
  • Collision #1
    You shot a Dragon at (-1.435515, -0.6421361, 0.0), 10.122897 wu away.
  • Collision #2
    You shot Oto-geom-1 at (-1.4828281, -0.6633002, -0.32958984), 10.456537 wu away.
  • Collision #3
    You shot Oto-geom-1 at (-1.535764, -0.6869795, -0.69834805), 10.829827 wu away.
    ----- Collisions? 4-----
  • Collision #0
    You shot a tin can at (0.4926858, -2.585003, 1.0), 9.376832 wu away.
  • Collision #1
    You shot a tin can at (0.5717817, -2.9999998, -0.4448614), 10.882191 wu away.
  • Collision #2
    You shot the Floor at (0.7242569, -3.8, -3.2301588), 13.784108 wu away.
  • Collision #3
    You shot the Floor at (0.8004944, -4.2, -4.6228065), 15.235066 wu away.

As you see, each object is double collided, but in the example the object is only collided once.

My program source is the same as example, but why the difference occurd?

@liucen said: I use jme3 sdk to pratice this example: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking, but I got the result different from the explanation.

This is my result:

----- Collisions? 2-----

  • Collision #0
    You shot a Dragon at (-2.5846875, 0.5756055, 2.0), 8.426858 wu away.
  • Collision #1
    You shot a Dragon at (-3.0000002, 0.66809493, 0.7145443), 9.780902 wu away.
    ----- Collisions? 4-----
  • Collision #0
    You shot a Dragon at (-1.1484121, -0.5137089, 2.0), 8.098318 wu away.
  • Collision #1
    You shot a Dragon at (-1.435515, -0.6421361, 0.0), 10.122897 wu away.
  • Collision #2
    You shot Oto-geom-1 at (-1.4828281, -0.6633002, -0.32958984), 10.456537 wu away.
  • Collision #3
    You shot Oto-geom-1 at (-1.535764, -0.6869795, -0.69834805), 10.829827 wu away.
    ----- Collisions? 4-----
  • Collision #0
    You shot a tin can at (0.4926858, -2.585003, 1.0), 9.376832 wu away.
  • Collision #1
    You shot a tin can at (0.5717817, -2.9999998, -0.4448614), 10.882191 wu away.
  • Collision #2
    You shot the Floor at (0.7242569, -3.8, -3.2301588), 13.784108 wu away.
  • Collision #3
    You shot the Floor at (0.8004944, -4.2, -4.6228065), 15.235066 wu away.

As you see, each object is double collided, but in the example the object is only collided once.

My program source is the same as example, but why the difference occurd?

Both sides of the object likely. first collision is the front, second is the back.

I agree with you. But there is another question .

I changed the cube, located only one cube at (0,0,0) and set its size (2,2,2):
public void simpleInitApp() {
initCrossHairs(); // a “+” in the middle of the screen to help aiming
initKeys(); // load custom key mappings
initMark(); // a red sphere to mark the hit

/** create four colored boxes and a floor to shoot at: */
shootables = new Node("Shootables");
rootNode.attachChild(shootables);
shootables.attachChild(makeCube("a Dragon", 0f, 0f, 0f));

}

protected Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(2, 2, 2);
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;
}

I click the mouse at cube center, and this is the result:
----- Collisions? 4-----

  • Collision #0
    You shot a Dragon at (0.0, 0.0, 2.0), 8.0 wu away.
  • Collision #1
    You shot a Dragon at (0.0, 0.0, 2.0), 8.0 wu away.
  • Collision #2
    You shot a Dragon at (0.0, 0.0, -2.0), 12.0 wu away.
  • Collision #3
    You shot a Dragon at (0.0, 0.0, -2.0), 12.0 wu away.

four collisions. That’s amazing.

You should actually print out the geometries too… maybe even their System.identityHashCode() so that you can track down why you have more than one. Only when you truly believe that you have more than one will you be able to find it.

Edit: and/or put together a single class test case the still exhibits the problem and post the whole thing so that we can see what else might be wrong.

That’s my program source. I just attach one cube into the world, located it at the Zero point.
I clicked the mouse when program started without any other action. The result show four collisions. Is this a bug?

[java]
package jme3test.helloworld;

import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;

public class HelloPicking extends SimpleApplication {

public static void main(String[] args) {
HelloPicking app = new HelloPicking();
app.start();
}
private Node shootables;
private Geometry mark;

public void simpleInitApp() {
initCrossHairs(); // a “+” in the middle of the screen to help aiming
initKeys(); // load custom key mappings
initMark(); // a red sphere to mark the hit

shootables = new Node("Shootables");
rootNode.attachChild(shootables);
shootables.attachChild(makeCube("a Dragon", 0f, 0f, 0f));

}

private void initKeys() {
inputManager.addMapping(“Shoot”,
new KeyTrigger(KeyInput.KEY_SPACE), // trigger 1: spacebar
new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); // trigger 2: left-button click
inputManager.addListener(actionListener, “Shoot”);
}

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.
    int num = shootables.collideWith(ray, results);
    //System.out.println(num);
    // 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(2, 2, 2);
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 Geometry makeFloor() {
Box box = new Box(15, .2f, 15);
Geometry floor = new Geometry(“the Floor”, box);
floor.setLocalTranslation(0, -4, -5);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.Gray);
floor.setMaterial(mat1);
return floor;
}

protected void initMark() {
Sphere sphere = new Sphere(30, 30, 0.2f);
mark = new Geometry(“BOOM!”, sphere);
Material mark_mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mark_mat.setColor(“Color”, ColorRGBA.Red);
mark.setMaterial(mark_mat);
}

protected void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - ch.getLineWidth()/2, settings.getHeight() / 2 + ch.getLineHeight()/2, 0);
guiNode.attachChild(ch);
}

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(-1.0f, -1.5f, -0.6f);

DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
golem.addLight(sun);
return golem;

}
}
[/java]

@pspeed said:

I have submitted my source as javacode again.