如何组织3D游戏的结构?

发布于 2024-10-04 21:20:53 字数 402 浏览 0 评论 0原文

这是一个有点模糊的问题,但请耐心等待。我正在使用 Python/Pyglet 和 openGL 编写游戏。我目前的结构是这样的,有一个名为“世界”的对象,其中有其他对象,其中包含其他对象等。我这样做是因为例如游戏的一部分是一个平台,上面有其他对象,当我倾斜平台时,我希望平台上的物体也随之倾斜。所以我执行 platform.draw() ,它调用 glRotate、glTranslate,然后绘制每个对象,保存其间的模型视图矩阵,这样平台上的所有对象都会一起移动。 第一个问题是,这是组织事物的明智方式还是我应该使用其他方法?

我没有相机课程,目前我只是翻译整个世界或其中的一部分以产生运动的幻觉。然而,将来我希望能够在对象之间切换视点,例如从从上往下看世界切换到从世界上的一个对象观察第一人称视角。所以第二个问题是构建我的程序的最佳方法是什么,以便将来可以实现这一目标?

This is a bit of a vague question but bear with me. I am in the process of writing a game using Python/Pyglet and openGL. I currently have it structured so that there is an object called 'world', in this are other objects with other objects inside them etc. I did it this way because for instance one part of the game is a platform with other objects on it, and when I tilt the platform I want the objects on it to tilt with it. So I do platform.draw() which calls glRotate, glTranslate, then draw each of the objects saving the modelview matrix inbetween, this way all the objects on the platform move together.
The first question is, is this a sensible way to organise things or should I be using some other method?

I don't have a camera class, currently I am just translating the whole world or parts of it to give the illusion of movement. However, in the future I want to be able to switch viewpoints between objects, so for instance switch from looking down onto the world from above to a 1st person view from one of the objects in the world. So the second question is what is the best way to structure my program so that this will be achievable in the future?

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

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

发布评论

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

评论(2

原谅我要高飞 2024-10-11 21:20:53

可能不是一个完整的答案,但我认为完整的答案需要一整本书......

你在层次结构中对世界的表示所做的事情与通常所说的“场景图”非常相似,而且它在很多情况下都是个好主意。库的一个很好的例子就是Open Scene Graph

关于“平移世界”,除非你真的每次都变换所有顶点,否则也完全没问题。这只是一个相对观点的问题,你真正拥有的与相机矩阵相同。您可以将变换视为将相机放置在世界中,或将世界移动到相机前面。

Not probably a full answer, but i think a full one would take a whole book...

What you are doing with your representation of the world in a hierarchy is very similar to what is usually known as "scene-graph", and it is a good idea in many cases. A good example of a library to do precisly this is Open Scene Graph.

About "translating the world", unless you are really transforming all the vertices every time, it is also perfectly fine. It is just a matter of relative point of view and what your really have is the same that a camera matrix. You can see the transformation as placing the camera in the world, or as moving the world in front of the camera.

_畞蕅 2024-10-11 21:20:53

您可以将逻辑放入单独的模块/单独的类/函数中。
在我的 2D 游戏中,我有一个 GameLogic 类,它简化了某些事件的注册方法或调度它们(以及取消注册+取消调度它们),并且我创建了一个 @state_wrapper 装饰器它注入一个简单的新型对象作为该方法的状态存储。如果您这样做,则不必将指针传递给所有世界对象,只有事件方法必须访问您的对象。

但我不会说这是最好的解决方案;)

You could put the logic into a seperate module / into seperate classes/functions.
In my 2D-Game I have a GameLogic class which simplifies registering it's methods for certain events or scheduling them (and unregistering+unscheduling them), and I created a @state_wrapper decorator which injects a simple new-style object as state-storage for that method. If you do it like that you don't have to pass the pointer to all your world objects, only the event-methods have to get access to your objects.

But I wouldn't claim that this is the best solution ;)

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