二维运动理论

发布于 2024-10-01 02:41:06 字数 1327 浏览 0 评论 0原文

我最近开始研究 OpenGL/SDL 并在 2D/3D 空间中玩弄对象,并且我确信该领域的大多数新手都会有一些关于做某事的“最佳”方法的疑问。我引用最好的,因为我假设没有最好的方法,这是个人喜好。

所以,我有一个实体,一个简单的 GL_QUAD,我想移动它。我有键盘事件设置,我可以获得按键/释放事件,这不是问题。

我有一个实体类,这是我的 GL_QUAD,伪实现...

class Entity
{
    void SetVelocity(float x, float y);
}

然后我有这个事件处理程序代码...

if theEvent.Key = UPARROW AND theEvent.State = PRESSED
    Entity.SetVelocity(0.0f, -1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
    Entity.SetVelocity(0.0f, 0.0f);

我的问题是,这是移动我的实体的最佳方式吗?这让我想到我们可以通过单独调整 X/Y 速度的方法来使其变得更复杂一些。如果我开始向左移动,SetVelocity 会忘记我的 X 速度吗?所以我永远不能沿对角线旅行。

例如...

class Entity
{
    void SetXVelocity(float x);
    void SetYVelocity(float y);
}

if theEvent.Key = UPARROW AND theEvent.State = PRESSED
    Entity.SetYVelocity(-1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
    Entity.SetYVelocity(0.0f);
if theEvent.Key = LEFTARROW AND theEvent.State = PRESSED
    Entity.SetXVelocity(-1.0f);
else if theEvent.Key = LEFTARROW AND theEvent.State = RELEASED
    Entity.SetXVelocity(0.0f);

这样,如果我有一个 XVelocity,然后按 UPARROW,我仍然会有我的 XVelocity,以及一个新的 YVelocity,从而对角移动。

有更好的办法吗?我在这里错过了一些非常简单的东西吗?

我正在使用 SDL 1.2、OpenGL、C++。不确定 SDL/OpenGL 中是否有某些内容有帮助?

预先感谢您的回答。

I have recently been getting into OpenGL/SDL and playing around with objects in 2D/3D Space and as i'm sure most newbies to this area do, have a few queries, about the 'best' way to do something. I quote best, because i'm assuming there isn't a best way, it's personal preference.

So, I have an entity, a simple GL_QUAD, which I want to move around. I have keyboard events setup, i can get the keypress/release events, not a problem.

I have an entity class, which is my GL_QUAD, pseudo implementation....

class Entity
{
    void SetVelocity(float x, float y);
}

I then have this event handler code...

if theEvent.Key = UPARROW AND theEvent.State = PRESSED
    Entity.SetVelocity(0.0f, -1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
    Entity.SetVelocity(0.0f, 0.0f);

My question is, is this the best way to move my entity? This has led me to thinking that we could make it a little more complex, by having a method for adjusting the X/Y Velocity, seperately. As SetVelocity would forget me X velocity if i started moving left? So i could never travel diagonally.

For Example...

class Entity
{
    void SetXVelocity(float x);
    void SetYVelocity(float y);
}

if theEvent.Key = UPARROW AND theEvent.State = PRESSED
    Entity.SetYVelocity(-1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
    Entity.SetYVelocity(0.0f);
if theEvent.Key = LEFTARROW AND theEvent.State = PRESSED
    Entity.SetXVelocity(-1.0f);
else if theEvent.Key = LEFTARROW AND theEvent.State = RELEASED
    Entity.SetXVelocity(0.0f);

This way, if I have an XVelocity and I then press the UPARROW, I will still have my XVelocity, as well as a new YVelocity, thus moving diagonally.

Is there a better way? Am I missing something very simple here?

I am using SDL 1.2, OpenGL, C++. Not sure if there is something in SDL/OpenGL which would help?

Thanks in advance for your answers.

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

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

发布评论

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

评论(4

愿得七秒忆 2024-10-08 02:41:06

这个问题非常普遍,因为它取决于您想要如何对世界中对象的运动进行建模。

通常每个物体都有其速度,该速度是根据加速度计算出来的,并有最大值。这意味着按键会改变指定帧的对象加速度,然后计算该加速度并将其应用于对象的当前速度。

这是通过更新阶段完成的,该阶段遍历所有对象并根据对象加速度计算速度变化。通过这种方式,您不必费心去修改速度本身,而是让您的引擎根据每个对象的状态进行计算。

The question is really general since it depends on how you want to model the movement of your objects in your world.

Usually every object has its velocity which is calculated basing on an acceleration and capped to a maximum. This means that a key press alters the acceleration of the object for the specified frame which is then calculated and applied to the current velocity of the object.

This is done through an update phase which goes through all the objects and calculates the velocity change according to the object acceleration. In this way you don't bother to modify the velocity itself but you let your engine to do its calculations depending on the state of every object..

那一片橙海, 2024-10-08 02:41:06

加速度在一段时间内应用,因此在 @jack 的示例中,您将在一秒的时间内应用 10m/s^2 的加速度。

您还应该修改您的应用程序,使其基于时间< /a>,不是基于框架的。

看看这个基本游戏物理介绍,我也想看看GameDev.net 物理教程

acceleration is applied over a period of time, so in the example by @jack, you would apply an acceleration 10m/s^2 over a time period of one second.

you should also modify your application to make it time based, not frame based.

have a look at this basic game physics introduction, and I would also really have a look at the GameDev.net Physics Tutorials

心清如水 2024-10-08 02:41:06

我认为您希望移动的方式是您希望玩家仅在按住按键时才移动。
简而言之:您的解决方案很好。

需要考虑的一些潜在问题:如果同时按下左右键会发生什么?

I assume the way you want movement to work is that you want the player to move only when a key is held.
In short: your solution is fine.

Some potential gotchas to take consideration of: What happens if both left and right is pressed?

花伊自在美 2024-10-08 02:41:06

好吧,您在这里描述的是一个简单的有限状态机。您可以将不同的移动方向(加上根本不移动)作为状态,将关键事件作为转换。这通常可以使用 状态模式 很好地实现,但这在 C++ 中通常非常痛苦(很多样板代码),并且可能超出了您的场景。

当然还有其他方法来表示实体的速度和方向,例如作为 2D 向量(其中长度给出速度)。这将使您能够轻松表示任意方向和速度。

Well, what you describe here is a simple finite state machine. You have the different directions in which you can move (plus no movement at all) as the states, and the key-events as transitions. This can usually be implemented quite well using the state pattern, but this is often quite painful in C++ (lots of boilerplate code), and might be over the top for your scenario.

There are of course other ways to represent speed and direction of your entity, e.g. as a 2D-vector (where the length gives the speed). This would enable you to easily represent arbitrary directions and velocities.

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