Opengl让“AI”桨上下移动(Opengl make “AI” paddle move up and down)

编程入门 行业动态 更新时间:2024-10-24 08:30:53
Opengl让“AI”桨上下移动(Opengl make “AI” paddle move up and down)

我有点像一个openGL /编程noobie所以我试图为正确的桨做一个“AI”。 我知道这不是正确的做法,我应该做的是让它跟随球。 但是现在我只是想让它不断上下移动。 我无法弄清楚如何做到这一点,尝试使用If循环之类的

if(paddle.pos [1]> 1){paddle.pos [1] = paddle.pos [1] - delta}

我将delta设置为0.01之类,1是屏幕的顶部。 显然这不对,因为一旦它低于1它再次上升,但我正在尝试做类似的事情。

第二个问题 - 当球开始时你如何将球从0,0移开? 有点同样的问题,我使用带有x值的if语句,但这绝对不对。

这顺便使用C.

I'm a bit of an openGL/programming noobie so I'm trying to make an "AI" for the right paddle. I know this isnt the proper way of doing it, what I SHOULD be doing is making it follow the ball. But right now I'm just trying to make it perpetually move up and down. I can't figure out how to do it, trying to use If loops like

if (paddle.pos[1] > 1){ paddle.pos[1] = paddle.pos[1] - delta}

I set delta to something like 0.01, 1 is the top of the screen. Obviously this isnt right because as soon as it goes down below 1 it goes up again, but I'm trying to do something like it.

2nd question - How do you move the ball from 0,0 when it starts? Kind of the same problem, am using if statements with the x values but thats definitely not right.

This is using C by the way.

最满意答案

尝试这样的方法使pos重复从0到1再回到0:

// Initialize. float pos = 0.0f; float delta = 0.01f; // On every update. pos += delta; if (pos > 1.0f) { pos = 1.0f; delta = -delta; } else if (pos < 0.0f) { pos = 0.0f; delta = -delta; }

这里的关键是每次到达其中一个结束位置时都会反转增量的符号。

Try something like this to make pos repeatedly go from 0 to 1 and back to 0:

// Initialize. float pos = 0.0f; float delta = 0.01f; // On every update. pos += delta; if (pos > 1.0f) { pos = 1.0f; delta = -delta; } else if (pos < 0.0f) { pos = 0.0f; delta = -delta; }

The key here is that you invert the sign of your increment each time you reach one of the end positions.

更多推荐

本文发布于:2023-07-15 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AI   Opengl   move   paddle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!