Problem with screenToWorld and raycast

I was creating a Rubik’s Cube simulation, where you can click on the subcubes to modify the corresponding faces. Here is the project
In the process, I need to determine which subcube the mouse is over, so that I can identify the faces that can be rotated.
To achieve this, I convert the mouse’s screen coordinates to world coordinates using the screenToWorld command of the camera component, and then perform a raycast with a depth range from nearClip to farClip.

const camera = this.cameraEntity.camera;
const fr = camera.screenToWorld(event.x, event.y, camera.nearClip);
const to = camera.screenToWorld(event.x, event.y, camera.farClip);
const result = this.app.systems.rigidbody.raycastFirst(fr, to);

cameraEntity is the camera entity passed as an attribute to the script.
The problem is that visually comparing the values, the world coordinates do not match the mouse coordinates in the camera plane, which is what I expect to see. This can be observed because the contact point with the subcube from the raycast does not correspond to the mouse cursor. As a result, in certain mouse positions, faces are rotated that do not correspond to the ones clicked.
What do you think could be the issue? It seems to be related to the resolution and the “Keep aspect ratio” setting in the rendering settings, but I’m not certain.

Hi @Emanuele_Raniello and welcome,

Thinking if the fill mode is the issue here, could you try and revert your project’s fill mode to Fill Window?

image

I’ve already tried it, it doesn’t solve the problem. Additionally, I’ve enabled “keep aspect ratio” to make the application usable on mobile devices. Thk for the reply.

Hi @Emanuele_Raniello !

It took me a little while and a bit of poking around, but I found the issue. You have your camera scaled a bit oddly, and it’s throwing off the calculations. Rescale the camera to 1, 1, 1 and it clears up the issue.

You can check it out in my fork here:

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

2 Likes

Perfect!

Hi @eproasim

Now it’s fixed. I don’t know why the function actually takes into account the camera scaling for the calculation, but it definitely led to that error. I wonder if this is intended behavior or an actual bug. Thank you so much for the help and dedication.