Correct direction for impulse (Physics/WebXR)

Hello!

I have a demo Babylon.js Playground

I don’t know how to take the direction of the controller so that the sphere always bounces in the right direction. I’ve tried different getDirections on the controller mesh, on the pointer mesh, but I’m not quite sure how these getDirections work. It would be cool to add the speed of throwing the hand. In fact, it would be cool to get a demo where you can throw this sphere in the direction of your hand, just as if I were throwing a ball in the real world.

Tagging our XR wizard @RaananW for when he comes back :smiley:

2 Likes

Howdy! Great question :slight_smile:

Every input source has this function - Babylon.js/webXRInputSource.ts at master · BabylonJS/Babylon.js (github.com), which gives you a ray with position and direction you can use anywhere you need.

Hope this helps!

2 Likes

Thanks for the answer!

My solve
I solved this problem in a slightly different way by taking forward from inputSource.pointer and scaling it.
Here is a demo https://playground.babylonjs.com/#7DTW9V#1

Rays

I know absolutely nothing about rays. Very little is written about them in the documentation. Could you explain how it works? I looked at the getWorldPointerRayToRef function and found that it returns nothing. It seemed to me that the function would just return the direction vector I needed.

Small demo with rays

If it’s not hard for you, could you demonstrate a demo that would work with getWorldPointerRayToRef?

First about this function - it returns nothing because it is a “toRef” function, meaning, the result is one of the parameters provided to the function (a reusable reference). Just like a lot of our vector functions.

Now, about rays - think about a ray as an object with a position and direction. it is a straight line that starts in the position (origin) and stretches in its direction. A ray also has a length parameter, which limits it’s “stretch”. So a ray with origin 0,0,0 and direction 0,1,0 with length of 100 will be a straight line from 0,0,0 to 0,100,0.
It is useful for example when you check mouse interaction with the scene (it uses rays). You have an origin (the mouse click position in 3d world space), you have a direction (“foreard” based on the camera’s direction), now you can send a ray from this point “forward”, and check which meshes intersect with it.

1 Like

Thanks for the detailed answer!

Thanks to your explanation, everything has become much clearer. I will try to play with the rays to better understand them.

1 Like