Picking is returning the wrong coordinates

Hello.



This will be my first post here. Very excited :slight_smile:

I have a problem with the coordinates returned from picking. I have two models in my scene and when I shoot a ray at them I can correctly access their geometry. Now the problem is that the coordinates for the pick stays the same if I move the models. I have tried updating both the rootNode and the models themselves - I am not quite sure how it works.

I’m not asking for a complete explanation, just a gentle nudge in the right way.



Here’s the code for picking.

It’s from the HelloCollision tutorial and was originally implemented by iterating over all the picked elements but it seemed the last item in the list was always the one I wanted.

[java]

CollisionResults results = new CollisionResults();

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

rootNode.collideWith(ray, results);

int i = results.size()-1;

// 4. Print the results.

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

Vector3f pt = results.getClosestCollision().getContactPoint();

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

System.out.println(" You shot " + hit

  • " at " + pt + “, " + dist + " wu away.”);

    [/java]



    And here’s the code for moving the models - seems fairly straight forward, but maybe I’m corrupting the bounds somehow.

    [java]

    Spatial s = inv.getChild(0);

    s.setLocalTranslation(v);

    [/java]

Are you using alpha-2? Then update to the latest nightly (see Help), there was a bug in the collision system.

Hi normen.



I just updated to the latest build (from alpha-2) and the problem persists.

Must’ve messed up some where else.

Thanks though.

[java]int i = results.size()-1;

// 4. Print the results.

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

Vector3f pt = results.getClosestCollision().getContactPoint();

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

System.out.println(" You shot " + hit

  • " at " + pt + “, " + dist + " wu away.”);[/java]

    Assuming you want to pick the thing you see, you want to have the closest collision of the Ray.

    However you pick the 1st collission in the list for everything but the contact point. I am not sure the collision list is ordered by distance.

Hi durandal.



I tried using getClosestCollision() but my models’ geometry object seems to be separated into more geometry objects which means that the names I gave to the spatial node doesn’t stay with all the geometries. So when I try to get the name out of the geometry of a given collision the name field is null.

The effect of all this is that I need the collisions with the name of the spatial node and not just the closest one. By trial and error I found that the last element of the list is always the element with a name field that isn’t null. I just assumed the list was sorted somehow, closest to farthest or vice versa because it is a list. I’d have used a set datastructure if I wanted an unordered set of elements.



Hope this explains my troubles.

Thanks for reading and trying though. There’s still hope :slight_smile:

Are you composing your own geometry from multiple smaller meshes (geometry batching)?

If so then I anticipated that problem too in question 8 (which Momoko_Fan answered). I havn’t tried what he suggested yet but I’m planning to in the coming days.



I’d be interested in your progress or perhaps solution, lonkz.

I created models in 3ds max and exported them as .obj which are then added to the jME assets and converted to .j3o.

I then use

[java]Spatial x = assetManager.loadModel(“Models/x.j3o”);[/java]

to load the model. This seems to automatically transform the model into several geometry objects. If only the name given to the original spatial was propegated to all the geometry instances the closest collision you mentioned would be the easiest, and altogether nicest, fix.

I’ll play around with the scenario some more and post a solution if I get it to work.

I’m still not ruling out that I may have failed miserably somewhere. So any hints would be appreciated.