Each Geometry has exactly one weapon associated with it right? (because it is the geometry of the weapon). And you have the geometry, but want the weapon.
When creating
Geometry geom = new Geometry("a box", aBox);
geom.setUserData("weapon", this);
Looking at your picking code again it looks different from what I’d expect. Usually you’d pick against the root node (i.e. the entire world at once) and see what you hit with your ray. So something like this:
protected void interact(Node rootNode, Vector3f startPosition, Vector3f lookDirection) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(startPosition, lookDirection);
rootNode.collideWith(ray, results);
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
Weapon weapon = (Weapon)closest.getGeometry().getUserData("weapon");
if (weapon!= null) {
//do whatever happens when a weapon gets picked
weapon.kill();
}
}
}
Or, if you want to be able to pick not just the closest, but everything you are looking at
protected void interact(Node rootNode, Vector3f startPosition, Vector3f lookDirection) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(startPosition, lookDirection);
rootNode.collideWith(ray, results);
for(CollisionResult result : results){
Weapon weapon = (Weapon)result.getGeometry().getUserData("weapon");
if (weapon!= null) {
//do whatever happens when a weapon gets picked
weapon.kill();
}
}
}
Although I guess there’s nothing wrong with prepicking out what you can pick
protected void interact(ArrayList<Interactable> interacts, Vector3f lookDirect) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(pos, lookDirect);
for (Interactable i : interacts) i.getNode().collideWith(ray, results);
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
Weapon weapon = (Weapon)closest.getGeometry().getUserData("weapon");
if (weapon!= null) {
//do whatever happens when a weapon gets picked
weapon.kill();
}
}
}
Of course everything but the first example isn’t blocked by other geometry, its an “X ray pick” rather than a normal sight pick. Which may or may not be what you want