基于 SDL_GetTicks() 的运动?

发布于 2024-12-09 16:33:19 字数 139 浏览 0 评论 0原文

我正在用 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 技术交流群。

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

发布评论

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

评论(2

淡淡的优雅 2024-12-16 16:33:19

我通过创建自己的 Timer 类解决了这个问题。
从技术上讲,您要做的是在构造函数中使用 GetTicks() 分配一个开始毫秒值

Timer::Timer(uint32_t ToWait, void (*call_back)(void*), void* context) : Callback(call_back), Context(context), Running(true), ToWait_private(ToWait)
{
    StartTicks = SDL_GetTicks();
}

现在您实现一个 update() 函数,每次都保存当前的刻度

void Timer::Update()
{
    if(!Running)
        return;

    CurrentTicks = SDL_GetTicks(); //get the current ticks

    if(CurrentTicks - ToWait > StartTicks) //if the ticks are more than how much to wait
    {
        Running = false;

        Callback(Context); //call my callback or whatever
    }
}

这不是全部代码,但它应该给您一个像如何实施。由于 SDL_ticks 计算的是毫秒,因此您还可以实现一些转换函数来设置以秒左右为单位的时间。

在你的游戏机制中,当你需要等待时,创建一个实例。然后,调用定时器的Update()。好处是它不必定期调用,因为 SDL_getTicks 每次都会为您提供值。

问题是为函数设置回调,取决于它是否是成员函数,那么你应该使用一些包装器。对于静态函数,没有什么可担心的,只需使用经典的函数指针即可。 Lambda 和许多其他东西,stoppin,暂停计时器也可以实现,只要有创意:]

为了最终回答你关于如何让球移动的问题,你可以

Move()

在每次时间到时调用一些函数作为回调。但是,当移动球时,您应该重新启动计时器。 :]

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()

Timer::Timer(uint32_t ToWait, void (*call_back)(void*), void* context) : Callback(call_back), Context(context), Running(true), ToWait_private(ToWait)
{
    StartTicks = SDL_GetTicks();
}

Now you implement an update() function, in wich you save current ticks every time

void Timer::Update()
{
    if(!Running)
        return;

    CurrentTicks = SDL_GetTicks(); //get the current ticks

    if(CurrentTicks - ToWait > StartTicks) //if the ticks are more than how much to wait
    {
        Running = false;

        Callback(Context); //call my callback or whatever
    }
}

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

Move()

function as callback every single time the time is up. When moving the ball, you should, however, restart the timer. :]

℡寂寞咖啡 2024-12-16 16:33:19

老了 修复你的时间步长!

你不必使用 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.

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