Screen xy to world coordinates

Hi,
apologize if looking in the forum I have already missed the answer to the question I’m about to ask

Someone can give me a concrete example of how to use Vector3 Unproject, to convert screen xy coordinates into world coordinates?

thank you in advance for any help!

Hey and welcome back!

here is how to use it:

        var vector= BABYLON.Vector3.Unproject(
                        mousePosition,
            	        engine.getRenderWidth(),
                        engine.getRenderHeight(),
                        BABYLON.Matrix.Identity(), scene.getViewMatrix(),
                        scene.getProjectionMatrix());
6 Likes

Thank you @Deltakosh!: )

the scenario is to set the position of a mesh.
Before I used the pickedPoint with this.scene.pick(this.scene.pointerX, this.scene.pointerY)

Does using Vector.Unproject way give the same results?

yep :slight_smile: mousePosition is your pointerX/Y thingy

2 Likes

Hi and thanks for a great product.

I would like to add - make sure your (input) mousePosition.z is set to 1 to get the expected results

I have been lurking for a while - writing a racing game … about 8 weeks in - thought I would try and make a positive contribution

5 Likes

Hey and welcome!!
Please share your game when you want :slight_smile:

Will do … although it is nowhere near as far along as the Need for Speed demo…

It’s a top down, racer with custom physics (mass spring system)… I am focussing on high frame rates and smooth gameplay, with analogue input (gyro, accelerometers, mouse) for a nice driving feel… cross platform obviously. I have one commercial game under my belt written with Direct X (QuickSnooker.com) but it is hamstrung by being PC only in an increasingly mobile world.

Lots to like about Babylon, have hit a few performance issues but nothing insurmountable, subdividing meshes should help, but that has broken picking at the moment, so I need to work out how to find the submesh from the subdivided mesh with ray picking … will ask more clearly if I get really stuck.

Very impressed by your dedication the the cause, I hope I can build something that does it justice.

Nick

Feel free! We are here to help

Hello everyone,

I am (I think) in the exact same issue. I need to add a sphere where the user clicks. I tried the following option :

var ballPos = BABYLON.Vector3.Unproject(
new BABYLON.Vector3(scene.pointerX,scene.pointerY,1),
engine.getRenderWidth(),
engine.getRenderHeight(),
BABYLON.Matrix.Identity(), scene.getViewMatrix(),
scene.getProjectionMatrix());

But the coordinates are really weird (like thousands or so)…

I thought about an option where I link a transparent plane end get the picked position but it’s seems a bit goofy.

Any idea ?

4 Likes

OK I think I have found the solution here : Scene.pointer to Vector3

3 Likes

Having same trouble here. Any help appreciated.

1 Like

is this one not working Scene.pointer to Vector3 - #2 by Cedric ?

Hi @trsh do you still have trouble?

The size of the result is related to the z-value of the source.Using a value smaller than 1 will get better results, or just normalize the result, then scaling to the size you want:

result.copyFrom(Vector3.Unproject(new Vector3(0,h*0.5,0),w,h,Matrix.Identity(),this._camera!.getViewMatrix(),this._camera!.getProjectionMatrix()));
const inv=this._camera!.getWorldMatrix().clone();
Vector3.TransformCoordinatesToRef(stw,inv.invert(),result);
result.normalize().scaleInPlace(10);
root.position.x=result.x;
root.position.y=result.y;
root.position.z=result.z;