car simulation

I’d like to simulate a car driving on a highway. The car is controlled by an artificial intelligence and should be able to keep a distance to the front car and to overtake slower cars.

How can I simulate the movement with java?

The highway can be straight on (no curves). So the car always stays in the middle and the highway “scrolls down”.

Which techniques could I use?

Rather than looking at techniques, I suggest you first consider what the simulation should look like:

i) Top down 2D view or a 3D view?
ii) If 3D is this 1st person or 3rd person view?
iii) Do you need to accurately simulate steering, or is sideways movement sufficient?
iv) Do you need textured graphics or is drawing the simulation from solid colour polygons sufficient?

2D will be easier than 3D. Cars can be drawn in top down view in a graphics editor and saved in .png format. These can then be loaded into an ImageBuffer using methods in the ImageIO class. You might also look at the Image class, which is simpler to use but less versatile. If you want to simulate steering, you will need a number of images, with cars shown at different angles. Since the camera will follow the car, some indication of forward movement is required. I suggest drawing a series of rectangles to represent the white (yellow for US) lines on the road. These are redrawn every frame in a new position dependent on the car’s velocity.

3D is harder. Draw a picture of the back end of a car in a graphics editor. Load it into an ImageBuffer. Draw it in the correct place on the screen, scaling the size according to the distance that the car is away from you. This requires 3D maths. Most of this can be done with similar triangles. Buy a book on 3D graphics to explain how this works. If the road can be drawn in a single grey colour then you can represent it as a triangle, one point is at the screen centre, the other two are at the screen bottom and are slight off screen to the left and right, when you are in the centre of the road. Drawing the lines in the centre of the road is harder, as you will now have to calculate the 2D position on the screen for the 3D position of each corner of the line rectangle. The line, as drawn on the screen won’t be a rectangle, but a trapezium. It can be drawn as two triangles. If you need to show the cars steering, you will need several pictures of the back of the cars, shown at slightly different angles.

For 3D third person view, you can draw your car the same as the others but (say) in a different colour. For a 1st person view, you can omit drawing your car. However, if you are simulating steering, this means that the road will not always be in front of you, making drawing it much more complex. A similar issue applies to the view shown of the other cars, where the image shown is now a function of both your and their steering angles. Best to stick to a 3rd person view to keep it simple.

All of the above assumed that the road and verges are drawn in a single colour. This can all be handled by the built in Java2D graphics library. However if you want a more realistic terrain, then you need to consider texture mapping. There is no built in support for this, but you can use 3rd party libraries like LWJGL or JOGL to access OpenGL which does support texturing. OpenGL provides 3D drawing support, which saves you from having to do all that similar triangle stuff too.

If this all looks new & hard, I would suggest a top down 2D view, with no depiction of steering. You can still simulate steering in the AI model, if you want to make that realistic (I assume that the AI is the object of the exercise).