Finding closest object to a position

User avatar
LarrxX
Posts: 5
Joined: Wed May 16, 2012 3:37 pm

Finding closest object to a position

Post by LarrxX »

Being a total n00b in Bullet, I seek the wisdom of more seasoned developers in here to help me figure this out without a lot of overhead.

I am looking to perform a "spherical raycast" i.e. give Bullet a position and a distance and have it send back a list of objects within the raycast sphere.

My initial thought was to run through all the bodies in my Bullet scene and test their distance. Then I saw there is a raytest() method in btDynamicsWorld and thought that maybe I could do multiple raycasts in different directions (but that would be way too costly I think).

So, any ideas?

Thanks.
user64
Posts: 14
Joined: Fri Jan 28, 2011 9:03 pm

Re: Finding closest object to a position

Post by user64 »

1) add collision object (btCollisionObject or derivative) with collision shape (deriv. from btCollisionShape) to the collision world (btCollisionWorld or deriv.);
2) create btSphereShape;
3) perform convex test (btCollisionWorld::convexSweepTest()) btSphereShape vs. all objects from btCollisionWorld;
4) PROFIT!
see:
Demos\CharacterDemo\CharacterDemo.cpp
User avatar
LarrxX
Posts: 5
Joined: Wed May 16, 2012 3:37 pm

Re: Finding closest object to a position

Post by LarrxX »

Cool thanks!

Quick question though: will this work if my "to" and "from" positions are the same? i.e. my collision sphere is static and does not move?

(sorry for the long delay in responding, but it seems the "subscribe topic" option is not working :( )
User avatar
LarrxX
Posts: 5
Joined: Wed May 16, 2012 3:37 pm

Re: Finding closest object to a position

Post by LarrxX »

I have a bonus question:

When I do a convex sweep, the callbacks available in Bullet all return btCollisionObject pointers (as far as I know).

Now, in my code, I have wrapper classes each containing its own copy of a Bullet object (mainly btRigidBody and BtSoftBody). How can I find the correct instance of my wrapper class contaiing the body returned by the convex sweep? Can I just simply compare the pointer adresses?

I found the set/get companionID methods for a btCollisionObject, can I use those to assign specific IDs to my objects or are they used internally by the collision algorithms (like it seems to be the case in btParallelConstraintSolver::solveGroup() ).

Thanks for any help/suggestions.
user64
Posts: 14
Joined: Fri Jan 28, 2011 9:03 pm

Re: Finding closest object to a position

Post by user64 »

will this work if my "to" and "from" positions are the same?
Check it!
Can I just simply compare the pointer adresses?
hmm..yeah. You can use btCollisionObject::m_userObjectPointer to get rid of search.