⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 414-419.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Into the Third Dimension</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=11//-->
<!--PAGES=414-419//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="410-414.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="419-423.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading33"></A><FONT COLOR="#000077">The World Coordinate System (WCS)</FONT></H4>
<P>The house that the architect is designing will eventually be built in a small town. This small town has decided that it needs to have its own coordinate system, and that the origin is in the middle of the town hall, with the x-axis going east and y-axis going north, as in Figure 11-13. Let&#146;s call this the world coordinate system (WCS). The house will be built at an exact coordinate in the WCS. The building will also be scaled, because nobody wants to live in a 10-inch by 20-inch house.
</P>
<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/11-14.jpg',600,410 )"><IMG SRC="images/11-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-14.jpg',600,410)"><FONT COLOR="#000077"><B>Figure 11-13</B></FONT></A>&nbsp;&nbsp;The building placed in the WCS</P>
<P>One day the architect gets the idea to calculate the exact coordinates that the vertices of his model will have in the real world. He knows the exact position where his house will be built and also the angle with respect to the town hall. So he enters the magic world of math. He takes his blueprint to the town hall and places it so that the axis in the MCS coincides with the axis in the WCS. He then recalculates the vertices by first scaling them so that the house will have the same dimensions as in the real world. Next he rotates the vertices by the same angle as the house will be rotated with respect to the WCS. Then he translates the vertices to the position where the house will be built. The coordinates resulting from these calculations are saved, and he waits until the construction work is done. He then checks the calculated vertices and measures the exact coordinates of each corner in the real house. Behold, they are exactly identical. What he does is in fact a series of transforms as discussed in Appendix E, 3D Transforms. At this point you should be acquainted with matrix operation. If you aren&#146;t, just take the results for given. Figure 11-14 shows the results of the architect&#146;s calculations.
</P>
<P><A NAME="Fig15"></A><A HREF="javascript:displayWindow('images/11-15.jpg',141,20 )"><IMG SRC="images/11-15t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-15.jpg',141,20)"><FONT COLOR="#000077"><B>Figure 11-14</B></FONT></A>&nbsp;&nbsp;The town populated by 3D models, each with its own MCS</P>
<P>Let&#146;s pick one single vertex in the MCS and call it <I>v</I>, and then transform it from MCS to WCS. Mathematically this can be expressed using matrix operations in the following way (the matrixes can be found in Appendix E, 3D Transforms):</P>
<P ALIGN="CENTER"><IMG SRC="images/11-01d.jpg"></P>
<P>Using the generic matrix class implemented in Appendix E, this series of transforms can be coded like this:
</P>
<!-- CODE SNIP //-->
<PRE>
//-- transform the vertices from MCS to WCS
matrix.makeIdentity();          //-- make the identity matrix
matrix.concatS(Sx,Sy,Sz);       //-- scaling
matrix.concatRx(Ax);            //-- rotate about X-axis
matrix.concatRy(Ay);            //-- rotate about Y-axis
matrix.concatRz(Az);            //-- rotate about Z-axis
matrix.concatT(Xpos,Ypos,Zpos); //-- translate
matrix.transform(Vm,Vw);        //-- transform points
</PRE>
<!-- END CODE SNIP //-->
<P>This series of transforms can be called the MCS to WCS transform. Since this is a tedious procedure, we will expand the generic matrix and hide these transforms in a convenient method called makeMCStoWCStransform(&#133;). This way all the math will be hidden and make the code into a math-free zone.
</P>
<P>After our architect discovers that he can mathematically transform vertices from MCS to WCS, he gets all carried away and wonders how his house would look if somebody took a picture of it from an arbitrary angle and position. He knows the position of the camera in WCS and also its orientation (angle). Figure 11-15 illustrates this.</P>
<P><A NAME="Fig16"></A><A HREF="javascript:displayWindow('images/11-16.jpg',600,406 )"><IMG SRC="images/11-16t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-16.jpg',600,406)"><FONT COLOR="#000077"><B>Figure 11-15</B></FONT></A>&nbsp;&nbsp;The camera has a position and angle in the world</P>
<H4 ALIGN="LEFT"><A NAME="Heading34"></A><FONT COLOR="#000077">The View Coordinate System (VCS)</FONT></H4>
<P>Since the camera is now the center of attention, we could think of its position as the center of the universe. The direction of the principal axes depends on the angle of the camera. Suppose that the architect has all the coordinates defined in WCS. The same vertices will not have the same coordinates in the VCS. This is realized by comparing a vertex with the principal axes of WCS and VCS. What he needs to do is to somehow calculate the coordinates of the vertices with respect to the VCS as in Figure 11-16.
</P>
<P><A NAME="Fig17"></A><A HREF="javascript:displayWindow('images/11-17.jpg',600,410 )"><IMG SRC="images/11-17t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-17.jpg',600,410)"><FONT COLOR="#000077"><B>Figure 11-16</B></FONT></A>&nbsp;&nbsp;Vertices have different coordinates in VCS than in WCS</P>
<P>This transformation is not as intuitive as the MCS to WCS transform but is actually closely related to it. Mathematically the matrix can be calculated by first making the MCS to WCS matrix and then calculating its inverse. This would, however, take a massive amount of calculations, so we must find a simpler method.
</P>
<P>The inverse can be constructed using the generic 3D matrix with the following code:</P>
<!-- CODE SNIP //-->
<PRE>
//-- make a matrix that transforms a vertex from WCS to VCS
matrix.makeIdentity();
matrix.concatT(-Xpos,-Ypos,-Zpos);
matrix.concatRz(-Az);
matrix.concatRy(-Ay);
matrix.concatRx(-Ax);
//-- transform the vertices from WCS to VCS
matrix.transform(Vw,Vm);
</PRE>
<!-- END CODE SNIP //-->
<P>These operations will be &#147;hidden&#148; inside a method called makeWCStoVCStransform(..) in the extended generic matrix. The implementation will be shown later. Just keep in mind that you don&#146;t necessarily need to know the math behind these transforms to use them.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="410-414.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="419-423.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -