Closest Physics object

So, bit of a different question here, how would I go about to find the closest physical object to a certain physical object? Is there a method such as physicsobject1.getClosest(RigidBodyControl.class) or similar? That would be awesome! :slight_smile: Any help or similar method appreciated :wink:

I donā€™t know about a straight way to find that out.
There might be a more clever solution than this, but you could just loop through your objects, find the nearest (via the .distance() of their locations) and check whether it has RigidBodyControl (or whatever you use) or not.
Sorry if you thought of that already and search for a smarter approach thoughā€¦

@benkibitzer said: I don't know about a straight way to find that out. There might be a more clever solution than this, but you could just loop through your objects, find the nearest (via the .distance() of their locations) and check whether it has RigidBodyControl (or whatever you use) or not. Sorry if you thought of that already and search for a smarter approach though...

I actually thought about that, but I was thinking that such a loop may become heavy both to write (adding all objects to some sort of list) and to actually do the loop on if in Updateā€¦ I also have some objects in other classes, public and not, so it might just become a bit tricky to actually make it work! Last resort perhapsā€¦

adding all objects to some sort of list
No creation of a list needed is it? Your (root)node has that list already, just remember the one object you looped through and check if the next is nearer (and if so, remember that one). But yeah, you should wait for one of the pros to show up and share some thoughts :) .
@benkibitzer said: Your (root)node has that list already

Of course! I forgot that ā€¦

note that then youā€™ll compare only the distance between centers, not between ā€œsidesā€. And i think that there is no easy way to get the real distance, as objects can have very strange shapes. But if you need something closer to the ā€œsideā€ distance, you can just get the boundingvolume and calculate the distance between center of volumes and remove the extends of both volume from the result. Something like that should give you a nice enough result most of times.

Maybe you could do a few sweepTests in the direction(s) you are interested in.

http://hub.jmonkeyengine.org/javadoc/com/jme3/bullet/PhysicsSpace.html#sweepTest(com.jme3.bullet.collision.shapes.CollisionShape,%20com.jme3.math.Transform,%20com.jme3.math.Transform)

I actually seem to have nailed down the problemā€¦ I added physicslisteners and everything, and now the ghost control actually notices when it is colliding with the room and when it is not! However, it only really notices when it is outside the room, as it reckons that just being inside the shape of the room (itā€™s a RigidBodyControl btw) will count as a collision. If I ā€œleaveā€ the room with the ghostControl (by pushing it out of the wall) it realises it is not intersecting and does all appropriate code! Any ideas how this could be fixed, or if I should change the type of physics from RBC?

The model was done in blender btw, and the debug screen only shows blue lines etc where the walls actually areā€¦ not inside the room!