Find the coordinates

Hello. I try to find the coordinates of the edge of the screen, but the function does not work. Tell me why even console. log does not work.

block.prototype.initialize = function() {
    var from = this.cameraEntity.camera.screenToWorld(0, 0, this.cameraEntity.camera.farClip);
    var to = this.cameraEntity.camera.screenToWorld(0, 0, this.cameraEntity.camera.nearClip);
    this.app.systems.rigidbody.raycastFirst(from, to, function (result) {
                   console.log('test raycast', result)
                });

};

Hi @sergey_ch,

The raycastFirst() method will scan and find any intersection in the physics world.

Are there valid rigid bodies at your screen edge (top left corner of your monitor)?

I have palne, but it doesn’t scale. I need to determine the edges of the screen to position the objects correctly.

You can’t do that then with raycastFirst, if there is no body there.

You can use screenToWorld at a certain distance from the camera (the distance you would like to position the plane on) to receive a world point there:

var edge = this.cameraEntity.camera.screenToWorld(0, 0, 10);

Weird. This should be a fairly simple function. For example, the task is to place objects from the beginning of the screen. At a resolution of 320 and 1080, the coordinates will be different, is there no way to find the coordinates of the upper-left corner of the screen?

Yes there is, and it’s fairly simple, I’ve posted a single line above with the solution.

Bare in mind that you are trying to convert a 2D point to a 3D point in world space, you need to provide the right distance from the camera (3rd argument).

You were trying to raycast using physics, that will not work if you don’t have any rigid bodies at that point.

2 Likes

I understand, I posted a plane and made it a rigidbody. Next, I use camera. screenToWorld. It finds the coordinates, but they don’t change if I change the screen resolution. They are always the same. Can you take a look?

https://playcanvas.com/editor/scene/1116456

Your plane is missing a collision component, add one and size it properly, and then raycasts will work:

Also if you are using screenToWorld in your initialize method that may not work since it many need a delay for everything to render once first.

In the past I’ve used something like this:

window.setTimeout( function(){    
   var worldPos = cameraEntity.camera.screenToWorld(0,0, 10);        
}.bind(this), 0 );