Mesh as collision detector but not physical object

pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Mesh as collision detector but not physical object

Post by pwr »

Hi!
(I hope this is the correct place to post this question)
I'm new to using physics engine. I feel a bit lost when trying to googling answers. So I really apologize if my question seems to be really stupid and easy to find answers to by simply googling them. It is because I don't know the terms to use.

This is what I want to do:
My is built up of chunks of blocks, like in minecraft. I want to add simple physics to my water. I have read about bouancy and I don't think that my game need that advanced simulation. So I don't think that have any use of the btHtFluid demo in the Bullet source.

This is what I'm thinking of doing:
Use a mesh that defines the water body and use collision detection to find out which objects are located in the water. If an object is located in the object I apply a force on the object.

This is what I don't know:
How do I create the water body mesh that I can use for collision detection. I can create bodies that hard hard that bounce when they collide. But the water body should only be used to signal that a body are inside so I can apply an impulse. In other words I don't want the water mesh to work as a real physical object only a search space to detect collision with mobile objects that should float.

I really hope someone understand what I want to do. :)

BR
pwr
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am

Re: Mesh as collision detector but not physical object

Post by marios »

Yep, You can do that easily just add this line while creating your body. Then it will not collide but it will be still generating contact points, that you want to use to check which body is inside water.

Code: Select all

body->setCollisionFlags(body->getCollisionFlags()| btCollisionObject::CF_NO_CONTACT_RESPONSE));
pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Re: Mesh as collision detector but not physical object

Post by pwr »

Thank you Marios for your reply. I am still a bit puzzled. I have looked at the CollisionInterfaceDemo that comes with the bullet source. This demo uses btCollisionWorld::contactTest() to test all objects in the world for collisions. Is that the correct way to find the objects that are colliding with the water? Or can I register a callback for the specific collisionobjects that I need to add specific behaviour for?
I'm worried that if I have many water bodies I will have to iterate threw a lot of objects in order to test for collisions and doesn't Bullet perform collision test on the objects in the world already. Would be good to be able to use that information instead of calling a separate collision test.
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am

Re: Mesh as collision detector but not physical object

Post by marios »

Of course there there are many ways to achieve that. Personally, in my project for forcefields (also adding force when something is inside, like in your water) I'm using first method, so adding body to world without collision response. What about performance, I'm not sure which method is faster, I think that contact tests are slower, but these are only my speculations. Generally, both methods are good and it shouldn't be much difference
pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Re: Mesh as collision detector but not physical object

Post by pwr »

Ok, sounds good. So you create the collisionobject and use btCollisionObject::CF_NO_CONTACT_RESPONSE. But how do you know if two bodies have collided?
If I understand you correctly, you don't use btCollisionWorld::contactTest()?
TheKing
Posts: 13
Joined: Sun Sep 11, 2011 2:02 pm

Re: Mesh as collision detector but not physical object

Post by TheKing »

But how do you know if two bodies have collided?
http://bulletphysics.org/mediawiki-1.5. ... d_Triggers
This is what you are looking for. The following article migth also interest you: http://bulletphysics.org/mediawiki-1.5. ... _Filtering
pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Re: Mesh as collision detector but not physical object

Post by pwr »

Thanks! Those articles were informative. Unfortunately I can't get the collisions to work the way I want.

I've tried to create a collision shape and adding it to a ghostobject. Setting the collision group and collisionflags to the all and default. The result was that I could go inside the box, as I wanted, but bullet didn't find any collisions when I tried the bullet example in the collision article in the wiki.

I've also tried to create a rigidbody and set it to CF_NO_CONTACT_RESPONSE. The result was that Bullet registered collisions in the collisions code from the collision article. But the rigidbody didn't let me get inside the box.

It seems like I only get collisions when I actually touch a polygon which is not what I want. I want to register when I'm inside a volume of space.

I hope somebody understand what I'm describing. There is a bit much code to post.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Mesh as collision detector but not physical object

Post by dphil »

pwr wrote: It seems like I only get collisions when I actually touch a polygon which is not what I want. I want to register when I'm inside a volume of space.
Yes, unfortunately, for meshes collisions are only registered when touching the surface. Objects fully contained inside a mesh - even if it is closed - do not register collisions. This is probably because of the difficulty/complexity of determining if objects are "inside" arbitrary concave shapes, and also because meshes may not be closed structures and may not be intended to represent solid objects. So, there are a couple ways around this:

1) Keep your volumes as simple primitive shapes (sphere, cube, etc)
2) Manually approximate your volume with a compound collision shape comprised of convex primitives
3) Use a convex decomposition algorithm to create a compound of primitives in an automated (and fairly accurate, depending on the decomposition parameters) way. For this, you could check out the HACD (hierarchical approximate convex decomposition) library recently added to Bullet. Though keep in mind that if you do a highly detailed decomposition, you'll likely still end up with a "hollow" centre in the volume. But in the simplest case, you can just wrap the whole volume in a single convex hull that will detect collisions anywhere inside it.
pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Re: Mesh as collision detector but not physical object

Post by pwr »

Ok, maybe I have misused the term collision? I actually want to register when an object is inside another body, complete or partial. marios wrote an answer above that he i simulating forcefields and it sounds like he is doing what I want to do. So maybe is the approach desribed in http://bulletphysics.org/mediawiki-1.5. ... d_Triggers not what I want.

It should be possible to do this in Bullet. Shouldn't be that hard to do it myself. But want to solve all the "collision" related functionality with help of Bullet and also think that this task is a great way to learn how to work with Bullet.
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: Mesh as collision detector but not physical object

Post by Dr.Shepherd »

dphil wrote:
pwr wrote: It seems like I only get collisions when I actually touch a polygon which is not what I want. I want to register when I'm inside a volume of space.
Yes, unfortunately, for meshes collisions are only registered when touching the surface. Objects fully contained inside a mesh - even if it is closed - do not register collisions. This is probably because of the difficulty/complexity of determining if objects are "inside" arbitrary concave shapes, and also because meshes may not be closed structures and may not be intended to represent solid objects. So, there are a couple ways around this:
How about creating a GhostObject around the water? In this case, you may use a box to represent the water body, so that the upper face is the water surface. Then you detect any collision that happens to this "water".

Correct me if I am wrong.
pwr
Posts: 12
Joined: Sat Sep 24, 2011 4:58 pm

Re: Mesh as collision detector but not physical object

Post by pwr »

I've created a ghostobject and tried that but didn't have any success. This is could be caused by me doing something wrong. So I was trying the other solution described above. I'm thinking that it would be more easier to simply check if an body is inside the water body. Then I don't have to care if the contact points is from the object traveling into the water or out from the water. Is this doable with a GhostObject? Do anyone have any sample code that I can use?
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am

Re: Mesh as collision detector but not physical object

Post by marios »

pwr wrote: marios wrote an answer above that he i simulating forcefields and it sounds like he is doing what I want to do
hi there,
Of course I forgot to mention that my collision object I use with CF_NO_CONTACT_RESPONSE flag to determine bodies inside forcefield, is convex object. With meshes it may not work because they don't know which side is inside/outside. So try play with convex objects
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Mesh as collision detector but not physical object

Post by dphil »

pwr wrote:I'm thinking that it would be more easier to simply check if an body is inside the water body. ... Is this doable with a GhostObject?
You can find out collision with both rigid bodies and ghost objects. It's more a matter of the type of collision shape used (for the rigid or ghost body), since as marios and I have pointed out, convex shapes can detect objects completely inside them, while concave shapes (like meshes) can only detect objects touching their surface (mesh triangles).
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: Mesh as collision detector but not physical object

Post by Dr.Shepherd »

OK, I think I learned from this topic.

btCollisionObject
-------------btRigidBody
-------------btSoftBody
-------------btGhostObject

btCollisonShape
-------------btConveShape
-------------btConcaveShape
-------------btCompoundShape

So just as dphl said, it is more matter of the CollisionShape selection, rather than the CollisionObject selection.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Mesh as collision detector but not physical object

Post by dphil »

Yes that is right Dr Shepherd. Those are the 2 primary class hierarchies. Collision objects more or less define how the objects respond to collisions, and collision shapes defines the spatial region/shape for which the object actually registers collisions with other objects. Any type of btCollisionShape can be applied to a rigid body or ghost object. I don't think collisions shapes are used with soft bodies, which I think are just created from mesh data (or by manually building up nodes and links between nodes).