📄 447-450.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=447-450//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="441-447.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="450-452.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Understanding the 3D Engine in the Core</FONT></H4>
<P>The 3D engine is a subsystem to the core. It is responsible for rendering the world as seen through a camera. As weird as it may sound, the world can be represented in any way you like. You can use a 2D engine and make a 2D representation, if you like. You can also skip this stage altogether and settle for watching lots of numbers in a console window, but that would be kind of boring.
</P>
<P>The 3D representation is done using three different cameras: a basic static camera, a ground-faking camera, and a tracking camera that can be hooked to an object.</P>
<P>The static camera, fCamera, is a regular camera that has a position and an orientation that can be changed at will. The ground-faking camera, fMagicCamera, can fake ground by drawing a horizon and a grid at ground level, giving a vague impression that there is a ground. The tracking camera, fTrackerCamera, can be hooked to an object. This camera will try to follow the hooked object. By follow, I don’t mean look at it, but try to keep up with its position and orientation. This is similar to the spotting camera seen in all flight simulators.</P>
<P>All cameras have a method paint(). When this method is called, they will render the world by first determining which objects are visible and then calling the paint() method on each visible fObject. This method is propagated to the fPolyhedronInstance, where the actual painting takes place.</P>
<P>The cameras are not part of the virtual world but are only used as a tool to view it. You can place them in the world or hook them to objects, and then at regular time intervals tell them to paint the world as they see it. You can insert an arbitrary number of cameras in your applet that view the world from different positions and angles. This will have a major impact on the performance, though.</P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Building an Application on Top of the Core</FONT></H3>
<P>Now that you know a little about the core, we can start looking at how to use it. The best way to learn something is by looking at examples. Below are two examples that show some of the things that you can do.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">A Small Example: The Bouncing Boxes</FONT></H4>
<P>In the following example you will see how you can use the core by extending the cornerstone classes. You should read the description and look at the code at the same time to get the most out of it. The “headers” for the core classes mentioned can be found on the CD-ROM.
</P>
<P>We will first extend the generic fMovingObject with a BouncingBox object. After that the fWorld class will be extended by the BouncingBoxWorld. Finally, these classes will be put together in an applet.</P>
<P>We will start by looking at how the generic core class fMovingObject can be extended to make a bouncing box, as shown in Listing 12-1 and Figure 12-3.</P>
<P><B>Listing 12-1</B> The bouncing box</P>
<!-- CODE //-->
<PRE>
class BouncingBox extends fMovingObject{
BouncingBox(fWorld w, fPoint3d p ) {
//-- construct the base class
super(w,p,new fAngle3d(0,0,0),
new fPoint3d(0,0,0),new fAngle3d(0,Math.PI,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));
}
//-- override the update method
public void update (double dt) {
//-- always let the base class do the default action for
//-- an overridden method. Never forget this!
super.update(dt);
//-- retrieve the position and velocity of the box
fPoint3d dp=getdPosition();
fPoint3d p=getPosition();
//-- check if the box has hit the ground
if(p.y<ourScale.y){
//-- the bottom of the box has hit the ground
//-- switch the sign of the velocity and
//-- decrease it a bit
dp.y=-dp.y*ourBounciness;
//-- make sure the box is not below ground level
p.y=ourScale.y;
//-- set the new position
setPosition(p);
}
//-- make sure gravity does it’s share
dp.y-=BouncingBoxWorld.gravity*dt;
//-- set the new velocity
setdPosition(dp);
}
//-- inititates the class
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("BouncingBox_ourDefaultPolyhedron");
InputStream is=url.openStream();
ourDefaultPolyhedron=new fConvexPolyhedron(is);
} catch(Exception e) {
e.printStackTrace();
}
//-- the scaling of the polyhedron
double xscale=new
Double(app.getParameter("BouncingBox_scalex")).doubleValue();
double yscale=new
Double(app.getParameter("BouncingBox_scaley")).doubleValue();
double zscale=new
Double(app.getParameter("BouncingBox_scalez")).doubleValue();
ourScale=new fPoint3d(xscale,yscale,zscale);
//-- the bounciness of this box
ourBounciness=new
Double(app.getParameter("BouncingBox_bounciness")).doubleValue();
}
//-- class constants
protected static fPolyhedron ourDefaultPolyhedron;
protected static double ourBounciness;
protected static fPoint3d ourScale;
}
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/12-03.jpg',407,469 )"><IMG SRC="images/12-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-03.jpg',407,469)"><FONT COLOR="#000077"><B>Figure 12-3</B></FONT></A> The BouncingBox class</P>
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">The BouncingBox Constructor</FONT></H4>
<DL>
<DD><B>•</B> The constructor of a bouncing box takes two parameters: the world in which it should be inserted and the starting position. The base class, fMovingObject, is constructed by setting the velocity and angle to zero. The angular velocity is set to PI, making the moving object rotate about the y-axis with an angular velocity of PI rad/s.
<DD><B>•</B> The next line in the constructor tells the base class fObject which polyhedron instance to use. Without a polyhedron instance, the object cannot be painted by a camera.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="441-447.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="450-452.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -