views:

507

answers:

5

For Operating Systems class I'm going to write a scheduling simulator entitled "Jurrasic Park".

The ultimate goal is for me to have a series of cars following a set path and passengers waiting in line at a set location for those cars to return to so they can be picked up and be taken on the tour. This will be a simple 2d, top-down view of the track and the cars moving along it.

While I can code this easily without having to visually display anything I'm not quite sure what the best way would be to implement a car moving along a fixed track.

To start out, I'm going to simply use OpenGL to draw my cars as rectangles but I'm still a little confused about how to approach updating the car's position and ensuring it is moving along the set path for the simulated theme park.

Should I store vertices of the track in a list and have each call to update() move the cars a step closer to the next vertex? If anyone can conceptually come up with a design better than this, I would appreciate some feedback.

+2  A: 

You might find Splines useful.

GMan
How so? Details? Code examples? Any information that pertains to his question?
Andy Mikula
The page I linked to has the following: Details, code examples, and information that pertains to his question.
GMan
It does, but it's not very beginner friendly.
Gary Willoughby
@Gary: They can only get so simple.
GMan
That's an elitist attitude, rather like your bio.
Gary Willoughby
@Gary: Heh, k. My profile is inspired by the constant bombardment of misguided questions that you'll see if hang around the C++ tag. What isn't beginner friendly? How can splines be presented any simpler?
GMan
+1  A: 

I would keep it simple:

Run a timer (every 100msec), and on each timer draw each ones of the cars in the new location. The location is read from a file, which contains the 2D coordinates of the car (each car?).

If you design the road to be very long (lets say, 30 seconds) writing 30*10 points would be... hard. So how about storing at the file the location at every full second? Then between those 2 intervals you will have 9 blind spots, just move the car in constant speed (x += dx/9, y+= dy/9).

I would like to hear a better approach :)

elcuco
Keeping their positions fixed won't work. They might be waiting on passangers, etc, etc, etc.
Mithrax
A: 

Well you could use some path as you describe, ether a fixed point path or spline. Then move as a fixed 'velocity' on this path. This may look stiff, if the car moves at the same spend on the straight as cornering.

So you could then have speeds for each path section, but you would need many speed set points, or blend the speeds, otherwise you'll get jerky speed changes.

Or you could go for full car simulation, and use an A* to build the optimal path. That's over kill but very cool.

Simeon Pilgrim
He only has one path...why does he need to find the 'optimal' path?
Mark
He says he has a fixed track not a single path, most 'tracks' I have seen have width, if your wanting it to look more real, most cars hug the corner, and drift wide after corner.
Simeon Pilgrim
A: 

If there is only going forward and backward, and you know that you want to go forward, you could just look at the cells around you, find the ones that are the color of the road and move so you stay in the center of the road.

If you assume that you won't have abrupt curves then you can assume that the road is directly in front of you and just scan to the left and right to see if the road curves a bit, to stay in the center, to cut down on processing.

There are other approaches that could work, but this one is simple, IMO, and allows you to have gentle curves in your road.

Another approach is just to have it be tile-based, so you just look at the tile before you, and have different tiles for changes in road direction an so you know how to turn the car to stay on the tile.

This wouldn't be as smooth but is also easy to do.

James Black
+2  A: 

If you want curved track, you can use splines, which are mathematically defined curves specified by two vector endpoints. You plop down the endpoints, and then solve for a nice curve between them. A search should reveal source code or math that you can derive into source code. The nice thing about this is that you can solve for the heading of your vehicle exactly, as well as get the next location on your path by doing a percentage calculation. The difficult thing is that you have to do a curve length calculation if you don't want the same number of steps between each set of endpoints.

An alternate approach is to use a hidden bitmap with the path drawn on it as a single pixel wide curve. You can find the next location in the path by matching the pixels surrounding your current location to a direction-of-travel vector, and then updating the vector with a delta function at each step. We used this approach for a path traveling prototype where a "vehicle" was being "driven" along various paths using a joystick, and it works okay until you have some intersections that confuse your vector calculations. But if it's a unidirectional closed loop, this would work just fine, and it's dead simple to implement. You can smooth out the heading angle of your vehicle by averaging the last few deltas. Also, each pixel becomes one "step", so your velocity control is easy.

In the former case, you can have specially tagged endpoints for start/stop locations or points of interest. In the latter, just use a different color pixel on the path for special nodes. In either case, what you display will probably not be the underlying path data, but some prettied up representation of your "park".

Just pick whatever is easiest, and write a tick() function that steps to the next path location and updates your vehicle heading whenever the car is in motion. If you're really clever, you can do some radius based collision handling so that cars will automatically stop when a car in front of them on the track has halted.

Furious Coder