Need help with bullet contact triggers

Post Reply
Boltzmann
Posts: 9
Joined: Wed Mar 07, 2012 6:07 pm

Need help with bullet contact triggers

Post by Boltzmann »

Hello there,

I am currently trying to develop a Physics Manager for a Games Engine in C++ and using OpenGL for rendering. This manager provides the creation of collidable objects that remain within the engine and who are deleted as the manager ceases to exist, for purposes of having no memory leaks. The game can basically ask the Physics Manager to create objects and add them to the world, but can also add forces to them. The Render Manager will ask the Physics Manager for transformation matrices for each object that it needs to render. Each object in the Physics Manager will have a wrapping class CollidableObject to provide abstraction from Bullet's classes and code.

The game needs to know when two objects collide in order to play sounds. So I need my collisions to send a message to the Event Handler, which will tell the game about the message. However, I need 2 types of CollidableObjects: those who trigger a sound and continue with their normal Bullet response and those who trigger a sound but don't have a physics response, that is, they go through each other. (Two good examples would be a car crashing with another and a powerup being picked, respectively)

Another thing: I'm using Bullet 2.7 because I couldnt build the latest one for PSP.

1- So what is the best way to find collisions and trigger a message to somewhere else,
2- how would I make some rigid bodies not collide with eachother but trigger a contact message
3- and how would I have that triggered message informing that CollidableObject A collided with CollidableObject B instead of btCollisionObject A collided with btCollisionObject B?

Thanks in advance!
Boltzmann
Posts: 9
Joined: Wed Mar 07, 2012 6:07 pm

Re: Need help with bullet contact triggers

Post by Boltzmann »

Anyone? I really need help on this one, I've already done all I need from the Physics Manager except for this Contact Event system.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm
Contact:

Re: Need help with bullet contact triggers

Post by dphil »

Boltzmann wrote: 1- So what is the best way to find collisions and trigger a message to somewhere else,
2- how would I make some rigid bodies not collide with eachother but trigger a contact message
3- and how would I have that triggered message informing that CollidableObject A collided with CollidableObject B instead of btCollisionObject A collided with btCollisionObject B?
1 - There is a relevant tutorial code article in the wiki: http://bulletphysics.org/mediawiki-1.5. ... d_Triggers, and I know there has also been a lot of forum posts/discussion about ways of using contact triggers/callbacks and alternatives to the wiki methods (offhand, I'd suggest searching some combo of "contact trigger callback"). This wiki article http://bulletphysics.org/mediawiki-1.5. ... _Filtering also has a little bit of relevant info (about custom collision filtering and sounds)
2 - btCollisionObject::setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);
3 - Strictly speaking two objects simply collide by having overlapping volumes, and are both equally involved in the collision. I'm guessing what you probably mean/want is which object had a higher speed at the time of collision(?). This can just be retrieved as the colliding object with the greater magnitude of linear velocity.
Boltzmann
Posts: 9
Joined: Wed Mar 07, 2012 6:07 pm

Re: Need help with bullet contact triggers

Post by Boltzmann »

dphil wrote:
Boltzmann wrote: 1- So what is the best way to find collisions and trigger a message to somewhere else,
2- how would I make some rigid bodies not collide with eachother but trigger a contact message
3- and how would I have that triggered message informing that CollidableObject A collided with CollidableObject B instead of btCollisionObject A collided with btCollisionObject B?
1 - There is a relevant tutorial code article in the wiki: http://bulletphysics.org/mediawiki-1.5. ... d_Triggers, and I know there has also been a lot of forum posts/discussion about ways of using contact triggers/callbacks and alternatives to the wiki methods (offhand, I'd suggest searching some combo of "contact trigger callback"). This wiki article http://bulletphysics.org/mediawiki-1.5. ... _Filtering also has a little bit of relevant info (about custom collision filtering and sounds)
2 - btCollisionObject::setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);
3 - Strictly speaking two objects simply collide by having overlapping volumes, and are both equally involved in the collision. I'm guessing what you probably mean/want is which object had a higher speed at the time of collision(?). This can just be retrieved as the colliding object with the greater magnitude of linear velocity.
Thanks a lot for the info, I'll learn more about collision callbacks and triggers so that I can post more technical problems if I have them.

About point 3 I should explain thoroughly: Basically I'm wrapping btRigidBodies nad btCollisionShapes inside my own PhysicsObj class. I want to provide abstraction from bullet. How would I use the collision callbacks and triggers to issue a message saying which PhysicsObj instances A and B are colliding. I know the callbacks probably give information about two btCollisionObject instances A and B that have collided, but I want to link those back to their PhysicsObj instances and I am confused on how this would work.
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am
Contact:

Re: Need help with bullet contact triggers

Post by marios »

this can be done by setting user pointer while creating your bullet collision object like

Code: Select all

collisionObject->setUserPointer( pointer_your_abstract_object);
after that you can get it by, of course you must cast to your type from void*

Code: Select all

((Your_PhysicsObj_type*)collisionObject->getUserPointer())
Boltzmann
Posts: 9
Joined: Wed Mar 07, 2012 6:07 pm

Re: Need help with bullet contact triggers

Post by Boltzmann »

marios wrote:this can be done by setting user pointer while creating your bullet collision object like

Code: Select all

collisionObject->setUserPointer( pointer_your_abstract_object);
after that you can get it by, of course you must cast to your type from void*

Code: Select all

((Your_PhysicsObj_type*)collisionObject->getUserPointer())
This was very helpful!!!

EDIT: I'm having a problem now

I've implemented a custom_collision_callback function in my physics manager and then had:

Code: Select all

extern ContactAddedCallback gContactAddedCallback;
...
void PhysicsManager::init()
{
   gContactAddedCallback = custom_collision_callback;
first of all: must that function be static? If so, then I have a problem since it checks a hash_map of game entities that have registered CollisionListeners, which is an interface to be inherited by game objects that want to get collision events. Basically, they need to implement a react_to(Entity* e) function. The problem is that by making custom_collision_callback static, the hash_map must also be static since its being accessed from within the custom_collision_callback function and then this error appears when building:

Code: Select all

PhysicsManager.obj : error LNK2001: unresolved external symbol "private: static class stdext::hash_map<class Entity *,class TEXEngine::Physics::CollisionListener *,class stdext::hash_compare<class Entity *,struct std::less<class Entity *> >,class std::allocator<struct std::pair<class Entity * const,class TEXEngine::Physics::CollisionListener *> > > TEXEngine::Core::PhysicsManager::_listeners"
Note: The PhysicsManager is a singleton, which means it's already static, so wouldnt it be possible to have gContactAddedCallback = PhysicsManager::get_instance()->custom_collision_callback; if custom_collision_callback is a non-static member function of PhysicsManager?

EDIT2:
This code:

Code: Select all

static bool CustomMaterialCombinerCallback(btManifoldPoint& cp,	
	const btCollisionObject* colObj0,int partId0,int index0,
	const btCollisionObject* colObj1,int partId1,int index1)
{
	return false;
}

extern ContactAddedCallback gContactAddedCallback;

void PhysicsManager::init()
{
	gContactAddedCallback = CustomMaterialCombinerCallback;
Gives me this error:

Code: Select all

unresolved external symbol "bool (__cdecl* TEXEngine::Core::gContactAddedCallback)(class btManifoldPoint &,class btCollisionObject const *,int,int,class btCollisionObject const *,int,int)"
This is exactly like the demo...
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Need help with bullet contact triggers

Post by Flix »

Well, no Bullet related questions here, just plain C++... Anyway, as far as I can understand (not much, to be honest :oops: )
Boltzmann wrote:first of all: must that function be static?
Of course it can be a static member or a global method, but you can "wrap it": just declare another non-static member function with the same arguments, then declare a global static instance of your class properly and in the static callback call the non-static method through the static instance you have created, passing it the same arguments.
Boltzmann wrote:unresolved external symbol "bool (__cdecl* TEXEngine::Core::gContactAddedCallback)(class btManifoldPoint &,class btCollisionObject const *,int,int,class btCollisionObject const *,int,int)"
Not sure here... you may have referenced a TEXEngine::Core::gContactAddedCallback somewhere. You probably can use ::gContactAddedCallback to enforce the fact that the global method is in the global namespace (or maybe you need to specify the namespace (and/or class) in which you created the CustomMaterialCombinerCallback...)
Antonz
Posts: 16
Joined: Wed Nov 17, 2010 10:57 am

Re: Need help with bullet contact triggers

Post by Antonz »

Just my 5 cents...
Here's how I'm wrapping contact callbacks : http://www.bulletphysics.org/Bullet/php ... 489#p27489
I just don't like Singletons and try to avoid them if possible. Simple ones are problematic, and complex ones (like based on Loki Singleton) are also have their problems. So, I'm storing pointer to my physics engine in every my physics objects wrapper (not a big deal) and call it after taking pointer to this object wrapper via getUserPointer().
aviator
Posts: 13
Joined: Thu Apr 02, 2015 5:15 pm

Re: Need help with bullet contact triggers

Post by aviator »

Hopefully this post will be useful.

Code: Select all

unresolved external symbol "bool (__cdecl* TEXEngine::Core::gContactAddedCallback)(class btManifoldPoint &,class btCollisionObject const *,int,int,class btCollisionObject const *,int,int)"
This is exactly like the demo...
This error could be caused by quite simple mistake:

Code: Select all

namespace YOURENGINENAMESPACE
{
extern ContactAddedCallback		gContactAddedCallback;
//methods
}
The correct way would be to do this way:

Code: Select all


extern ContactAddedCallback		gContactAddedCallback;

namespace YOURENGINENAMESPACE
{
//methods
}
Maybe it won't help for everybody, but this is exactly what happened with my physics class and it had exactly the same unresolved external error.
I just don't like Singletons and try to avoid them if possible. Simple ones are problematic, and complex ones (like based on Loki Singleton) are also have their problems.
Well, that's not completely right, Singleton's are quite useful and quite simple, it all depends on class purpose.

Code: Select all

class OnlyOne
{
public:
        static const OnlyOne& Instance()
        {
                static OnlyOne theSingleInstance;
                return theSingleInstance;
        }
private:        
        OnlyOne(){}
        OnlyOne(const OnlyOne& root);
        OnlyOne& operator=(const OnlyOne&);
};
Post Reply