📄 519-521.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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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’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’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 {
// 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 {
cos_table = new float[TABLE_SIZE];
sin_table = new float[TABLE_SIZE];
double temp;
for (int i=0; i<TABLE_SIZE; i++) {
temp = DEG_TO_RAD*(double)i;
cos_table[i] = (float)Math.cos(temp);
sin_table[i] = (float)Math.sin(temp);
}
}
/////////////////////////////////////////////////////////////////
// return value of cos from lookup table
/////////////////////////////////////////////////////////////////
public static float cos(int degree) {
if (degree >= 360) {
degree = degree % 360;
}
else if (degree < 0) {
degree = (-degree)%360;
}
return cos_table[degree];
}
/////////////////////////////////////////////////////////////////
// return value of sin from lookup table
/////////////////////////////////////////////////////////////////
public static float sin(int degree) {
if (degree >= 360) {
degree = degree % 360;
}
else if (degree < 0) {
degree = (-degree)%360;
}
return sin_table[degree];
}
/////////////////////////////////////////////////////////////////
// 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) {
double t= Math.atan2((double) v1y, (double)v1x);
t *= RAD_TO_DEG;
if (t < 0.0) t += 360.0;
return (double)t;
}
/////////////////////////////////////////////////////////////////
// compute magnitude of a vector
/////////////////////////////////////////////////////////////////
public static float computeMagnitude(int v1x,int v1y) {
return (float)Math.sqrt((double)(v1x*v1x+v1y*v1y));
}
/////////////////////////////////////////////////////////////////
// return random numbers of specified type between 0 and Max
/////////////////////////////////////////////////////////////////
public static int getRand(int Max) {
return (int)(Math.random() * Max);
}
public static float getRand(float Max) {
return (float)(Math.random() * Max);
}
public static double getRand(double Max) {
return (Math.random() * Max);
}
}
</PRE>
<!-- END CODE //-->
<P>Now let’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 + -