How to pick 3d objects in simpleBulletApplication

I worte some codes to pick 3d objects in simpleBulletApplication. I copied the codes of HelloPicking into my codes. But when the objects left the original positions, the program could not detect them, and when they came back to their original positions , they could be detected. I think  maybe the area which can be detected did not move along with the 3d shape.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jmonkeytest;


import com.jme3.app.SimpleBulletApplication;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
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.shape.Box;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author Administrator
 */
public class TestPick extends SimpleBulletApplication{
    Geometry box;
  Geometry mark;
   PhysicsNode node1 ;
   int count;
    public static void main (String args[]){
        TestPick tp = new TestPick();
        tp.start();
    }
    public void simpleInitApp() {
    initCrossHairs();
    initKeys();
    initMark();
            Material mat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
    mat.setColor("m_Color", ColorRGBA.Orange);
        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
    mat2.setColor("m_Color", ColorRGBA.Gray);
    Box bo = new Box(Vector3f.ZERO,1,1,1);
    box = new Geometry("Box", bo);
    box.setMaterial(mat);
    rootNode.attachChild(box);


     node1 = new PhysicsNode(box, new BoxCollisionShape(new Vector3f(1f, 1f, 1f)), 1);
     getPhysicsSpace().add(node1);
     rootNode.attachChild(node1);
     node1.updateGeometricState();
     node1.updateModelBound();
     node1.updatePhysicsState();

       Box bo2 = new Box(Vector3f.ZERO,10,1,10);
    Geometry box2 = new Geometry("Box", bo2);
    box2.setMaterial(mat2);
   // box2.setLocalTranslation(0f, -1f, 0f);
    rootNode.attachChild(box2);

     PhysicsNode node2 = new PhysicsNode(box2, new BoxCollisionShape(new Vector3f(10f, 1f, 10f)), 0);
     getPhysicsSpace().add(node2);
     rootNode.attachChild(node2);
     node2.setLocalTranslation(0f, -2f, 0f);
     node2.updateGeometricState();
     node2.updateModelBound();
     node2.updatePhysicsState();
    }
  private void initKeys() {
    inputManager.addMapping("Shoot",
      new KeyTrigger(KeyInput.KEY_SPACE), // trigger 1: spacebar
      new MouseButtonTrigger(0));         // 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.
        node1.collideWith(ray, results);
        // 4. Use results. (We print them and mark the hit with a red dot.)
        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.");
          // The closest collision point is what was truly hit - let's mark it.
          mark.setLocalTranslation(results.getClosestCollision().getContactPoint());
          rootNode.attachChild(mark);
        }
        // No hits? Then remove the red mark.
        if (results.size() == 0) {
          rootNode.detachChild(mark);
        }
      }
      rootNode.updateGeometricState();
     rootNode.updateModelBound();
    }
  };

   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/SolidColor.j3md");
    mark_mat.setColor("m_Color", ColorRGBA.Red);
    mark.setMaterial(mark_mat);
  }

  /** A centred plus sign to help the player aim. */
  protected void initCrossHairs() {
    guiNode.detachAllChildren();
    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 - guiFont.getCharSet().getRenderedSize()/3*2,
      settings.getHeight()/2 + ch.getLineHeight()/2, 0);
    guiNode.attachChild(ch);
  }

      public void simpleUpdate(float tfp) {
        //每20帧执行
        if (count > 20) {
            //在boxNode上添加一个力
           node1.applyForce(new Vector3f(0f, 150f, 0f), new Vector3f(0f, 0f, 0f));
            count = 0;
        }
        count++;
    }
}

First, you attach the box to the rootNode and not to the PhysicsNode and then its attached to the PhysicsNode in the constructor, you sure you wanna do that ;). Second, how do your boxes move? You never seem to add them to the PhysicsSpace?

Cheers,

Normen

normen said:

First, you attach the box to the rootNode and not to the PhysicsNode and then its attached to the PhysicsNode in the constructor, you sure you wanna do that ;). Second, how do your boxes move? You never seem to add them to the PhysicsSpace?
Cheers,
Normen


I'm not quite sure about the relationship between the rootNode and the PhysicsNode. I just follow the help codes. I added the physicsNode to the physicsSpace and added a force on one of the boxes per frame. So it works.

Here is another question. If I add a velocity on a box it works , but if I chang the mass of the box before the velocity can not effect the box.
double1984 said:

Here is another question. If I add a velocity on a box it works , but if I chang the mass of the box before the velocity can not effect the box.

Yeah, might be that setMass() is still bugged, always best to set the mass in the constructor.

Hi!



I need this funcionality also, it is the very basis to stuff work here, and I cant figure out what is the problem that only allows the box to be hit if it does not move with physics :(.



@double1984 did u manage to fix it ? :smiley:

teique said:

Hi!

I need this funcionality also, it is the very basis to stuff work here, and I cant figure out what is the problem that only allows the box to be hit if it does not move with physics :(.

@double1984 did u manage to fix it ? :D


I am still an amateur and I do not have the ability to fix this bug. But the developer had fixed the ray collision with moved and rotated objects, I am not sure if this bug is already fixed. Maybe, you can update the jar package to the latest one, and try it again.

I looked at the generated collision positions and saw too small values, even if box was far away, so I thought the hit position is relative to the object being hit.

I just added it to the translation position before setting it in the marker, and it seems to be working!! (I renamed some vars…)

         CollisionResult col = results.getClosestCollision();

         Geometry geom = col.getGeometry();

         Vector3f posHit = col.getContactPoint();

         Vector3f posMark = geom.getLocalTranslation().add(posHit);



thx dude!!



I think such example/test would be very helpfull to ppl that are beggining in with physics and picking objects, what is the very basis for a good game IMHO :smiley:



PS.: I got this snapshot libs also jME3_08-18-2010.zip, dunno if related also…

Latest svn version contains a getWorldContactPoint() method :slight_smile: