There are some variations, but the basic structure of my programs is almost always the same.
The main class in my program is always titled Mykeynavbeh, but of course you can change it.
A KeyNavigatorBehavior object is created so that you can navigate through the scene by pressing an arrow key.
The KeyListener interface is implemented so that the keyTyped method can be used. You can move or rotate a 3D model by pressing an assigned key.
Related videos:
If you copy the following code, then you can rotate a 3D model connected under tg when the s key is pressed:
But if you are really interested in what's going on here,
please check the following video: 6:28~
Then, I added an inner class, in the following code MovingCar, to the Mykeynavbeh class.
The MovingCar class extends the Behavior class, so it has the processStimulus method, which can be executed at certain intervals.
Within the method, you can write code for moving a car model.
If you press the f or s key to turn the model to the right or to the left while the model is moving forward, a realistic car movement can be created.
Related video:
You can create a 3D model using primitives, but there are limitations to what primitives can do.
Related videos:
5:14~
If you can import external 3D models into your Java 3D programs, it opens up a whole new world.
Related videos:
Just by pressing the s, f, e, or c key, you get the feeling that you are in a car or in a flying object, and controlling it.
Related videos:
Having said that, sometimes, importing external 3D models causes a whole bunch of errors, or even worse, we don't understand what our PCs are complaining about. In some cases, nothing appears with no error.
Among .wrl, .obj, and .3ds files, .wrl files are the easiest to import.
If a texture specification part is gone, just insert the following part, then it works fine 99%:
For .3ds, the Inspector3DS/Loader3DS Documentation says that 3DS format supports only 8.3 filenames (a maximum file name of eight characters and up to a three character extension) for textures:
http://www.mnstarfire.com/download/inspector3ds.html
On my PC, sometimes a texture with a long file name disappears, but sometimes it doesn't, which puzzle me.
Seemingly, .obj files are the easiest to import because the Wavefront OBJ loader comes with Java 3D, but this format is often a tough nut to crack, and textures are displayed strangely.
I don't know why, but if you import a problem .obj file into Blender or Wing3D, export it, then import it into a Java 3D program, everything is fine. I think it's worth a try.
On the other hand, if you import an .obj file into Blender, export it, then import it into a Java 3D program, a "java.io.FileNotFoundException" error might occur.
In this case, check its .mtl file. If a map_Kd line is like this:
map_Kd C:\Users\Username\MyJava3D\xxxx\model\xxxx.jpg (absolute path),
change it to like this:
map_Kd xxxx.jpg.
An .obj file exported from Blender may cause the following error:
ParsingErrorException: Unrecognized token, line~
Java 3D cannot understand a line beginning with "o,"
so comment out or delete such a line.
o Cylinder_Cylinder.007
↓
#o Cylinder_Cylinder.007
Related videos:
If we are really, really good at creating realistic environments using modeling software, we can say, sitting in front of our PC screens, "I'm the simulator of the world!."
Even if it's impossible, we definitely need grounds, skies, terrains, or buildings for our flying 3D objects.
Otherwise, nobody knows what they are doing.
To create terrains, I used Terragen Classic. Since terrain files (.ter) generated by Terragen can be converted to .obj, they can be imported into Java 3D programs.
My source code for Terragen terrains is based on Landscape.java used in the Dr. Andrew Davison's "Killer Game Programming in Java."
For details of Landscape.java, please refer to Chapter 27, "Terrain Generation with Terragen" of Dr. Andrew Davison' "Killer Game Programming in Java."
Related videos:
Using lights effectively makes the virtual world fantastic.
Light color and direction are specified like this:
Related video:
Using the SharedGroup class makes one object look like many. If you use a polygon to which a texture is applied, you can save more memory.
Related videos: