放置物体在旋转的球体表面

huangapple go评论69阅读模式
英文:

Placing an object on the surface of a rotated Sphere

问题

这是我的第一篇帖子,所以我会尽力解释。每当我将物体放在旋转球体表面时,它会随着球体的旋转而偏移。

我尝试将它重新偏移到鼠标位置,但没有成功。我不知道该怎么办。

这是我的代码:

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]) // 返回交点

                if (Intersects.length > 0) {
                    let intersectSelect = Intersects[0];
                    let intersectObj = intersectSelect.object;
                    if (intersectObj.name == "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;
        }
    }
}

这是我的动画函数。

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

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

    renderer.render( Scene, camera );
}

这是一个GIF,演示了发生的情况。

放置物体在旋转的球体表面

英文:

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.

放置物体在旋转的球体表面

答案1

得分: 1

感谢Marquizzo,我找到了解决我的问题的方法。

我需要使用WorldToLocal来获取本地位置,而不是世界位置。

cube.position.copy(Moon.worldToLocal(intersectSelect.point));
英文:

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));

huangapple
  • 本文由 发表于 2023年3月4日 00:29:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定