Page 1 of 1

How to make crouching. Change collision shape on the fly?

Posted: Mon Feb 07, 2011 4:12 am
by ill
I'm using a capsule as a collision shape for my game character.

I thought it would be pretty straight forward to just on the fly switch between two capsules which are different heights. I can't seem to find a function that allows to set a collision shape on the fly and can't find anything on Google.

Is there some way to change the height of a capsule without warping the rounded bottom edges with a scale transform?

Re: How to make crouching. Change collision shape on the fl

Posted: Mon Feb 07, 2011 7:02 pm
by sparkprime
Everything in my engine is a compound (even simple cubes) so I just move stuff around within the compound in order to change its shape. For removing parts entirely I have to reset the pairs.

world->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(body->getBroadphaseHandle(), world->getDispatcher());

You may just be able to change the parameters of your capsule and then perhaps reset various things that might depend on the shape.

You can always remove it from the collision world and put it back in again. This is not usually as intrusive as it sounds.

Re: How to make crouching. Change collision shape on the fl

Posted: Tue Feb 08, 2011 1:56 am
by winspear
Another cool idea would be to use two capsules joined together by a translation joint. Then when you want to crouch, you can just slide the upper capsule down by applying force. You can also get some good bounciness from your character during jumping when implemented this way.

Re: How to make crouching. Change collision shape on the fl

Posted: Tue Feb 08, 2011 3:37 am
by lulzfish
I would give this guy a quick try before anything complex:

http://bulletphysics.com/Bullet/BulletF ... e5051e1a5a

btCollisionObject is a superclass of btRigidBody, so this function is available to all btRigidBodies.

Re: How to make crouching. Change collision shape on the fl

Posted: Tue Feb 08, 2011 7:11 am
by sparkprime
winspear wrote:Another cool idea would be to use two capsules joined together by a translation joint. Then when you want to crouch, you can just slide the upper capsule down by applying force. You can also get some good bounciness from your character during jumping when implemented this way.
You would rather want to put them in a compound so that they are locked tightly together instead of being subject to the whims of the constraint solver.

Re: How to make crouching. Change collision shape on the fl

Posted: Tue Feb 08, 2011 3:09 pm
by Flix
Another approach I would try is a btMultiSphereShape with 2 spheres (i.e. equal to a btCapsuleShape). When crouching, the upper sphere can be moved down.

Re: How to make crouching. Change collision shape on the fly?

Posted: Tue Nov 06, 2018 11:06 pm
by avithohol
I know its old but in case somebody come across.

This works for me:

rigidBody->getCollisionShape()->setLocalScaling(btVector3(1,0.5,1));

This scale my body's capsule shape half sized on the Y axis.