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

📄 450-452.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:Building 3D Applets with App3Dcore</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=12//-->
<!--PAGES=450-452//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="447-450.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="452-455.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">The Overridden Update</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The first thing we do here is let the base class update itself. This is a must, or fMovingObject will not get the chance to update the position and angle.
<DD><B>&#149;</B>&nbsp;&nbsp;The position and velocity are retrieved, since they will both be used.
<DD><B>&#149;</B>&nbsp;&nbsp;Check whether the box has hit the ground by looking at the scale of the model. The scale of the model decides how large the object is. This is because the vertices of the models are defined between -1 and 1 on all axes.
<DD><B>&#149;</B>&nbsp;&nbsp;If the box has hit the ground, invert the speed and multiply it by a factor that controls the bounciness of the box.
<DD><B>&#149;</B>&nbsp;&nbsp;The position of the model is set so that we ensure that the bottom of the box is at ground level.
<DD><B>&#149;</B>&nbsp;&nbsp;The method setPosition() is used to set the new position of the object. This is a must, since getPosition() and getdPosition() return clones. This is to ensure the integrity of the core.
<DD><B>&#149;</B>&nbsp;&nbsp;Outside the <I>if</I> statement, the velocity is affected by the gravity, and the new velocity is set using setdPosition().
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">The initiateClass() Method</FONT></H4>
<P>This method is only called once: at the beginning of the application. This method can be seen as a <I>class</I> constructor, meaning that it prepares a class before creating instances. Let&#146;s look at the advantages gained from using this method. All bouncing boxes have something in common. They are all represented by a polyhedron that has a certain scaling. They all have a certain &#147;bounciness,&#148; too. Instead of hardcoding these constants, we would like to retrieve them when the application starts. All these constants are stored in the HTML (HyperText Markup Language) file as parameters to the applet. This way you can change all &#147;constants&#148; without recompiling the code but by simply restarting the application with another HTML file.</P>
<P>This method will save you LOTS of time once the application is done and only the final tuning is left. When I wrote Asteroids96, a game that is not featured in this book, I spent over a third of my time compiling, looking at the applet for three seconds, then changing a number and recompiling.</P>
<P>All class constants are of the types static and start, with the prefix &#147;our&#148; to emphasize that this is not a class variable but a constant for all instances of this class. Let&#146;s look at what actually takes place in this method:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Retrieve a 3D model from an F3D file. The name of the file is stored as a parameter in the HTML file, as shown below:
<!-- CODE SNIP //-->
<PRE>
&lt;param name=BouncingBox_ourDefaultPolyhedron value=cube.f3d&gt;
</PRE>
<!-- END CODE SNIP //-->
<BR>The parameter name starts with the class name, followed by an underscore and then the name of the constant. The value depends on the parameter type. In this case it is a string that refers to a filename.
<DD><B>&#149;</B>&nbsp;&nbsp;The next thing that is in common for all bouncing boxes is that their 3D model is scaled. This is because all the vertices in an F3D file are between -1 and 1 on all axes. The actual file format and how models can be imported from external applications will be described at the end of the chapter.
<DD><B>&#149;</B>&nbsp;&nbsp;Finally, the &#147;bounciness&#148; of the boxes is retrieved. This bounciness determines how much of the initial energy is saved after a bounce (see Listing 12-2).
</DL>
<P><B>Listing 12-2</B> The BouncingBoxWorld class</P>
<!-- CODE //-->
<PRE>
class BouncingBoxWorld extends fWorld &#123;
   BouncingBoxWorld(Applet app)&#123;
      //-- make a world which is 200x200 meters with the left top corner
      //-- at -100,-100. The world is divided into 5x5 grids
      super(app,-100,-100,200,5);

      //-- insert a couple of boxes at random height
      for(double x=-10;x&lt;=10;x&#43;=10)&#123;
         for(double z=-10;z&lt;=10;z&#43;=10)&#123;
            new BouncingBox(this,new fPoint3d(x,Math.random()*30,z));
         &#125;
      &#125;
   &#125;

   protected void initiateClasses (Applet app) &#123;
      BouncingBox.initiateClass(app);

      gravity=new
Double(app.getParameter("BouncingBoxWorld_gravity")). doubleValue();
   &#125;

   public static double gravity;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">The BouncingBoxWorld Constructor</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The constructor of this class takes a single parameter, the applet that has constructed it.
<DD><B>&#149;</B>&nbsp;&nbsp;The superclass fWorld is constructed by specifying the dimensions of the world.
<DD><B>&#149;</B>&nbsp;&nbsp;A couple of boxes are inserted into the world at random height above the ground.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="447-450.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="452-455.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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