Error comparing data from setUserData with an int

Why do I get an error in the comparison while writing this? I’m trying to compare the number of lives an object has, which is a number (int), with the number 0 so that it tells the app to remove that object from the rootNode.

[java]
if (casa1.getUserData(“Lives”) == 0) {
rootNode.detachChild(casa1);
}
[/java]

I’m squinting trying to see what the actual error is… but I can’t see it from here.

@pspeed said: I'm squinting trying to see what the actual error is... but I can't see it from here.

That comparison is giving me an error:

[java]
(casa1.getUserData(“Lives”) == 0)
[/java]

Oh nevermind I found the error. Looks like I wasn’t implementing neither the setter nor the getter. Thank you.

@pspeed said: I’m squinting trying to see what the actual error is… but I can’t see it from here.
Ok, so now I'm getting a UnsupportedCollisionException with this code:

[java]
private void initDisparos() {
// 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());
ray.setLimit(50); // Rango del arma
// 3. Collect intersections between Ray and Shootables in results list.
shootables.collideWith(ray, results);

    if (ray.collideWith(casa1, results) <= 3) {
        if (casa1.getVidas() == 0) {
            rootNode.detachChild(casa1);
        } else {
            casa1.setVidas(casa1.getVidas()-1); // -poderArma (1)
        }
    }
    if (ray.collideWith(casa2, results) <= 3) {
        if (casa2.getVidas() == 0) {
            rootNode.detachChild(casa2);
        } else {
            casa2.setVidas(casa2.getVidas()-1); // -poderArma (1)
        }
    }
    if (ray.collideWith(casa3, results) <= 3) {
        if (casa3.getVidas() == 0) {
            rootNode.detachChild(casa3);
        } else {
            casa3.setVidas(casa3.getVidas()-1); // -poderArma (1)
        }
    }

    // 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();
        System.out.println("* Collision #" + i);
        System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
    }
}

[/java]

I set the condition for <= 3 because, I don’t know why, but there seems to always be 3 collisions whenever I “shoot”.

If you are going to leave the stack trace out of an exception then don’t even bother to report it.

@pspeed said: If you are going to leave the stack trace out of an exception then don't even bother to report it.

Sorry, here it is:

[java]
oct 06, 2014 10:18:08 PM com.jme3.app.Application handleError
Grave: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
com.jme3.collision.UnsupportedCollisionException
at com.jme3.math.Ray.collideWith(Ray.java:400)
at mygame.Main.initDisparos(Main.java:274)
at mygame.Main.initMovimiento(Main.java:180)
at mygame.Main.simpleUpdate(Main.java:314)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)

BUILD SUCCESSFUL (total time: 13 seconds)
[/java]

I think all of your ray.collideWith() lines are backwards.

@pspeed said: I think all of your ray.collideWith() lines are backwards.

What do you mean? Like this:
[java]
if (casa1.collideWith(ray, results) <= 3) {
if (casa1.getVidas() == 0) {
rootNode.detachChild(casa1);
} else {
casa1.setVidas(casa1.getVidas()-1); // -poderArma (1)
}
}
[/java]

If so, I’m still getting an exception apparently “not supported yet”:

[java]
oct 06, 2014 10:35:50 PM com.jme3.app.Application handleError
Grave: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.UnsupportedOperationException: Not supported yet.
at mygame.Casa.collideWith(Casa.java:100)
at mygame.Main.initDisparos(Main.java:274)
at mygame.Main.initMovimiento(Main.java:180)
at mygame.Main.simpleUpdate(Main.java:314)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)

BUILD SUCCESSFUL (total time: 13 seconds)
[/java]

Is this even the correct way to compare if a ray hit an object?

What is mygame.Casa?

@pspeed said: What is mygame.Casa?

I’m posting the class:

[java]
public class Casa extends Spatial implements Collidable{

Node estructurasNodo;
Spatial casa;

public Casa(AssetManager assetManager, BulletAppState bulletAppState, float x, float y, float z) {
    estructurasNodo = new Node();
    casa = assetManager.loadModel("Models/house 5.j3o");
    casa.setLocalScale(0.05f, 0.05f, 0.05f);
    casa.setLocalTranslation(x, y, z);
    estructurasNodo.attachChild(casa);
    CompoundCollisionShape casaShape = new CompoundCollisionShape();
    PlaneCollisionShape suelo = new PlaneCollisionShape(new Plane(new Vector3f(0, 1f, 0), 1));
    BoxCollisionShape caja = new BoxCollisionShape(new Vector3f(27f, 30f, 30f));
    casaShape.addChildShape(suelo, casa.getLocalTranslation());
    casaShape.addChildShape(caja, new Vector3f(casa.getLocalTranslation().getX() + 14, casa.getLocalTranslation().getY() + 22, casa.getLocalTranslation().getZ() + 9));
    RigidBodyControl casaControl = new RigidBodyControl(casaShape, 100f);
    estructurasNodo.addControl(casaControl);
    bulletAppState.getPhysicsSpace().add(casaControl);
    bulletAppState.getPhysicsSpace().addAll(estructurasNodo);

    casa.setUserData("Vidas", 3);
}

public int getVidas() {
    return (Integer) casa.getUserData("health");
}

public void setVidas(int vidas) {
    casa.setUserData("Vidas", vidas);
}

public Node getEstructurasNodo() {
    return estructurasNodo;
}

@Override
public void updateModelBound() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setModelBound(BoundingVolume modelBound) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public int getVertexCount() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public int getTriangleCount() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Spatial deepClone() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void depthFirstTraversal(SceneGraphVisitor visitor) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected void breadthFirstTraversal(SceneGraphVisitor visitor, Queue&lt;Spatial&gt; queue) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
[/java]

Well, first, never ever extend Spatial. Never extend Node. Never extend Geometry.

Second, did you see all of the methods that you haven’t even implemented? Why override them if you will not implement them?

It is no surprise that you are getting an exception since your own code throws it:
[java]
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
[/java]

@pspeed said: Well, first, never ever extend Spatial. Never extend Node. Never extend Geometry.

Second, did you see all of the methods that you haven’t even implemented? Why override them if you will not implement them?

It is no surprise that you are getting an exception since your own code throws it:
[java]
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
[/java]

Yes I did not see that method down there, my bad. But then how do I create a Spatial if I don’t want to do it directly in the Main class but from within another class and then call it from the Main?

Also, regarding the other question, what do I use to compare a hit from a ray with a Spatial?

@DualHunter said: Yes I did not see that method down there, my bad. But then how do I create a Spatial if I don't want to do it directly in the Main class but from within another class and then call it from the Main?

Node myNode = new Node();
…attach the stuff you want to the node… controls, whatever.

Instead of extending Spatial, add a custom control.

@DualHunter said: Also, regarding the other question, what do I use to compare a hit from a ray with a Spatial?

spatial.collideWith(ray, results);

…on a spatial where you haven’t messed up all of the methods by overriding them with exception-throwing code.

@pspeed said: Node myNode = new Node(); …attach the stuff you want to the node… controls, whatever.

Instead of extending Spatial, add a custom control.

Do you mean, like, making a method to return a Node from inside the class to the Main and the attaching it to the rootNode. Am I not doing that already?

Here I create the new Node and attach the Spatial “casa” to it:

[java]
estructurasNodo = new Node();
casa = assetManager.loadModel(“Models/house 5.j3o”);
casa.setLocalScale(0.05f, 0.05f, 0.05f);
casa.setLocalTranslation(x, y, z);
estructurasNodo.attachChild(casa);
[/java]

Then I create a method to return the Node “estructurasNodo”, having already attached the Spatial:

[java]
public Node getEstructurasNodo() {
return estructurasNodo;
}
[/java]

Finally I attatch that Node to the rootNode in the Main:

[java]
casa1 = new Casa(assetManager, bulletAppState, 40f, 0f, 20f);
casa2 = new Casa(assetManager, bulletAppState, -32f, 0f, 20f);
casa3 = new Casa(assetManager, bulletAppState, -100f, 0f, 20f);
rootNode.attachChild(casa1.getEstructurasNodo());
rootNode.attachChild(casa2.getEstructurasNodo());
rootNode.attachChild(casa3.getEstructurasNodo());
[/java]

Is that what you meant?