[J2me3d series study Article 3] (instant mode) transform cubes-rotation, scaling, and translation

Source: Internet
Author: User


 

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/j2me-kjava/262.html


 

This chapter describes how to construct a simple cube and set the color of the cube.

 

In the previous article, I also emphasized twoThe difference between the transform object is not mentioned here. The operation cube is passed in during rendering.Transform objectThe parameter changes its status, position, and size. Next, paste a piece of code that is processed in the key, and then comment and analyze it. First, we will introduce three methods;

  • Transform. postscale (float Sx, float Sy, float sz): scales 3D objects in the X, Y, and Z directions. Values greater than 1 will expand the object according to the given factor; values between 0 and 1 will contract the object. If the value is negative, the scaling and image operations are performed simultaneously.
  • Transform. posttranslate (float Tx, float ty, float Tz): Move a 3D object by adding a specified value to the X, Y, and Z coordinates. A negative value indicates moving the object to the negative axis.
  • Transform. postrotate (float angle, float ax, float ay, float az): Rotate objects through (0, 0, 0) and (ax, ay, AZ) axes at the given angle. If the angle is positive, it indicates that if you observe in the direction of the rotating axis, the object is rotated clockwise. For example, postrotate (30, 1, 0, 0) will rotate the object 30 degrees around the X axis.

All operation names start with "Post", indicating that the current transform object is multiplied by the given conversion matrix from the right-the sequence of matrix operations is very important. If you rotate 90 degrees to the right, and then take two steps, then your location is obviously different from the first two steps and then turn around. You can call two post Methods postrotate () and posttranslate () after each walk command to obtain the preceding walk command. The Call Sequence determines the obtained walking instruction. Because the multiplication is used, the final conversion you use will be applied first.

M3G has a transform class and a transformable interface. All Quick Mode APIs can use a transform object as a parameter to modify the associated 3D object. In addition, in the retention mode, use the transformable interface to convert nodes that are part of the 3D world. The subsequent articles will learn more.

Private int XYZ; // axis that identifies the current operation <br/> private finale int state_roate = 0, state_scale, state_translate; <br/> private int state <br/> Public void keypressed (INT key) {<br/> If (Key = key_num7) // key 7 indicates changing the X of the current operation? Y? Z? Axis <br/> XYZ ++; <br/> If (XYZ> 2) <br/> XYZ = 0; <br/> else if (Key = key_num9) <br/> XYZ --; <br/> If (XYZ <0) <br/> XYZ = 2; <br/> If (Key = key_num1) // press Key 1 to indicate the operation (rotation, scaling, or translation) to be performed. <br/> state ++; <br/> If (State> 2) <br/> state = 0; <br/> else if (Key = key_num3) <br/> state --; <br/> If (State <0) <br/> state = 2; <br/> float angle = 0; // angle; <br/> If (Key =-1 | key =-2 | key =-3 | key =-4) {<br/> switch (state) {<br/> case state_roate: // in the rotation operation, if angle = negative, the rotation is counter-clockwise, positive and vice versa <br/> If (Key =-1 | key =-3) <br/> angle =-10; <br/> else if (Key =-2 | key =-4) <br/> angle = 10; <br/> If (XYZ = 0) <br/> tf_m.postrotate (angle, 1, 0, 0); <br/> else if (XYZ = 1) <br/> tf_m.postrotate (angle, 0, 1, 0); <br/> else if (XYZ = 2) <br/> tf_m.postrotate (angle, 0, 0, 1); <br/> break; <br/> case state_scale: // under the scaling operation, angle> 1 identifies the method, <1 indicates downgrading the logo <br/> If (Key =-1 | key =-3) <br/> angle = 0.8f; <br/> else if (Key =-2 | key =-4) <br/> angle = 1.2f; <br/> If (XYZ = 0) <br/> tf_m.postscale (angle, 1, 1); <br/> else if (XYZ = 1) <br/> tf_m.postscale (1, angle, 1 ); <br/> else if (XYZ = 2) <br/> tf_m.postscale (1, 1, angle); <br/> break; <br/> case state_translate: // In the translation operation, if angle = negative, the translation is clockwise. If (Key =-1 | key =-3) <br/> angle =-1; <br/> else if (Key =-2 | key =-4) <br/> angle = 1; <br/> If (XYZ = 0) <br/> tf_m.posttranslate (angle, 0, 0); <br/> else if (XYZ = 1) <br/> tf_m.posttranslate (0, angle, 0); <br/> else if (XYZ = 2) <br/> tf_m.posttranslate (0, 0, angle ); <br/> break; <br/>}< br/>

The code is simple and easy to understand.

In order to give some students a better understanding, I will give a representative description of remark 1;

When we perform a change operation on the cube, the first step is to know which axis is used for the operation, so press the key to determine the defined xyz, XYZ = 1 indicates the X axis, and so on.

Step 2: After determining which axis is used for the operation, start to determine what operation we want to perform on it. Note 1 is in the state_roate State, it must be a rotating operation. Next Step 3: assign a value to the Angle Parameter corresponding to the operation. Note 1 here angle = 10, which means that the rotation angle is 10. Before the value assignment, you can determine the buttons, when-1-3 (corresponding to the upper and left directions of the simulator), it rotates counter-clockwise. When-2-4 (corresponding to the lower and right directions of the simulator), it rotates clockwise; in fact, that is, whether it is "+" or "-" before the rotation angle 10! Step 4: Change the operation method postrotate () the first parameter is the angle, and the second to fourth parameters also indicate which axis to rotate. Here the input is, 0, it also corresponds to XYZ = 1 and operates on the X axis!

 

Of course, I may also describe what I understand. If I still don't understand it, put the code into the project and run it, and manually modify the angle, it is easy to understand the running effect by changing the operation parameter values.

 

(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)

 

2010/12/9 0: 23

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.