// (C) 2001 SyGem Software import java.awt.*; import java.applet.*; import com.sygem.jazz3d3.*; import com.sygem.jazz3d3.primitive.Sphere3d; import com.sygem.jazz3d3.primitive.Line3d; import com.sygem.jazz3d3.primitive.Landscape3d; public class landscapeFollow extends Applet implements Runnable { Thread m_ce; World myWorld; int light1; int sphereID,landID,rayID; final static int landscapeSize = 6; //Construct the applet public landscapeFollow() { } //Initialize the applet public void init() { myWorld = new World(this); setLayout(new BorderLayout()); add("Center",myWorld); RenderSolid tex = new RenderSolid(); tex.setDrawingMode(Render.GOURAUD); Sphere3d sp1 = new Sphere3d(10,10,0,1,8); sp1.scaleObject(0.2,0.2,0.2); Line3d collisionRay = new Line3d(0,1,8,0,0.8,8); rayID = sp1.addChild(collisionRay,0,0,0); sp1.setColour(255,0,0); sp1.setRenderer(tex); sphereID = myWorld.addObject(sp1); Landscape3d sp2 = new Landscape3d(20012001,0,0,8); sp2.fractalize(); sp2.fractalize(); sp2.fractalize(); sp2.fractalize(); sp2.smooth(); sp2.generateShape(); sp2.scaleObject(landscapeSize,1,landscapeSize); sp2.setRenderer(tex); landID = myWorld.addObject(sp2); Light l1 = new Light(0,0,1); myWorld.addLight(l1); myWorld.getCamera().translateWorld(0,6,-2); myWorld.getCamera().pointAt(myWorld.getObject(landID)); } //Start the applet public void start() { if (m_ce == null) { m_ce = new Thread(this); m_ce.start(); } } //Stop the applet public void stop() { if (m_ce != null) { m_ce.stop(); m_ce = null; } } //Destroy the applet public void destroy() { } public void run() { double x,y,z; x = 0; y = 2; z = 8; double xd,yd,zd; xd = 0.02; zd = -0.03; double xmax = (landscapeSize/2)-0.1; double xmin = -xmax; double zmax = 8 + xmax; double zmin = 8 + xmin; myWorld.prep(); while (true) { Object3d sphere = myWorld.getParentObject(sphereID); Object3d ray = myWorld.getObject(rayID); double curX = sphere.getCenter().getX(); double curY = sphere.getCenter().getY(); double curZ = sphere.getCenter().getZ(); if ((curX >= xmax) || (curX <= xmin)) xd = -xd; if ((curZ >= zmax) || (curZ <= zmin)) zd = -zd; sphere.translateWorld(xd,0,zd); // And now we can do the collision detection... Vertex start = ray.getWorldVertex(0); Vertex end = ray.getWorldVertex(1); if (myWorld.pick(start,end,landID)) { double dist = HitPoint.getT() * 0.2; // length of the ray... sphere.translateWorld(0,0.1-dist,0); } else { sphere.translateWorld(0,-0.01,0); } myWorld.getCamera().pointAt(sphere); myWorld.prepareCanvas(); myWorld.generateImage(); myWorld.drawImage(); paint(myWorld.getCanvas(), curY); myWorld.finishCanvas(); } } void paint(Graphics g, double curY) { g.setColor(Color.blue); String disp = "Sphere height: "+curY; int length = (disp.length() > 21) ? 21 : 1; disp = disp.substring(0,length); g.drawString(disp,20,40); } }