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

📄 435-440.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=435-440//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="433-434.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch12/441-447.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading52"></A><FONT COLOR="#000077">Implementing the Rotating Cubes Applet</FONT></H4>
<P>Most of the code in Listing 11-13 is standard applet code, but some sections might prove a bit more difficult to understand.
</P>
<P><B>Listing 11-13</B> The rotating cubes applet</P>
<!-- CODE //-->
<PRE>
import java.awt.*;
import java.net.*;
import java.io.*;
/**
 * A rotating cubes applet
 * .. putting the classes to work with a quick and dirty
 * applet.
 */
public class Cube extends fNoFlickerApplet implements Runnable&#123;
    fGenericCamera camera;
    fPoint3d CamPos;
    fAngle3d CamAngle;

    fPolyhedron cube;
    fPolyhedronInstance cubeInstance[];
    fPoint3d pos[];
    fAngle3d agl;

    Thread myThread;
   /**
    * initiate the applet.
    */
    public void init()&#123;
      //--
      //-- create a camera
      //--
      camera=new fGenericCamera(400,400,Math.PI/2);
      CamPos=new fPoint3d(0,0,5);
      CamAngle=new fAngle3d();
      //--
     //-- load a model from the file cube.f3d
      //--
        try&#123;
           InputStream is=new URL(getCodeBase(),"cube.f3d").openStream();
           cube=new fConvexPolyhedron(is);
         &#125; catch(Exception e)&#123;e.printStackTrace();&#125;

      //-- create 9 instances of the cube
   cubeInstance=new fPolyhedronInstance[9];
       for(int n=0; n&lt;9; n&#43;&#43;)&#123;
       cubeInstance[n]=new fPolyhedronInstance(cube);
        &#125;

      //--
      //-- create the positions and angle
        //--
       pos=new fPoint3d[9];
       int n=0;
       for(int y=-5; y&lt;=5; y&#43;=5)&#123;
          for(int x=-5; x&lt;=5; x&#43;=5)&#123;
             pos[n]=new fPoint3d(x,y,0);
             n&#43;&#43;;
          &#125;
       &#125;
        agl=new fAngle3d();
       //--
       //-- start the thread
      //--
        myThread=new Thread(this);
        myThread.start();
    &#125;

    public void run()&#123;
        while(true)&#123;
           //--
           //-- sleep 1/10 of a second
           //--
            try &#123;
               myThread.sleep(100);
            &#125; catch ( InterruptedException e) &#123;&#125;

            //--
            //-- update the angle of the models
            //--
            agl.x&#43;=Math.PI/20; agl.y&#43;=Math.PI/30;

           //--
          //-- update camera angle and position
           //--
          CamPos.z&#43;=0.2; CamAngle.z&#43;=Math.PI/50;
          camera.setOrientation(CamPos,CamAngle);

          //--
          //-- request a repaint
          //--
             repaint();
        &#125;
    &#125;
    public void start()&#123;
        if(myThread==null)&#123;
           myThread=new Thread(this);
           myThread.start();
       &#125;
       &#125;
    public void stop()&#123;
        if(myThread!=null)&#123;
           myThread.stop();
           myThread=null;
       &#125;
    &#125;

     public void paint(Graphics g)&#123;
       //-- clear screen
       g.clearRect(0,0,size().width,size().height);

       //--
       //-- paint the models
       //--
       for(int n=0; n&lt;9; n&#43;&#43;)&#123;
           cubeInstance[n].setOrientation(pos[n],agl);
           cubeInstance[n].paint(g,camera);
       &#125;
    &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading53"></A><FONT COLOR="#000077">Initiating the Applet</FONT></H4>
<P>Initiating this applet consists of four steps:
</P>
<DL>
<DD><B>1.</B>&nbsp;&nbsp;Construct a camera with a view angle of 90 degrees. This is about three times as large as a handy-cam&#146;s view angle.
<DD><B>2.</B>&nbsp;&nbsp;Load the model of the cube from the file cube.f3d and construct nine instances.
<DD><B>3.</B>&nbsp;&nbsp;Create an array of coordinates containing the positions of the models with respect to WCS.
<DD><B>4.</B>&nbsp;&nbsp;Start a thread.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading54"></A><FONT COLOR="#000077">The run() Method</FONT></H4>
<P>The position and orientation of the camera and models are updated. Then a repaint is requested.
</P>
<H3><A NAME="Heading55"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;At this point you could start experimenting with different models. Find an .f3d file on the CD and change the line in the code that loads the &#147;cube.f3d&#148; to the model of your choice.
<DD><B>&#149;</B>&nbsp;&nbsp;One other thing you could do right away is to implement some more complex motion pattern into the camera. Place the cubes at &#147;ground&#148; level and circle about them, for example.
<DD><B>&#149;</B>&nbsp;&nbsp;Another thing could be to extend the indexing polygon class with a wire frame polygon. It will be almost the same as the filled polygon class except for the paint method, which must be changed to actually draw a polygon instead of filling it.
</DL>
<H3><A NAME="Heading56"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>As you have seen, the process of designing even a small fraction of a 3D engine can be pretty tricky. And the sad part is that there are still some complex issues that need to be taken care of, like 2D, 3D clipping, polygon shading, visible objects determination, and so on. The list goes on and on. And these topics pertain only to the visual parts of a 3D engine. The nonvisual part is just as important and covers issues such as collision detection, construction of virtual worlds, creating objects with behaviors, and so on.
</P>
<P>In this chapter you have learned some basic 3D programming. Abstraction, one of the most important parts of object-oriented programming, was also illustrated. With the knowledge acquired in this chapter, you could build your own 3D Java engine. You must, however, consult other literature for the issues not covered here. Try <I>Black Art of 3D Programming</I> if you are especially interested in 3D graphics.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="433-434.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch12/441-447.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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