如何根据三角函数以特定角度旋转 3D 后获得新坐标,就像codesandbox 中给出的示例一样

发布于 2025-01-10 08:21:47 字数 1235 浏览 1 评论 0原文

我需要你的帮助。

我了解旋转矩阵以及如何使用它在旋转一定角度后获取新坐标。但我无法了解在下面的代码中如何捕获新坐标以及立方体如何围绕假想圆旋转。

我正在使用 React Three Fiber - React 库来使用 ThreeJS。如果你不了解这个库,如果你擅长计算机图形学和数学,你仍然可以弄清楚它。

实例 - https://codesandbox.io/s/capra-christmas-forked-eukvyi

function Box() {
  const [ref, api] = useBox(() => ({ mass: 1, args: [1, 1, 1], position: [1, 1, 1], isKinematic: true }))
  useFrame((state) => {
    const t = state.clock.getElapsedTime() // get elapsed time within frame. if you new to react three fiber you can think it will get new increasing time after every sec. 
    api.position.set(Math.cos(2 * t ) * 10, Math.sin(20 ) * 5, Math.sin(20) + 1.5) // how this line of code specifying coordinates (x,y,z) in terms of trigonometry 
    api.rotation.set(Math.sin(t * 6), Math.cos(t * 6), Math.sin(t * 6))
  })
  return (
    <mesh ref={ref} castShadow receiveShadow position={[10, 0, 0]}>
      <boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
      <meshLambertMaterial attach="material" color="red" side={THREE.DoubleSide} />
    </mesh>
  )
}

I need your help.

I know about rotation matrix and how it is used to get the new coordinates after rotating in certain angle. But I am not able to get how in below code new coordinates are being captured and cube is revolving around the imaginary circle.

I am using react three fiber - react library for using ThreeJS. If you don't know about this library, still you can figure it out if you are good in computer graphics and mathematics of it.

Live example - https://codesandbox.io/s/capra-christmas-forked-eukvyi

function Box() {
  const [ref, api] = useBox(() => ({ mass: 1, args: [1, 1, 1], position: [1, 1, 1], isKinematic: true }))
  useFrame((state) => {
    const t = state.clock.getElapsedTime() // get elapsed time within frame. if you new to react three fiber you can think it will get new increasing time after every sec. 
    api.position.set(Math.cos(2 * t ) * 10, Math.sin(20 ) * 5, Math.sin(20) + 1.5) // how this line of code specifying coordinates (x,y,z) in terms of trigonometry 
    api.rotation.set(Math.sin(t * 6), Math.cos(t * 6), Math.sin(t * 6))
  })
  return (
    <mesh ref={ref} castShadow receiveShadow position={[10, 0, 0]}>
      <boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
      <meshLambertMaterial attach="material" color="red" side={THREE.DoubleSide} />
    </mesh>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2025-01-17 08:21:47

在您引用的代码沙箱中,他们使用圆的参数方程来围绕给定点旋转对象,这是一个很好的可视化效果。

https://www.mathopenref.com/coordparamcircle.html

这个想法是改变一个轴基于 cos(theta) 和另一个基于 sin(theta) *theta 在这种情况下是时间

api.position.set(Math.cos(2 * t) * 10, Math.sin(20 * t) * 5, Math.sin(20) + 1.5)

它看起来像在您的代码中您正在根据时间而不是您的 sin 函数更改 cos 函数。尝试使用上面的代码片段。另外,代码沙箱中的第三个轴仅用于使对象在旋转时上下移动一点。您可以查看上面链接中的数学知识,以更好地了解正在发生的情况。

in the code sandbox you referenced they are using the parametric equation of a circle to rotate an object around a given point, heres a nice visualization.

https://www.mathopenref.com/coordparamcircle.html

the idea is to change one axis based on cos(theta) and another based on sin(theta) *theta in this case being time

api.position.set(Math.cos(2 * t) * 10, Math.sin(20 * t) * 5, Math.sin(20) + 1.5)

it looks like in your code you are changing your cos function based on time but not your sin function. try using the snippet above. also the third axis in the code sandbox is just used to make the object move up and down a little as it rotates. You can look into the maths in the link above to get a better understanding of what is happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文