opengl中沿旋转方向移动

发布于 2024-10-17 02:01:05 字数 1043 浏览 4 评论 0原文

我对 OpenGL 确实很陌生,但对基本三角学有很好的掌握(自从上学以来就忘记了很多!),但我在这方面遇到了麻烦。

我有一个在 Z 轴上向前和向后移动的角色。为了向左和向右移动,而不是扫射,我希望它们旋转(分别按下左和右箭头键时),然后当它们再次向前/向后按下时,它们会朝它们所面对的方向移动。

所以我所做的就是让左/右函数在用于绘制角色旋转的角度变量上添加/减去少量内容。然后,前向/后向函数对 x 和 z 轴变量添加/减去少量内容。它们如下(向后):

z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading)

航向变量是由左右箭头键操纵的角度。

我认为这会起作用,因为当玩家直线前进时,航向为 0,因此 cos(0) = 1 和 sin(0) = 0,这意味着他们在 X 轴上没有移动,但在 Z 轴上前进了 0.005 的全部量。我想我的基本三角学知识并不完全可靠,因为如果我转动一点然后向前移动,它们就会朝那个方向移动,但如果我再移动一点然后向前移动,如果旋转 90 度,它们就会沿着同一条线移动,然后继续然后又好像它是180度然后是270度等等。

编辑:我会尝试更好地解释这一点,基本上如果我向左转后向前按,它会朝我想要的方向前进,但如果我放开向前,再转一点,然后再向前推,角度已经增加了,但方向却与应有的方向成90度。抱歉我无法很好地解释这一点。

编辑:好的,我遇到了一些奇怪的问题,我认为这些问题可能会导致奇怪的“90度”问题,当我让角色看起来90度(通过硬编码标题= 90)左/右,cos(标题)应该是0吧?但由于某种原因,它的结果是 -0.44,如果我 cos-1(-0.44) 我得到 116.1,这是否与 Math.cos() 想要弧度角度或其他什么有关?我完全迷失在这里了。

这是解决这个问题的正确方法吗?我完全陷入了用减号进行试验和错误的困境...

任何答复都表示赞赏,

谢谢

InfinitiFizz

(另外,我知道我应该使用 deltaTime 作为角色的速度/旋转值,而不是硬编码的 0.005f 但我想在解决这个问题之前先解决这个问题)。

I'm really new to OpenGL but have quite a good grasp of basic trigonometry (forgotten quite a bit since school!) but I'm having trouble with this.

I have a character that moves forwards and backwards on the Z axis. To move left and right, rather than strafing I want them to rotate (when pressing left and right arrow keys respectively) and then when they press forwards/backwards again they move in the direction they're facing.

So what I did was have the left/right functions add/subtract small amounts onto an angle variable that is used to draw the character's rotation. Then the forward/backward functions add/subtract small amounts to the x and z axes variables. They are as follows (for the backwards):

z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading)

The heading variable is the angle which is manipulated by the left and right arrow keys.

I thought this would work because when the player is going straight forward the heading is 0 and so cos(0) = 1 and sin(0) = 0 which means they move nowhere on the X but forwards the full 0.005 amount on the Z. I guess my rudimentary trigonometry knowledge wasn't completely sound because if I turn a bit then move forwards they go in that direction but if I move a bit more then move forwards they go in that same line if it was rotated 90 degrees, and continues then again as if it was 180 degrees then 270 degrees etc.

EDIT: I'll try and explain that better, basically if I press forwards after turning to the left it will go in the direction I want, but if I let go of forward, turn a bit more and then press forward again, the angle has increased as it should have but the direction is like 90 degrees around from the direction it should be going. Sorry I can't really explain this well.

EDIT: Okay, I'm getting some weird problems that I think might be causing the strange "90 degrees" problem, When I get the character to look 90 degrees (through hard-coding heading=90) left/right, cos(heading) should be 0 right? But for some reason it is coming out as -0.44, and If I cos-1(-0.44) I get 116.1, is it something to do with Math.cos() wanting the angle in radians or something? I'm completely lost here.

Is this the right way of going about this problem? I'm completely stuck just trial and erroring around with minus signs...

Any reply is appreciated,

Thanks

InfinitiFizz

(Also, I know I should use a deltaTime for the speed/rotation values of the character not a hard-coded 0.005f but I want to get this problem out of the way first before I sort that).

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

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

发布评论

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

评论(1

绝對不後悔。 2024-10-24 02:01:05

它应该是*而不是+

基于:

x = r cos (theta)
y = r sin (theta)

您会想要:

z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading);

如果这就是您所追求的?我必须承认,我并不完全理解你对实际发生情况的描述。

编辑:您可能想要使用大于 0.005 的“半径”,具体取决于您的坐标系的工作方式。

It should be * not +

based on:

x = r cos (theta)
y = r sin (theta)

you will want:

z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading);

If that's what you're after? I must admit, I didn't fully understand your description of what actually happens.

EDIT: you'll probably want to use a larger "radius" than 0.005, depending on how your coordinate system is working.

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