Python:交通模拟(道路上的汽车)

发布于 2024-08-30 19:14:10 字数 422 浏览 9 评论 0原文

我想创建一个像这样的交通模拟器: http://www.doobybrain.com/wp-内容/uploads/2008/03/traffic-simulation.gif 但我并没有对此进行深入思考。

我会创造一流的汽车。 每辆车都有自己的颜色、位置等。

我可以用数组创建道路。

但如何告诉汽车去哪里呢? 我可以听听你的想法吗?

编辑:是否禁止从优秀程序员那里获取新想法?为什么有些人想关闭这个帖子?或者要问这样的问题?我不明白他们。 :(

EDIT2:下次我会去游戏玩家论坛。到目前为止我喜欢这些想法。谢谢!

I want to create a traffic simulator like here:
http://www.doobybrain.com/wp-content/uploads/2008/03/traffic-simulation.gif
But I didn't thougt very deep about this.

I would create the class car.
Every car has his own color, position and so on.

And I could create the road with an array.

But how to tell the car where to go?
Could I hear your ideas?

EDIT: Is it forbidden to get new ideas from good programmers? Why do some people want to close this thread? Or were to ask such questions? I dont understand them. :(

EDIT2: Next time I will go to a gamer-forum. I like the ideas until now. Thanks!

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

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

发布评论

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

评论(7

罪歌 2024-09-06 19:14:11

你不会告诉汽车去哪里。由于其速度,它无论如何都会前进。通过向前看(从现在开始几个时间步将在哪里,还有路吗?),您可以看到是否需要调整速度。

道路并不是一个数组;而是一个数组。它是一个矩阵或位图。你不能在拐角处开得太快,否则你会飞离公路。

首先明确制定目标和范围总是一个好主意。在这种情况下,您将考虑建模的范围(涉及的物理原理)和汽车运动的复杂性(碰撞、转弯等)。

我发现如果你能用简单的英语(或者在我的例子中是荷兰语)清楚地表达你的概念,编写代码就会变得容易得多,有时甚至微不足道。

编辑

为了清楚起见,当我谈论速度时,我谈论的是矢量,它既有大小 (10MPH) 又有方向。你的向量将有 2 个元素,xy,除非你还想要飞机,或者想要建模山丘等(你不想要山丘,因为你会还必须考虑重力:),其中使用 z 位置和速度。

You don't tell a car where to go. It goes anyway due to its velocity. By looking ahead (where will it be a few timesteps from now, and is there still a road?) you can see whether you need to adjust the velocity.

And a road isn't an array; it's a matrix or bitmap. You can't go all that fast in the corner or you'll fly from the road.

It's always a good idea to clearly formulate the goals and scope first. In this case, you would be thinking about the extent of the modeling (the physics involved) and the complexity of the car movements (collisions, following bend roads, and so on).

I find that if you can state your concept clearly in plain English (or Dutch in my case), writing the code becomes much easier, almost trivial sometimes.

EDIT

For clarity, when I'm talking about velocity I'm talking about a vector, which has both magnitude (10MPH) and direction. Your vectors would have 2 elements, x and y, unless you also want airplanes, or want to model hills and such (you don't want hills 'cause you'd also have to take gravity into account:) where a z position and velocity are used.

独享拥抱 2024-09-06 19:14:11

每辆车都有位置、速度、方向/轨迹/目的地。根据这些信息以及其他汽车的相对位置和(最终)“路标”的存在,您可以计算每辆车的下一个位置以及汽车是否会减速或加速。

当您将一辆新车添加到模拟中时,只需设置其初始位置、速度和目的地。

Each car has a position, speed, direction/trajectory/destination. Based in this information and, relative position of the other cars and (eventually) the presence of "road signs", you can calculate the next position of each car and if the car will be slowing down or accelerating.

When you add a new car to the simulation, just set its initial position, speed and destination.

如果没有 2024-09-06 19:14:11

您还需要考虑碰撞检测,不一定是汽车本身之间的碰撞,但您需要一种方法来告诉代理(人工智能)在其他车辆附近何时适合制动。您可以通过在每辆车周围设置边界球来实现此目的。

You'll also need to consider collision detection, not necessarily collisions between cars themselves but you'll need a way of telling the agents (the AI) when it's appropriate to brake when in the vicinity of of other vehicles. You could do this with bounding spheres around each car.

洋洋洒洒 2024-09-06 19:14:11

我鼓励您为此使用我自己的项目。它被称为 GarlicSim,它是一个进行模拟的平台,它将非常适合您想要的模拟类型。 (顺便说一下,就在昨天我发布了一个大版本,版本0.4)。

它都是 Python 的,而且它还会给你一个漂亮的 GUI。

如果你认真对待它,我可以帮助你编写你的 simpack。如果您想这样做,请在邮件列表上发送邮件。

I encourage you to use my own project for this. It's called GarlicSim, it's a platform for making simulations, and it will work great for the kind of simulation you have in mind. (Incidentally, just yesterday I made a big release, version 0.4).

It's all Python, and it'll give you a nice GUI too.

If you'll be serious about it, I could help you write your simpack. If you want to do it, send a mail on the mailing list.

层林尽染 2024-09-06 19:14:11

模拟器背后的理论是排队理论。我在母校的课程上找到了一个页面,其中会向您显示一些关键字,可以帮助您 - https://www.erasmus.cvut.cz/prospectus/FTS/11THO-E.html
模拟器本身会“更容易”。

The theory behind the simulator is Queing Theory. I found a page on a course on my alma mater that will show you some keywords, that can help you - https://www.erasmus.cvut.cz/prospectus/FTS/11THO-E.html.
Simulator itself will be "easier".

夜唯美灬不弃 2024-09-06 19:14:11

有一个名为 processing 的基于 Java 的包,它针对艺术家和非程序员或初学者程序员,其中包括一些非常好的可视化功能,同时提供简化的开发环境(例如,没有“public static void main(String[] args)”)。查看展示粒子系统的演示,了解如何对您的个人汽车进行建模。此代码调用 pyprocessing 的 Python 版本遵循许多相同的简单编程习惯。

这是我的小“tesla”小程序 - 在两个球形电极之间移动鼠标。

There is a Java-based package called processing that is targetted at artists and non- or beginner-programmers that includes some very nice visualization features, while presenting a simplified development environment (no "public static void main(String[] args)", for instance). Look at the demos that show particle systems to get an idea on how to model your individual cars. There is a Python rendition of this code call pyprocessing that follows much of the same simple programming idiom.

Here is my little "tesla" applet - move the mouse between the two spherical electrodes.

孤君无依 2024-09-06 19:14:11

第一个问题:模拟的范围是什么?

您可以像离散时间内的一维细胞自动机一样简单,也可以像成熟的物理系统一样复杂(您甚至可以对引擎中的化学反应进行建模!),使用先进的人工智能和先进的逼真图形和声音效果。

其间,还有很多可能性。想想你能做什么以及你想学什么。

First question: What is the scope of the simulation?

You can do it as much as simple as a one dimensional cellular automata in discrete time or as complex as a full fledged physical system (you can model even the chemical reactions in the engine!) with a sophisticated AI and advanced photorealistic graphics and sound effects.

In between, there are lots of possibilities. Think about what you can do and what you want to learn.

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