J2MEMobile3D getting started tutorial series article 2

Source: Internet
Author: User
Here, I would like to thank you for your support for one of the previous articles in the J2MEMobile3D getting started tutorial series. with your support, I decided to share my Mobile3D learning process with you, I hope you can come and discuss Mobile3D here. First of all, I would like to thank you for your support for one of the articles in the previous article on the introduction to the j2_mobile 3D tutorial series, I decided to share my Mobile3D learning process with you. I hope you can discuss Mobile3D together.
  
In the previous tutorial, I gave you a detailed introduction. in Moble3D, you can create a 3D image by performing operations in a timely manner *. A brief introduction is made to the import model in the m3g file. this time I want to use *. the use of the m3g file briefly introduces Mobile3D's animation control and some operations on the model.
  
First of all, let me briefly talk about how to create m3g files. this is actually very simple. you only need to select a 3D graphics production software you are familiar with and install the corresponding plug-ins, the software I use here is Maya and 3DS MAX, and the plug-in uses H3T EXPort Plugin. The plug-in includes maya and 3ds max, and I also need to download a software M3G Tools kit.
  
These connections can be found on Sony EriCSSon's website. It should be okay to install the plug-in. After the plug-in is installed, you can create a model, set the material diagram, set the camera, set the key frame, and so on, and finally output it as a H3T file. Then open the M3G Tools kit and output the h3t file to the m3g file. Before using the m3g file, you 'd better use the M3G Tools kit to view the file, in order to record the corresponding information, such as the userid and the tree structure of the scenario. these are all very important. If you do this, you will find that camera in the exported m3g file is not under the world tree, but on the same level as the world tree.
  
In fact, I also mentioned in the previous article that camera and rendering information can not be placed under the world tree, but all model information must be placed under the tree. When we look at the m3g file, it is important to see the location of the world node and the userid of the model we need to operate on.
  
The preparatory work is almost done. now let's look at Mobile3D. in the previous article, I said that the m3g file uses javax. microedition. m3g. loader. load (String url), and the method returns javax. microedition. m3g. in Object3D arrays, someone may ask why a world is not directly returned since World is the root node?
  
The structure of the m3g file just showed that camera and animation settings were not put in the World node, but at the same level as the World node. The World node is the root node of the scenario. So how can we get the World node correctly? There are two methods:
  
1. traverse the Object3D array and compare the userid of each element. if the useid of the official World node is used, this element is taken out.
  
2. traverse the Object3D array and compare whether each element is an instance of the World class. since the World node is the root node of the scenario, in this Object3D array, there should also be only one instance object of the World class.
  
The first method is relatively simple. here I only provide the snippet code of the second method. (Actually the same)
  
PRivate void loadWorld (){
System. out. println ("now loading ...");
Try {
Buffer = Loader. load ("/img/TmpMicroFile. m3g ");
For (int I = 0; I if (buffer [I] instanceof World ){
World = (World) buffer [I];
Return;
}
}
} Catch (Exception e ){
Buffer = null;
System. out. println ("thorw a exception when loading ");
E. printStackTrace ();
}
}
  
Then we set the camera. The difference is that this time we get the camera information from the World, and then we can make some basic settings for it. Next we will focus on the animation part.
  
When we imported this m3g file, we actually had the animation information, and the animation information may be very complicated. I tested the motion of the two axes of the connecting rod, there is no problem with IK reverse motion from some materials. Maybe you are in a hurry. why not control the playing of the animation?
Simply put, World. animation () to update the animation information. this method needs to be passed into an int type parameter. when you call this method for the first time, the system will record this value, the next call will compare with this value, and the system calculates the position of the animation update for us. at the same time, this method will return an int type parameter, this parameter indicates a recommended value (in milliseconds) for the next update ).
  
At this time, we can let the thread that calls this method sleep at this time, so that the animation data can be updated for the next time. Here you may have questions: my animation only has dozens of frames, but it is played repeatedly. this is good news, but in many cases the length of the video and when it will be played, what should we do by ourselves?
  
At this time, we need to use javax. microedition. m3g. animation controller class. In fact, every animation model that can be moved has its own animation controller object. like model animation, we can use World. find (int controllerID). in this class, we can use setActiveInterval (int activeTime, int unactiveTime) to set the start time and focus time of the animation playing in the system, in addition, the setPosition (int startTime, int endTime) method is used to control which part of the animation needs to be played.
  
Sorry, I didn't use the two methods in this tutorial. but if you want to know how to use these two methods, I suggest you look at the demo of the kangaroo in Demo3D in WTK2.2, which is quite specific. Here I will show the animated and drawn code snippets.
  
Protected void paint (Graphics g ){
  
StartTime = System. currentTimeMillis ()-worldStartTime;
Validity = world. animate (int) startTime );
PerFrameTime = (int) System. currentTimeMillis ();
  
G. setColor (0x00 );
G. fillRect (0, 0, getWidth (), getHeight ());
  
G. setClip (0, 0, getWidth (), getHeight ());
G3d. bindTarget (g, true, Graphics3D. DITHERGraphics3D. TRUE_COLOR );
G3d. setViewport (0, 0, getWidth (), getHeight ());
  
G3d. render (world );
  
G3d. releaseTarget ();
  
FramePor = (int) 1000/(int) System. currentTimeMillis ()-perFrameTime );
  
System. out. println ("3D demo frame/sn:" + framePor );
  
If (validity <1)
{// The validity too small; allow a minimum of 1 ms.
Validity = 1;
}
  
If (validity = 0x7fffffff)
{// The validity is infinite; scehdule a refresh in 1 second.
Validity = 1000;
}
  
}
  
Public void run (){
While (isRun ){
Repaint ();
Try {
Thread. sleep (validity );
} Catch (Exception e ){}
}
}
  
If you do not like the thread, you can change it to Timer. I am familiar with threads, so here I am using threads.
  
It seems that I only understand this, and all of them are shared with everyone. The following describes several methods in the Transformable class. First, let's introduce the Transformable class. The Transformable class is a very important class, and Node is its subclass. you know its importance.
  
There are four Transformable methods: postRotate (float, float) place is related to the rotation of the object. in Mobile3D, by default, the rotation axes of all objects are in the center of the object. Therefore, if you rotate the object, it must be rotated, instead of turning around a certain point or an axis, this 1.1 must be clarified. In this case, we may say that the rotation here only changes the orientation of the object and does not change the position of the object.
  
Now, I understand. some people may ask, "Isn't that exactly what we need? Why should we emphasize it ?" In fact, in some 3D engines, rotation is based on the origin. That is to say, if you need to perform rotation, you must perform operations such as moving, re-rotating, and re-moving. In fact, the advantage of this operation is that the point operation can be convenient, because the point does not have the concept of rotation. To some extent, our Mobile3D cannot operate on the point, so its smallest unit is Mesh. how can it achieve rotation? You can see a matrix.
  
90, 0, 0, 0
0, 0, 0
0, 0, 1, 0
0, 0, 0
  
This matrix indicates that the current model has 90 degrees of rotation on the y axis. Now let's look back at the postRotate (float a, float x, float y, float z) method. There are four parameters. The first one is the angle to be rotated this time, in fact, the last three axes of this rotation are represented by a vector. the postRotate method is to continue the new rotation in the original orientation, if we use a mathematical formula, we use the original matrix multiplied
  
A, 0, 0, 0
0, x, 0, 0
0, 0, y, 0
0, 0, 0, z
  
This matrix is the new direction matrix. If this method sounds hard to understand from a mathematical perspective, you just need to remember that the first of the four parameters is the rotation angle, and the last three determine the rotation axis.
The postRotate method is similar to setOrientation (float a, float x, float y, float z ). The difference is that the orientation is directly set instead of rotating.
  
Let's talk about rotation and let's look at movement. This seems much simpler. translate (float x, float y, float z) means translation based on the specified vector. setTranslation (float x, float y, float z.
  
In fact, there is another way to scale (float sx, float sy, float sz), and setScale (float sx, float sy, float sz) directly sets the scale, these six methods are very useful for direct operations on objects, especially the first four, which are frequently used in game preparation. the last two methods seem to be less frequently used, most of the time, we operate directly on the camera distance (unless there is an extraordinary need ). Here is an example of using the square method.
  
Public void keyPressed (int keycode ){
Float [] camerTra;
Float x;
Float z;
Switch (keycode ){
Case GameCanvas. DOWN:
Break;
Case GameCanvas. UP:
Break;
Case 52:
Dir = dir-2;
System. out. println (dir );
X = (float) (3 * Math. sin (dir * 3.14159f)

The above is the content of the article series 2 in The Beginner's Guide series of j2mmobile 3D. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.