screenToWorld and FOV? šŸ¤”

Iā€™m using screenToWorld to get coordinates in world space and set plane entity position according to calculations, the y is always 0, I noticed near corners offset starts to appear, any ideas how to avoid that?

 var depth = this.entity.getPosition().y;
    var cameraEntity = this.entity;
    cameraEntity.camera.screenToWorld(event.x, event.y, depth, this.posas);
    this.app.root.findByName("Box").setPosition(this.posas.x,0,this.posas.z);

The screen to world expects a distance from camera to the point in world space, while you are providing a distance from world origin to the object.

If you want the object to move along a plane, then you can switch camera to orthographic projection.

In case of perspective, I usually create pc.Plane to represent a surface that the object would move on, and then create a pc.Ray using camera screen to world. Plane has a ray intersection test, which can also give a point in world space. I then use that point to position the object.

1 Like