基于 SDL_GetTicks() 的运动?
我正在用 OpenGL 和 SDL 编写乒乓球游戏。我对 SDL_GetTicks() 的工作原理了解甚少,但我正在努力想出一种方法来实现如何让我的球移动,例如,作为新手,每 1000 毫秒移动一次。
简短的例子、解释、帮助,任何东西都将不胜感激。
I'm writing a game of pong in OpenGL and SDL. I have a small knowledge of how SDL_GetTicks() works but I am struggling to think of a way to implement how to make my ball move for example every 1000 milliseconds being the novice I am.
Short examples, explanations, help, anything at all would be appreciated greatly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过创建自己的 Timer 类解决了这个问题。
从技术上讲,您要做的是在构造函数中使用 GetTicks() 分配一个开始毫秒值
现在您实现一个 update() 函数,每次都保存当前的刻度
这不是全部代码,但它应该给您一个像如何实施。由于 SDL_ticks 计算的是毫秒,因此您还可以实现一些转换函数来设置以秒左右为单位的时间。
在你的游戏机制中,当你需要等待时,创建一个实例。然后,调用定时器的Update()。好处是它不必定期调用,因为 SDL_getTicks 每次都会为您提供值。
问题是为函数设置回调,取决于它是否是成员函数,那么你应该使用一些包装器。对于静态函数,没有什么可担心的,只需使用经典的函数指针即可。 Lambda 和许多其他东西,stoppin,暂停计时器也可以实现,只要有创意:]
为了最终回答你关于如何让球移动的问题,你可以
在每次时间到时调用一些函数作为回调。但是,当移动球时,您应该重新启动计时器。 :]
I solved it by creating my own Timer class.
Technically, what you do is that at constructor you assign a start miliseconds value by using GetTicks()
Now you implement an update() function, in wich you save current ticks every time
This is not all code but it should give you a point like how to implement it. Since the SDL_ticks are counting miliseconds, you can also implement some convert function for setting time in seconds or so.
In your game mechanism, whenewer you need to wait, create an instance. Then, call Update() of timer. Good thing is that it does not have to be called regularily, as SDL_getTicks does give you the value every time.
The thing is to set a callback to a function, depends whether it is member function, then you should use some wrapper. In case of static function, there is nothing to fear and just use classic pointer to function. Lambda and many other things, stoppin, pausding timer can also be implemented, just be creative :]
To finally answer your question on HOW to make the ball move, you can call some
function as callback every single time the time is up. When moving the ball, you should, however, restart the timer. :]
老了 修复你的时间步长!
你不必使用 RK4,即“最后一个时间步长之间的插值”两个SIM状态”是重要的部分。
Ye Olde Fix Your Timestep!
You don't have to use RK4, the "interpolate between the last two sim states" is the important part.