# [SOLVED] Calculate CapsuleCollisionShape Height

Hello Guys,
I’m participating in a small game jam and I’m trying to calculate the height parameter to use with the CapsuleCollisionShape. Here is the current code:

``````    CapsuleCollisionShape capsule = new CapsuleCollisionShape(radius, height);
CharacterControl charCtl = new CharacterControl(capsule, stepHeight);
``````

I’m trying to calculate the height parameter according to a given size of the model I would like to use as a character in my game.

So I have tried this:

``````float height = 0;
BoundingVolume bv = mySpatial.getWorldBound();
if(bv instanceof BoundingBox) {
BoundingBox bb = (BoundingBox)bv;
height = bb.getYExtent();
}
``````

And set the height variable as the CapsuleCollisionShape height in the constructor. But there is not always a match… If I scale / unscale the model then the Bounding Box’s yExtent cannot be used as height for the CapsuleCollisionShape .

It’s easy to manually tune the CapsuleCollisionShape height to fit my model but when I scale / unscale the model I want to be able to re-calculate the CapsuleCollisionShape height and I can’t find the correct formula.

what do you think? Anyone tried calculating CapsuleCollisionShape for a given model?
Thanks

Is your scaling non-uniform? Try only scaling the geometries and not nodes.

1 Like

If you scale the model by X, doesn’t that just mean you have to multiple the height by the same factor? (As the scaling is linear)

2 Likes

The “height” in the constructor and getHeight() method refers to the height of the cylindrical portion of the capsule before the scale factor is applied.

Perhaps you want to set the total height of the capsule, which would be

``````float totalHeight = capsule.getHeight() + 2 * capsule.getRadius();
``````

before scaling.

Edit: the Javadoc is fairly clear — CapsuleCollisionShape (MinieLibrary API)

2 Likes

`BoundingBox.getYExtent()` returns the extent, which is only half of the height. Try multiplying it by 2.

3 Likes

OK guys I have found the problem! Maybe it will help someone in the future using the CharacterControl & CapsuleCollisionShape.
The key is that you cannot just manipulate the capsule height. You must change the radius as well for example if you scale your model by 0.5 (half the size) , you need to multiply both the capsule height & radius by 0.5. My main issue was that I changed only the capsule’s height.
The second problem in my code was that I scaled the Geometry and not its parent node. I think it is best practice when dealing with scaling to always scale the parent node (if you have one). I fixed that as well but again the key point for fixing this issue was to change both capsule’s height & radius.

Thanks a lot for your suggestions

2 Likes