Godot-cpp - Get CapsuleShape3D from CollisionShape3D

Godot Version

v4.6.3.stable.official

Question

Hi guys, I’m trying to use Object::cast_to, to cast a CollisionShape3D to a CapsuleShape3D with the following code (though it doesn’t work obviously, because cast_to takes in an Object rather than Shape3D):

CapsuleShape3D* capsuleShape = Object::cast_to<CapsuleShape3D>(collisionShape->get_shape());

So essentially, it’s trying to cast Shape3D to CapsuleShape3D, which doesn’t seem right.

My question: is there a way to get the CapsuleShape3D from the CollisionShape3D? There’s no methods in CollisionShape3D that would suggest so.

My goal: to get the height property of CapsuleShape3D.

To add, if I try casting the collisionShape itself to CapsuleShape3D, it will throw an exception at runtime.

Example:

CapsuleShape3D* capsuleShape = Object::cast_to<CapsuleShape3D>(collisionShape);

Cheers!

Hi all, I prompted Claude (was reluctant to but couldn’t help myself) and it says to use .ptr() (which the code now works).

CapsuleShape3D* capsuleShape = Object::cast_to<CapsuleShape3D>(collisionShape->get_shape().ptr());

I will leave this post here as an FYI for anyone who comes across the same problem.