面向对象游戏编程中的物理

发布于 2024-10-27 23:32:40 字数 801 浏览 5 评论 0原文

所以我正在尝试学习如何开发2D游戏。

我正在使用 Android 和 C#。

我遇到的问题是这样的。

绘制某种精灵是很容易的。我努力的地方是想象如何对屏幕上各种对象的交互进行编码。

例如,假设我有以下对象。

public class MyGenericObject {
    public MyGenericObject() {}
    public Int32 Width { get; set; }
    public Int32 Height { get; set; }
    public Int32 X { get; set; }
    public Int32 Y { get; set; }
    public Image ObjectImage { get; set; }
}

假设我想对 N 个这些对象之间的交互行为进行建模。在这种情况下,人们会使用什么典型模式?

例如,您是否可以简单地调用诸如 MyPhysicsSim.DoPhysics(List); 之类的方法,

假设这不是一个巨大的延伸。那么,在这个方法内部,如何计算交互作用和产生的运动呢?我只是无法理解它(显然我确实明白有一些基本方程可以求解方向和速度等,但我似乎无法弄清楚如何将它们应用为算法)。

我看过很多教程,所有教程似乎都专注于绘制一个对象,或者假设对内部结构的理解程度超出了我的能力范围。所以我希望得到一个非常简单、简单的回应。

所以基本上我要问的是,对于傻瓜来说,如何在 OO 范式中处理 2D 对象之间的交互?

So I am in the process of trying to learn how to develop 2D games.

I am playing around with both Android and C#.

The issue that I am having is this.

It is easy enough to draw a sprite of some sort. Where I struggle is to visualize how one can code interactions of various objects on screen.

For example lets say I have the following Object.

public class MyGenericObject {
    public MyGenericObject() {}
    public Int32 Width { get; set; }
    public Int32 Height { get; set; }
    public Int32 X { get; set; }
    public Int32 Y { get; set; }
    public Image ObjectImage { get; set; }
}

Lets say I want to model the behavior of an interaction between N number of these Objects. What is a typical pattern one would utilize in such a scenario?

So for example would you simply call a method such as MyPhysicsSim.DoPhysics(List<MyGenericObjects>);

Assuming this is not a huge stretch. Inside of this method then, how do you calculate the interactions and resulting movements? I just can't get my head around it (Obviously I do understand that there are basic equations that can solve direction and speed and so forth but I just can't seem to work out how to apply these as an algorithm).

I have looked at numerous tutorials and all of them seem to concentrate on just drawing an object or assume a level of understanding about the internals that is way over my head. So I am hoping for a really simple, dumbed down response.

So basically what I am asking is how do you approach interactions between 2D objects in a OO paradigm for dummies?

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

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

发布评论

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

评论(1

三月梨花 2024-11-03 23:32:40

传统上,大多数游戏在核心位置都有一个类似于下面的循环:

while (1) {
    World.respondToInput();
    World.updatePhysics();
    World.renderScene();
    Timer.waitUntilFrameExpires();
}

“updatePhysics()”方法可以做一些非常基本的事情,例如:

for (Object obj1 in World.trackedObjects()) {
    for (Object obj2 in World.trackedObjects()) {
        if (obj1.influences(obj2)) {
            obj1.update(); obj2.update();
        }
    }
}

所以你在模拟的世界中基本上有一个“刻度”,并且您的代码可以反映场景中可能发生的增量。

Traditionally, most games have a loop similar to the one below somewhere at the core:

while (1) {
    World.respondToInput();
    World.updatePhysics();
    World.renderScene();
    Timer.waitUntilFrameExpires();
}

and the 'updatePhysics()' method can do something very basic, such as:

for (Object obj1 in World.trackedObjects()) {
    for (Object obj2 in World.trackedObjects()) {
        if (obj1.influences(obj2)) {
            obj1.update(); obj2.update();
        }
    }
}

So you basically have a 'tick' in the world you're simulating, and your code works to reflect the delta in the scene that would have occurred.

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