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

📄 519-521.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 the JAVAroids Game</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=13//-->
<!--PAGES=519-521//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="514-519.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="522-526.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>These mathematical operations occur so often in this game that it makes sense to define a helper class, GameMath, that encapsulates useful routines. GameMath is declared a <I>final</I> class, which means it can&#146;t be subclassed. GameMath includes conversion factors between degrees and radians, as well as a lookup table for sine and cosine. The lookup tables are used to reduce computation during game play, and they&#146;re computed with a static initializer. (A static initializer initializes static variables when the class is loaded.) Listing 13-4 shows the definition of the GameMath class.</P>
<P><B>Listing 13-4</B> GameMath class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// the GameMath class contains mathematical routines for our
// graphics and games programs
//
/////////////////////////////////////////////////////////////////

  public final class GameMath &#123;
    // constants
    static final float DEG_TO_RAD = (2.0f*3.14159f)/360.0f;
    static final double RAD_TO_DEG = 1.0/DEG_TO_RAD;

    static final int TABLE_SIZE = 360;

    // storage for lookup tables
    static float cos_table[];
    static float sin_table[];

/////////////////////////////////////////////////////////////////
// initialize cos and sin tables
/////////////////////////////////////////////////////////////////

    static &#123;
       cos_table  = new float[TABLE_SIZE];
       sin_table  = new float[TABLE_SIZE];

       double temp;
       for (int i=0; i&lt;TABLE_SIZE; i&#43;&#43;) &#123;
         temp = DEG_TO_RAD*(double)i;
         cos_table[i] = (float)Math.cos(temp);
         sin_table[i] = (float)Math.sin(temp);
       &#125;

    &#125;

/////////////////////////////////////////////////////////////////
// return value of cos from lookup table
/////////////////////////////////////////////////////////////////

    public static float cos(int degree) &#123;
        if (degree &gt;= 360) &#123;
         degree = degree % 360;
       &#125;
       else if (degree &lt; 0) &#123;
         degree = (-degree)%360;
       &#125;

        return cos_table[degree];
    &#125;

/////////////////////////////////////////////////////////////////
// return value of sin from lookup table
/////////////////////////////////////////////////////////////////

    public static float sin(int degree) &#123;
        if (degree &gt;= 360) &#123;
         degree = degree % 360;
       &#125;
       else if (degree &lt; 0) &#123;
         degree = (-degree)%360;
       &#125;
       return sin_table[degree];
    &#125;
/////////////////////////////////////////////////////////////////
// computes angle, in degrees, from the x-axis
// returns angle in degrees between 0.0 and 360.0
// where 90.0 degrees is the y-axis.
/////////////////////////////////////////////////////////////////

  public static double computeAngle(int v1x,int v1y) &#123;
    double t= Math.atan2((double) v1y, (double)v1x);
    t *= RAD_TO_DEG;
    if (t &lt; 0.0) t &#43;= 360.0;
    return (double)t;
  &#125;

/////////////////////////////////////////////////////////////////
// compute magnitude of a vector
/////////////////////////////////////////////////////////////////

    public static float computeMagnitude(int v1x,int v1y) &#123;
      return (float)Math.sqrt((double)(v1x*v1x&#43;v1y*v1y));
    &#125;

/////////////////////////////////////////////////////////////////
// return random numbers of specified type between 0 and Max
/////////////////////////////////////////////////////////////////

    public static int getRand(int Max) &#123;
      return (int)(Math.random() * Max);
    &#125;

    public static float getRand(float Max) &#123;
      return (float)(Math.random() * Max);

    &#125;

    public static double getRand(double Max) &#123;
      return (Math.random() * Max);

    &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>Now let&#146;s implement moving and rotating polygons!
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="514-519.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="522-526.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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