[SOLVED] Model geometry name doesn’t match

Hello, I’m using Raycasting to apply an impulse to a loaded model, when the Raycast hits the model it applies the impulse. I’ve printed the geometry name in the console with (hit), the name of the geometry is “Ninja-geom-1”, but it doesn’t seem to match.



Here’s the code: [java]if (binding.equals(“ApplyImpulse”)) {

CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

ninja.collideWith(ray, results);

for (int i = 0; i < results.size(); i++) {

float dist = results.getCollision(i).getDistance();

Vector3f pt = results.getCollision(i).getContactPoint();

String hit = results.getCollision(i).getGeometry().getName();

System.out.println(hit);

if(hit == “Ninja-geom-1”) {

System.out.println(“Bam!”);

ninja.getControl(RigidBodyControl.class).applyImpulse(new Vector3f(10,0,0), pt);

}[/java]

I don’t know if I’m just doing something wrong or if this is some kind of bug.

Thanks! :smiley:

You are doing an == string compare which means that they must be the exact same instance of String object… not just the same value.



Anyway, string comparison is a really poor way to see if it is the right object. It would be better to tag it with some UserData or something.

2 Likes

I’ve now been trying to tag the spatial with UserData, but it seems that when I use [java]results.getCollision(i).getGeometry().getName();[/java] it returns the ninja geometry and not the ninja with the sword (spatial). How would I get the spatial name instead of the geometry name?

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()

And then maybe that again.



I’ve personally responded to this question so many times that I will just refer you to the google search in the upper right if you need more information.

1 Like

Alright. Thank you for the help! :slight_smile: