📄 455-458.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, 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=12//-->
<!--PAGES=455-458//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="452-455.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="458-461.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Building on the Example: Collisions and Object Interactions</FONT></H4>
<P>The last example illustrated how the core class fMovingObject and fWorld can be extended. This example (see Figure 12-4) will show collision handling, extending fObject, and removing an object from the world.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/12-04.jpg',405,468 )"><IMG SRC="images/12-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-04.jpg',405,468)"><FONT COLOR="#000077"><B>Figure 12-4</B></FONT></A> Collisions in the bouncing boxes example </P>
<P>We will start by extending the core class fObject with StaticBox, shown in Listing 12-5.
</P>
<P><B>Listing 12-5</B> The static box</P>
<!-- CODE //-->
<PRE>
class StaticBox extends fObject{
StaticBox(fWorld w, fPoint3d p ) {
//-- construct the base class by specifying position and angle
super(w,p,new fAngle3d(0,0,0));
//--
//-- every non-abstract object MUST have a polyhedron instance.
//-- this line of code MUST be somewhere in the constructor
//--
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
}
public void update (double dt) {
super.update(dt);
if(getAge()>ourLifeTime){
die();
}
}
public static void initiateClass (Applet app) {
//--
//-- get the static "constants" that all bouncing boxes have
//--
try{
//-- load the default polyhedron for all bouncing boxes
//-- this MUST be done in all non-abstract classes
String polyfile=
app.getParameter("StaticBox_ourDefaultPolyhedron");
URL url=new URL(app.getCodeBase(),polyfile);
InputStream is=url.openStream();
ourDefaultPolyhedron=new fConvexPolyhedron(is);
} catch(Exception e) {
e.printStackTrace();
}
//-- the scaling of the polyhedron
double xscale=new
Double(app.getParameter("StaticBox_scalex")).doubleValue();
double yscale=new
Double(app.getParameter("StaticBox_scaley")).doubleValue();
double zscale=new
Double(app.getParameter("StaticBox_scalez")).doubleValue();
ourScale=new fPoint3d(xscale,yscale,zscale);
ourLifeTime=new
Double(app.getParameter("StaticBox_ourLifeTime")).doubleValue();
}
//-- class constants
protected static fPolyhedron ourDefaultPolyhedron;
protected static fPoint3d ourScale;
protected static double ourLifeTime;
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The StaticBox constructor is similar to the BouncingBox’s constructor except that this class has fObject as a base class. The base class is constructed by specifying its position and angle.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>This object doesn’t do anything except age. When its lifetime has been exceeded, it will remove itself from the world with the die() method.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Collision Handling</FONT></H4>
<P>Listing 12-6 shows the bouncing box with collision handling. To conserve space, only the changes to Listing 12-1 are shown. The complete source code can be found on the CD-ROM.
</P>
<P><B>Listing 12-6</B> The bouncing box, with collision handling</P>
<!-- CODE //-->
<PRE>
class BouncingBox extends fMovingObject{
..
..
public boolean interestedOfCollisionWith (fObject obj){
//-- i'm interested in collisions with static boxes
if(obj instanceof StaticBox) return true;
//-- none of the objects I know, let the base class decide
return super.interestedOfCollisionWith (obj);
}
protected boolean handleCollisionWith (fObject obj,double dt){
//-- if it is one of my "interest" objects
if(obj instanceof StaticBox){
//-- retrieve the position and velocity of the box
fPoint3d dp=getdPosition();
dp.y=-dp.y;
//-- make sure gravity does it's share
dp.y-=BouncingBoxWorld.gravity*dt;
//-- set the new velocity
setdPosition(dp);
//-- event has been handled, get more events
return true;
}
//-- if I don't know what this object is then it
//-- means that a super class is interested.
//-- let it take care of it
return super.handleCollisionWith(obj,dt);
}
..
..
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">The interestedOfCollisionWith() Method</FONT></H4>
<P>Collision detection is an expensive operation. In order to optimize the core, one has to be very selective about which collisions objects should be interested in. Say that an object of type LargeTruck has collided with an object of type Maggot. From the large truck’s point of view, this is not an important event, but for the maggot this might be a turning point in its life. To express interest in a collision, an object has to override this method. In this case a BouncingBox is interested in collisions with all objects of the type StaticBox. If it doesn’t recognize the object type or it is not interested, it will let its base class make the decision. The base class in this case is fObject, and since this is a core object, it is very ignorant and will return <I>false</I>.</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">The handleCollisionWith() Method</FONT></H4>
<P>When a collision with an object of interest has occurred, this method will be called. In this case a collision with a StaticBox will result in a bounce.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="452-455.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="458-461.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -