3D basics for flash cs4 and later versions (as3.0)

Source: Internet
Author: User

I haven't updated it for a while. I 've been thinking about this 3D problem.

Many classes have been added since the flash of cs4, such as the 3D class to be introduced, which is the same as the basic idea of 3D. But I want to propose it separately. I think it is quite necessary.

   

(The cs4 version is no longer used as the environment below)

 

   Review the formula in chapter 15 of forwarding:

Basic
Scale = fL/(FL + zpos );
Sprite. scalex = Sprite. scaley = scale;
Sprite. Alpha = scale ;//Optional
Sprite. x = vanishingpointx + xpos * scale;
Sprite. Y = vanishingpointy + ypos * scale;

 

ZSort:
//Suppose there is an array of 3D objects with the zpos attribute
Objectarray. sorton ("zpos", array. Descending | array. Numeric );
For (var I: uint = 0; I <numobjects; I ++ ){
   Setchildindex (objectarray [I], I );
}
 

 Coordinate rotation:
X1 = cos (anglez) * xpos-sin (anglez) * ypos;
Y1 = cos (anglez) * ypos + sin (anglez) * xpos;
X1 = cos (angley) * xpos-sin (angley) * zpos;
Z1 = cos (angley) * zpos + sin (angley) * xpos;
Y1 = cos (anglex) * ypos-sin (anglex) * zpos;
Z1 = cos (anglex) * zpos + sin (anglex) * ypos;
 
3DDistance:
Dist = math. SQRT (dx * dx + dy * dy + DZ * Dz );

It seems quite troublesome. Do you think it is necessary to have a z-column attribute. This attribute is added now.

The

Scale = fL/(FL + zpos );
Sprite. scalex = Sprite. scaley = scale;
Sprite. Alpha = scale ;//Optional
In fact, it is equivalent to the current Z,

The usage is the same as that of X and Y, for example, Sprite. z = 100;

Review coordinate representation:

 

 

Figure 15-1Right Coordinate System

Figure 15-2Left-hand Coordinate System

After testing, the zcoordinate here uses the right-hand coordinate system, that is, the inner is + Z, and the outward is-Z. Isn't it too abstract?

Use the perspective effect of the previous forwarding to modify the following:

Package {
 Import flash. display. Sprite;
 Import flash. Events. event;
 Import flash. Events. keyboardevent;
 Import flash. UI. keyboard;
 Public class sampledemo extends sprite {
  Private var ball: ball;
  Private var xpos: Number = 0;
  Private var ypos: Number = 0;
  Private var zpos: Number = 0;
  Private var vpx: Number = stage. stagewidth/2;
  Private var vpy: Number = stage. stageheight/2;
  Public Function sampledemo (){
   Init ();
  }
  Private function Init (): void {
   Ball = new ball ();
   Addchild (ball );
   Addeventlistener (event. enter_frame, onenterframe );
   Stage. addeventlistener (keyboardevent. key_down, onkeydown );
  }
  Private function onenterframe (Event: Event): void {
   Xpos = mousex-vpx;
   Ypos = mousey-vpy;
   Ball. z = zpos;
   Ball. x = vpx + xpos;
   Ball. Y = vpy + ypos;
  }
  Private function onkeydown (Event: keyboardevent): void {
   If (event. keycode = keyboard. Up ){
    Zpos + = 5;
   } Else if (event. keycode = keyboard. Down ){
    Zpos-= 5;
   }
  }
 }
}

The ball here can be replaced with any other component. In fact, it is to replace the Z-column representation with Z, which saves a lot of code.

There is a FL volume in the previous forwarding, which can be seen as a distance from 0 to-Z in Z. It can also be set to a constant, which is used to be set to 250, but is used in the right hand coordinate system, so fl =-250. This variable can still act as the visible value of an object. If it is smaller than this variable, the object will not be visible. This is more realistic.

For example, add the code above:

Private function onenterframe (Event: Event): void {
 If (zpos <FL ){
  Xpos = mousex-vpx;
   Ypos = mousey-vpy;
   Ball. z = zpos;
   Ball. x = vpx + xpos;
   Ball. Y = vpy + ypos;
 Ball. Visible = true;
 } Else {
 Ball. Visible = false;
 }
}

This will disappear when the screen is exceeded.

 

You can add acceleration on the Z column, or set a range for rebound and other exercises to see the effect.

There are also many examples in the last 15 chapters. You can use Z with slight modifications.

 

The Z-column sorting problem can also be recorded through arrays, because it is easier to control sorting with specific values.

 

Now we have not only added zcoordinates, but also added the rotation attribute of coordinates. With the help, we only talk about rotationx, rotationy,

TherotationXAttribute. This attribute is the Rotation Angle of the target object around the X axis relative to its original direction.

TherotationYAttribute. This attribute is the Rotation Angle of the target object around the Y axis relative to its original direction.

In fact, the Z column also has this property rotationz, which is used in the same way as the coordinate z.

Example: SPRITE. rotationz = 30;

 

I think so much about it now. If you have any questions, continue to supplement it ~~

 

 

(If You Want To reprint it, please specify the source at http://blog.sina.com.cn/jooi. Thank you)

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.