英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论