在透视中移动 3D 对象时如何考虑屏幕尺寸 [关闭]

问题描述 投票:0回答:1

如何通过透视移动我的 3D 立方体?

我已经有了光线投射和检查设置...我什至可以移动我的立方体,但对我来说问题是立方体移动得太快。我的屏幕是 800x800,所以我的增量高于世界比例(增量 = 鼠标移动该帧)。

float sensitivity = 0.1f;

float moveX = f3DEditFrame.window.mouse.deltaX * sensitivity;
float moveY = f3DEditFrame.window.mouse.deltaY * sensitivity;

Vec3 mouseMoveInWorld = {
        moveX * camera.right.x + moveY * camera.up.x,
        moveX * camera.right.y + moveY * camera.up.y,
        moveX * camera.right.z + moveY * camera.up.z

};

cube.aabb.position.x += mouseMoveInWorld.x;
cube.aabb.position.y -= mouseMoveInWorld.y;
cube.aabb.position.z += mouseMoveInWorld.z;

关于如何正确计算运动有什么建议吗?

另外如何在堆栈溢出上添加语法突出显示?

c game-development
1个回答
0
投票

您必须考虑相机相对于立方体的位置。与距离成反比缩放:相机与物体之间的距离越小,物体就越大:

Vec3 cubeToCamera = cube.aabb.position - camera.position;
float depthScaling = 1.0f / glm::length(cubeToCamera); // Scale inversely with distance

// Scale the mouse movement by the depth
Vec3 mouseMoveInWorld = {
    moveX * camera.right.x * depthScaling + moveY * camera.up.x * depthScaling,
    moveX * camera.right.y * depthScaling + moveY * camera.up.y * depthScaling,
    moveX * camera.right.z * depthScaling + moveY * camera.up.z * depthScaling
};

语法高亮另请参阅这篇文章: https://stackoverflow.com/editing-help#syntax-highlighting

```lang-c
int main(void) {
    return 0;
}
```
© www.soinside.com 2019 - 2024. All rights reserved.