views:

363

answers:

7

Hello!

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!

+6  A: 

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 wether 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 uou would be thinking about the extent of the modelling (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.

extraneon
@extraneon: Why so harsh? You don't even know what kind of simulation it is going to be, wether there are physics involved and so on. Maybe he was talking about the street "model" as an array (a 2-dimensional array describing a tile based world) ... @kame: Maybe you should be more specific (what the focus of your simulation is going to be, etc.)
das_weezul
@das_weezul You're right. Id did sound a bit harsh. I've updated the answer to reduce the harshness a bit.
extraneon
Yes I meant a tile based world. Maybe I thougt everybody understand that I want to do "easy" stuff, when you see the picture. :/
kame
+2  A: 

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.

Dan Andreatta
+1  A: 

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.

djhworld
+2  A: 

Hello Kame,

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.

cool-RR
+2  A: 

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".

Karel Frajtak
A: 

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.

fortran
I want it really easy. Like in the picture. Cars are twodimensional rectangles.
kame
The picture is a still image, it doesn't tell anything about the time or the dynamics of the simulation. You should work out a detailed specification to know what you want to do first.
fortran
+1  A: 

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.

Paul McGuire