自由落体问题

发布于 2024-08-30 21:26:22 字数 1007 浏览 2 评论 0原文

好的,我会尽力解释我的问题。我有这个程序,你可以选择 5 个球。当您选择一个时,您可以在按住鼠标按钮并且光标位于球的半径内的同时拖动它。

问题是,我需要一种方法,当用户停止按下鼠标按钮时使球上升,就像他将球漂浮在空中然后让它再次落下一样。我有一种方法可以知道时间、速度以及加速度,但我不知道如何实现它。

现在我有这个:

void Circle::Fall(float velocity,float time)
{    
    if(this->posY >= 580)
    {
        this->posY = 580;
        this->vfall= 0.0f;
    }
    else if(this->posY < 580)
    {
        //this->distance=9.81f * 0.5f*time*time;
        this->vfall+= velocity;
        this->posY += this->vfall;
    } 
}

像这样它就会掉落,我无法达到我试图解释的效果。

另外,我正在计算这样的时间(以防万一它有帮助):

difX=(x> event.motion.xrel)? x-event.motion.xrel : event.motion.xrel-x;
difY=(y> event.motion.yrel)? y-event.motion.yrel : event.motion.yrel-y;

我使用 difY 作为时间变量


好吧,抱歉,我现在把它变成了英语。我将尝试让这个更容易理解:

你需要让球以释放点击时鼠标的速度漂浮得更久一点,就像一只手在拿了一些球后将球扔到空中一样冲动。它不需要掉头或任何东西,只需在 Y 上稍微上升一点。我正在使用 SDL,以防您需要知道。

此外,您还可以从窗口底部取出球,当您松开它们时单击它们他们会自动归还

OK, I'm going to try my best to explain my problem. I have this program where you can select 5 balls. When you select one, you can drag it while you have the mouse button pressed and the cursor is within the ball's radius.

The problem is that I need a way to make the ball go up when the user stop pressing the mouse button, like he sent it to float in the air then made it fall down again. I have one way to know the time, velocity and thus the acceleration, but I don't know how to implement it.

Right now i have this:

void Circle::Fall(float velocity,float time)
{    
    if(this->posY >= 580)
    {
        this->posY = 580;
        this->vfall= 0.0f;
    }
    else if(this->posY < 580)
    {
        //this->distance=9.81f * 0.5f*time*time;
        this->vfall+= velocity;
        this->posY += this->vfall;
    } 
}

With it like this it just falls, and I can't make the effect I tried to explain.

Also, I'm calculating the time like this (just in case it helps):

difX=(x> event.motion.xrel)? x-event.motion.xrel : event.motion.xrel-x;
difY=(y> event.motion.yrel)? y-event.motion.yrel : event.motion.yrel-y;

And I'm using difY as the time variable


OK, sorry, I made it English now. And I'm going to try to make this easier to understand:

You need to make the ball float a little longer with the speed of the mouse at the moment it releases the click, like a hand throwing a ball into the air after taking some impulse. It does not have to make a U-turn or anything just go up a little on Y. I'm using SDL in case you need to know

Also you take the balls from the bottom of the window and when you release the click on them they return them automatically

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

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

发布评论

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

评论(3

傲性难收 2024-09-06 21:26:22

我想你想要这本书:游戏开发者物理学

I think you want this book: Physics for Game Developers.

不顾 2024-09-06 21:26:22

我认为你需要根据声音设置向上的初始速度。

如果存储垂直速度,则可以将其设置为正数。然后每一帧,将速度添加到该位置,并从速度中减去一点,使其表现得像有重力。

因此,有一个 this->yVelocity (或用您自己的语言 ;D)和一个 this->yPosition,以及一个 gravityMagnitude >,可能类似于 -0.1

每一帧,执行如下操作:

this->yVelocity += gravityMagnitude; //Subtracts 0.1
this->yPosition += this->yVelocity; //Make it actually move

这将真实地模拟重力,因为球的运动将模拟二次函数。

另外,这样,如果你想让球一开始浮起来一点,只要他们一放手就说:

this->yVelocity = 2.0; //Or some other positive number

它会以一点速度开始。

I think you need to set an initial velocity upward, by the sounds of it.

If you store the vertical velocity, you can set it to be a positive number. Then every frame, add the velocity to the position, and subtract a little bit from the velocity to make it act like there's gravity.

So have a this->yVelocity (or in your own language ;D) and a this->yPosition, as well as a gravityMagnitude, which could be something like -0.1.

Every frame, do something like this:

this->yVelocity += gravityMagnitude; //Subtracts 0.1
this->yPosition += this->yVelocity; //Make it actually move

This will realistically simulate gravity, since the motion of the ball will mimic a quadratic function.

Also, this way, if you want the ball to float up a bit at first, simply say this as soon as they let go:

this->yVelocity = 2.0; //Or some other positive number

And it will start with a little speed.

拥有 2024-09-06 21:26:22

确实把我的记忆拉回了高中物理。您需要的是 suvat 公式,特别是位移公式:

           1   2
s = ut  +  - at
           2
  • s 是位移(来自初始位置)。
  • u 是起始速度。
  • t 是经过的时间。
  • a是加速度。

因此,假设您以(例如)每秒 5 米的速度向上抛球,并且加速度为 -9.8 米每秒平方(在地球上),那么一秒后它的位置将比起始高度高出 0.2 米。

请注意,您只需担心这些方程中的上下(重力),除非您的应用程序非常现实以至于需要考虑风阻。

您需要做的就是存储初始位移(高度)和速度(仅垂直分量),然后您可以计算任意给定时间值的位移。

请记住,这些方程适用于恒定加速度的情况。它们可能(大部分)保持在地球周围,但在重力快速变化的地方(例如黑洞附近)可能不太适应。

This is really stretching my memory back to high school physics. What you need are the suvat formulae, specifically the one for displacement:

           1   2
s = ut  +  - at
           2
  • s is the displacement (from initial position).
  • u is the starting speed.
  • t is the elapsed time.
  • a is the acceleration.

So, given that you're throwing the ball up at (for example) 5 metres per second, and with acceleration being -9.8 metres per second squared (on Earth), its position after one second would be 0.2 metres above the starting height.

Note that you only have to worry about up and down in these equations (gravity) unless your application is going to be so realistic that it takes wind resistance into account.

All you need to do is to store the initial displacement (height) and velocity (vertical component only), then you can calculate the displacement at any given time value.

Keep in mind these equations are for constant acceleration situations. They may hold (mostly) around the Earth but may not be so accommodating where the gravity changes rapidly (like near a black hole).

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