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++;
}
}