I declared a PickTool at the top of my World:
protected PickTool picker;
Next, I set all of the shapes I want to pick from to be pickable:
shape3d.setPickable(true);
Then I try picking:
public void doPicking() {
picker.setShapeRay(p_eye, velocity);
PickResult picked = picker.pickClosest();
if (picked != null) {
velocity.z = 0;
velocity.x = 0;
}
else {
return;
}
PickIntersection intersect = picked.getIntersection(0);
Point3d nextpoint = intersect.getPointCoordinates();
}
This is from a tutorial at http://www.benmoxon.info/Java3d/purple7.htm.
Nothing happens when there is a collision. The PickResult is always
null. What am I doing wrong?
/**
* Does picking
* @see http://www.benmoxon.info/Java3d/purple7.htm
* @return true if something was picked
*/
public boolean doPicking() {
if (pick_tool == null) {
// create the pick_tool to always use the map
pick_tool = new PickTool(bgMain);
}
// set the shape of the pick tool as a ray from the current location
// in the direction of the move
pick_tool.setShapeRay(p_eye, velocity);
// get a pick to the closest shape
PickResult res = pick_tool.pickClosest();
// if we got a result, check for the closest intersection distance
if (res != null) {
float closestIntersect = Float.MAX_VALUE;
// a big number just in case
for (int j = 0;j < res.numIntersections(); j++) {
if ( (j == 0) || (res.getIntersection(j).getDistance() <
closestIntersect) ) {
closestIntersect = (float) res.getIntersection(j).getDistance();
}
}
if (closestIntersect < 2.0) {
// collision, adjust accordingly
}
return true;
}
return false;
}
Gaaah! Even more evil Formatting! I fixed it for you: