javascript - 如果发生碰撞,如何使用光线转换防止变换控制移动对象?

标签 javascript 3d three.js cannon.js

所以我正在使用 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/

相关文章:

javascript - Ionic Android 应用程序启动后出现黑屏

javascript - Atom 错误突出显示 - 禁用

c++ - 绑定(bind)到 OpenGL 3.x VBO

opengl - GLSL 4.x 子例程变量会导致任何性能开销吗?

javascript - Three.js - 来自 Math.Plane 的 PlaneGeometry

javascript - 推送时 NodeJS 数组结果为空

javascript使用delete还是依赖垃圾回收?

java - 3D 对象相遇处的不平整边缘和故障

javascript - 使用 STLLoader 进行 CSG 操作

javascript - Three.js 代码中的对象始终呈现在另一个对象之上