[SOLVED] Is there any way to get the point's local position when using raycast?

Hello, I am using the raycast to get point where the mouse click down.

From the docs, I can get the entity and the point’s world position by RaycastResult.

However, I want to get the point’s local position of the entity, is there anyway to do this ?

Thanks.

If you want to convert a world position to a local position you have to first determine which Entity is going to be the ‘parent’, since a local position only makes sense relative to some parent. So assuming you have that parent entity you can then do:

    var mat = parent.getWorldTransform().invert(); // get inverse world transform of parent
    var worldPosition = new pc.Vec3(5, 0, 0); // example world position
    var localPosition = new pc.Vec3(); // example local position which will hold the result
    mat.transformPoint(worldPosition, localPosition); // transform world to local

So in summary you get the world transform of the parent, you invert it and then you transform a world position to a local position.

5 Likes

Thanks for your help, it works !

1 Like