如何根据三角函数以特定角度旋转 3D 后获得新坐标,就像codesandbox 中给出的示例一样
我需要你的帮助。
我了解旋转矩阵以及如何使用它在旋转一定角度后获取新坐标。但我无法了解在下面的代码中如何捕获新坐标以及立方体如何围绕假想圆旋转。
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您引用的代码沙箱中,他们使用圆的参数方程来围绕给定点旋转对象,这是一个很好的可视化效果。
https://www.mathopenref.com/coordparamcircle.html
这个想法是改变一个轴基于 cos(theta) 和另一个基于 sin(theta) *theta 在这种情况下是时间
它看起来像在您的代码中您正在根据时间而不是您的 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
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.