PhysX Gem: runtime resizing of Collider components

Lumberyard 1.18 brings a new gem: PhysX functionality in preview mode. I’m here to share a code snippet to get something accomplished in it that isn’t available out of the box: dynamically resizing collision shapes. The new component for physical shapes is PhysX Collider.

physx_colliderIn the editor you can modify its size, for example sphere’s size. However, at runtime this functionality is not easily available.

Note: PhysX Collider is actually an editor-only component that is replaced with a real game component during slice compilation.

Either way, here is a runtime way to modify shape’s size from the same entity that has a PhysX collider component. Use it as your own risk!

void* nativePointer = nullptr;
using ColliderBus = PhysX::ColliderComponentRequestBus;
ColliderBus::EventResult(nativePointer, GetEntityId(), 
    &ColliderBus::Events::GetNativePointer);
if (nativePointer)
{
    if (physx::PxShape* nativePxShape = 
            static_cast<physx::PxShape*>(nativePointer))
    {
        const physx::PxSphereGeometry sphereGeometry{my_new_radius};
        nativePxShape->setGeometry( sphereGeometry );
    }
}

A few tips: GetNativePointer returns a pointer to a  real PhysX object. One can then modify the shape’s geometry with setGeometry. This works alright with static rigid bodies but you will have to see how that behaves with dynamic rigid bodies.

P.S. You can find more information on PhysX and Lumberyard in “Game Programming with Amazon Lumberyard” book.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s