Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  car simulation  (Read 2502 times)
0 Members and 1 Guest are viewing this topic.
Offline stevy

Innocent Bystander

« Posted 2005-07-26 13:28:39 »

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?
Offline Alan_W

JGO Knight

Medals: 8
Projects: 3

Java tames rock!

« Reply #1 - Posted 2005-07-27 18:50:30 »

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

Time flies like a bird. Fruit flies like a banana.
Pages: [1]
  ignore  |  Print  

EgonOlsen (45 views)
2018-06-10 19:43:48

EgonOlsen (27 views)
2018-06-10 19:43:44

EgonOlsen (47 views)
2018-06-10 19:43:20

DesertCoockie (210 views)
2018-05-13 18:23:11

nelsongames (127 views)
2018-04-24 18:15:36

nelsongames (126 views)
2018-04-24 18:14:32

ivj94 (867 views)
2018-03-24 14:47:39

ivj94 (128 views)
2018-03-24 14:46:31

ivj94 (779 views)
2018-03-24 14:43:53

Solater (143 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!