WebXR Controllers Velocity Detection

Hello,

I’m trying to get my XR controllers (not using hand tracking but Oculus Quest 2 controllers) velocity from my scene. I’m making a drumkit and I want to be able to adapt the volume and attack of a sound depending on the velocity with which the drums are hit. I’m using Havok as a physics engine and I am also parenting meshes to my controllers (Motion type : Animated, Prestep : Teleport), to imitate drumsticks.

My issue is the following : I’ve tried different solutions but I can’t manage to get the velocity of my XR controllers at all. I have found these two topics about similar issues / subject :

The first one shows that I should get an impostor. I’m not sure if this approach is deprecated but it should work. Yet, I have to activate the physics feature manually from the features manager and once I do and try to getImpostorForController(), I only get a null object. I’ve replicated this issue (without the rest of the project which is too dense for a playground) here, maybe you can tell me what I’m doing wrong : Babylon.js Playground

The second approach seems a bit complicated for the straightforward implementation I’m looking for. I didn’t manage to make it work from my first attempt and I’d like to be sure there’s no way to use the internal accelerometer from the controllers instead of having to calculate the velocity myself. If this is the right way then I’ll manage to do it this way.

Thanks.

All of the WebXR implementations available for physics are still using the old physics engine architecture.
If you only need the velocity, it is provided to you by the WebXR specs already - WebXR Device API . You can access the controller from the Babylon classes directly, and get the linear and angular velocity. Note that WebXR is RHS and Babylon is LHS, so you will need, in this case, to flip the Z axis.

Otherwise, if you are using havok, maybe @Cedric can help with finding the velocity from the attached Havok objects.

1 Like

For linear velocity : Babylon.js/packages/dev/core/src/Physics/v2/physicsBody.ts at ca9b939753e24c807c9e42a71fe4cd5d87d739f7 · BabylonJS/Babylon.js · GitHub

And there is the equivalent for angular velocity

2 Likes

Thank you very much for your help.

Following your answer, I tried to use the XRPose as it was done in the second playground I linked but I realized the playground doesn’t work either in my navigator (yet I’m using Chrome).
If the WebXR API has changed it’s not directly a Babylon-related issue but the link you sent is quite recent so maybe I’m still getting something wrong.

I tried it in my project and I indeed managed to log xrFrame and xrPose at each iteration (passing my controller ray target space and the xr reference space as parameters), but I never get the linearVelocity values, they are always undefined.

I saw that the linear and angular velocity attributes might return null if the user agent cannot populate it but my controllers are well recognized inside the XRScene as I can to execute other actions with it, and I think a (0,0,0) Vector is different than null in the case it might be explained by the fact that my controller is considered not to be moving.

I simplified the experimentation in the same playground as before (I’m logging each xrframe and xrpose but never happen to log the velocity values):

I’ll keep working on it but if you happen to know what could explain this behavior…

Thank you again

I’m only having trouble for the XR controllers velocity but thank you this link will definitely be useful in the future

1 Like

Question related to this thread - will havok update the linear velocity of moving objects in the scene? This is a perfect example - the controllers are moved by the user, so their position is updated on each frame. Does havok take this into consideration?

If this was a way to get it it would be perfect but I didn’t find a physicsBody attached to the XR controllers. I activated the physics feature from the features manager at first but then I was unable to get the impostor related for reasons I still don’t really understand.
Also, if I parent an object to my controller’s grip in Animated motion type and with a teleport prestep, I can’t get the velocity directly from this object (I suppose because of the teleport prestep but Cedric will obviously know better).

Any of these solutions seems like a suitable one but none work for me to this day.
Indeed, if there is a way to get it from havok directly, it would probably solve my problem.

Physics body linear velocity is not updated by setting its position with Animated mode:

Computing a velocity outside of the Physics engine is not very difficult. It’s delta position divided by delta frame time.