2

this is my first post so I will try my best to explain. Every time I place an object on a rotating spheres surface, it gets offset the more the sphere rotates.

I tried offsetting it back to the mouse, but it didn't work. I'm at a loss for what to do.

Heres my code:

function placeObj(event) {
    if (event.button == 0) {
        switch(placeObj) {
            case null:
                Mouse.x = (event.offsetX / window.innerWidth) * 2 - 1;
                Mouse.y = -(event.offsetY / window.innerHeight) * 2 + 1;
                var Intersects = raycast(Mouse, [Moon]) // Returns the intersects

                if (Intersects.length > 0) {
                    let intersectSelect = Intersects[0];
                    let intersectObj = intersectSelect.object;
                    if (intersectObj.name == "Sphere") // Check if its a sphere {
                        var fbxLoader = new FBXLoader();
                        var cube = new THREE.Mesh(
                            new THREE.BoxGeometry(0.2, 0.2, 0.02),
                            new THREE.MeshBasicMaterial(),
                        )

                        Moon.add(cube);
                        cube.position.copy(intersectSelect.point);
                        cube.lookAt(Intersects[0].face.normal);
                    }
                }
                break;
            
            default:

                break;
        }
    }
}

Heres my animate function.

function animate() {
    Controls.update();
    requestAnimationFrame( animate );

    if (!stopRotation) {
        Moon.rotation.y += 0.0001;
    }

    renderer.render( Scene, camera );
}

Heres a GIF to demonstrate what happens.

GIF

M -
  • 26,908
  • 11
  • 49
  • 81
Rokborr
  • 33
  • 8
  • 2
    When performing an intersection, the `intersect.point` coordinates [are in world coordinates](https://threejs.org/docs/#api/en/core/Raycaster.intersectObject), not in the object's local coordinates. This means the `point` coordinates you're getting are assuming the moon's y-rotation is 0. You'd have to apply the y-rotation to `point` to get the local space coordinates. – M - Mar 03 '23 at 17:41

1 Answers1

1

Thanks to Marquizzo, I found the solution to my problem.

I needede to use WorldToLocal to get the local position instead of the world position.

cube.position.copy(Moon.worldToLocal(intersectSelect.point));
Rokborr
  • 33
  • 8