所以我正在使用 Three.js,并且盒子里面有一些立方体。我使用变换控件用鼠标在框内移动立方体。我想使用光线转换来检查碰撞。问题是如果发生碰撞,如何防止变换 Controller 移动对象?如果它撞到墙上我想阻止它。顺便说一句,我使用的是 Three.js 的 r81 版本。
UPDATE: I've used the size of the room to constrain the cubes from moving outside of the room. This seems to work well. Is there a way to use the cannon.js just for collisions? I don't want the momentum or gravity or any other feature. JUST the collision check and to stop it dead in its tracks when there is a collision.
最佳答案
我知道这篇文章是很久以前的,但希望谷歌用户发现这有帮助。我无法阻止用户移动我的对象,但通过向渲染方法添加一些逻辑,我可以立即将其移回正确的位置。
对于原始发布者的碰撞问题,您可以将事件监听器附加到变换控件,并在对象处于非法状态时请求重新定位对象。
transformControls.addEventListener('objectChange', (e) => {
if (illegalPosition(this.obj.position)) {
needsReset = true;
}
lastPosition = attachedObject.position.clone();
});
然后在你的渲染函数中
if (needsReset) {
attachedObject.position.set(lastPosition.x, lastPosition.y, lastPosition.z);
}
如果这感觉有点老套,那是因为它确实如此。但对于我们这些没有时间或技能来阅读和修改 TransformControls.js 的人来说,我认为这可能会有所帮助。
关于javascript - 如果发生碰撞,如何使用光线转换防止变换控制移动对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967054/