Physx raycast in lua

Is this a feature yet?

I know it isn’t documented, but I’ve found other nondocumented behaviorcontext bound classes that I’ve been able to use in Lua, so thought I’d ask.

I haven’t found anything for physx raycasts but I’m also not super familiar with lumberyard code and also not super strong in C++.

Anyone know?

Line 235-237 in Gems/LmbrCentral/Code/Source/Physics/PhysicsSystemComponent.cpp

        ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
        ->Event("RayCast", &PhysicsSystemRequestBus::Events::RayCast)

All PhysycsSystemRequestBus events are excluded from list, document or preview but still you can call from Lua like below.

    local rayConfig = RayCastConfiguration()
rayConfig.origin = Vector3(0,0,33)
rayConfig.direction = Vector3(0,1,0)
rayConfig.maxDistance = 1000.0
rayConfig.maxHits = 1

-- return RayCastResult
local hit = PhysicsSystemRequestBus.Broadcast.RayCast(rayConfig)

Debug.Log("hit:"..tostring(hit:GetHitCount()) )

Thank you for the reply.

Does that work with PhysX? I thought that only worked for entities with legacy physics components. I haven’t been able to make that work with PhysX but maybe I’m doing something wrong.

In script canvas the RayCastWorldSpace does what I’m looking for under PhysX>World. And I believe the function is here but I’m not sure if there’s any class reflected that would allow me to instantiate an object to use that in Lua.

Oh, your right, that will call legacy physics.

The RayCastWorldSpace node seems just a wrapper of Physics::World::RayCast function and none of components reflected that RayCast function in PhysX gem.

It seems WorldRequestBus is reflected in the Utils.cpp but only Get/SetGravity is reflected.
Thus, maybe the easiest way is create your own gem in your project, then implement the wrapper function of RayCast then reflect it to the Lua.


Ah, okay. I was hoping I was just missing it. I’ll try creating my own gem for it.

Thank you for confirming!

It works! ( my first gem :slight_smile: )

Essentially just copied the script canvas code, but with the returned results being of a reflected struct rather than a tuple.

Thanks, @Soeda, for pointing me in the right direction!

1 Like