in my game, when a model is clicked, an event must occur
but this only works when you click a BOX, SPHERE … when you click a model.j3o, an error occurs.
my function:
private void initClick()
{
inputManager.addMapping("Shoot",
new KeyTrigger(KeyInput.KEY_F1), // trigger 1: spacebar
new MouseButtonTrigger(1)); // trigger 2: left-button click
inputManager.addListener(actionListener, "Shoot");
}
private ActionListener actionListener = new ActionListener()
{
@Override
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.
//andares[indexAndar].pegaApIndex().pegaComodoIndex().getNode().collideWith(ray, results);
rootNode.collideWith(ray, results);
// 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();
if (hit != null)
{
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
click (hit);
}
}
}
}
};
error when click a model.j3o:
Collisions? 2
Exception in thread "LWJGL Renderer Thread" java.lang.NullPointerException
at V3D.Controller.Cena$1.onAction(Cena.java:195)
at com.jme3.input.InputManager.invokeActions(InputManager.java:112)
at com.jme3.input.InputManager.onMouseButtonEvent(InputManager.java:271)
at com.jme3.input.lwjgl.LwjglMouseInput.update(LwjglMouseInput.java:74)
at com.jme3.input.InputManager.update(InputManager.java:394)
at com.jme3.app.Application.update(Application.java:392)
at com.jme3.app.SimpleBulletApplication.update(SimpleBulletApplication.java:270)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:112)
at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:190)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:162)
at java.lang.Thread.run(Thread.java:619)
line 195:
String hit = results.getCollision(i).getGeometry().getName();
Can someone help me?