/* * pMapGen.java * * (C) 2005, Alex S. */ import java.util.*; import java.awt.*; /** * random map generator */ public class pMapGen { /** * given 2 points, find their difference */ private static void vectorSubtract (double[] va, double[] vb, double[] out){ out[0] = va[0]-vb[0]; out[1] = va[1]-vb[1]; out[2] = va[2]-vb[2]; } /** * cross product of two vectors */ private static void crossProduct(double[] v1, double[] v2, double[] cross ) { cross[0] = v1[1]*v2[2] - v1[2]*v2[1]; cross[1] = v1[2]*v2[0] - v1[0]*v2[2]; cross[2] = v1[0]*v2[1] - v1[1]*v2[0]; } /** * normalize */ private static double vectorNormalize(double[] in, double[] out ) { double length, ilength; length = (double)Math.sqrt(in[0]*in[0] + in[1]*in[1] + in[2]*in[2]); if (length == 0) { return 0; } ilength = (double)1.0/length; out[0] = in[0]*ilength; out[1] = in[1]*ilength; out[2] = in[2]*ilength; return length; } /** * dot product */ private static double dotProduct (double[] v1, double[] v2){ return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; } /** * gets plane equation from three points on plane * * Plane is: Ax + By + Cx + D = 0 */ private static boolean planeFromPoints(double[] plane, double[] a, double[] b,double[] c,double[] d ){ double[] d1 = new double[4]; double[] d2 = new double[4]; vectorSubtract( b, a, d1 ); if (dotProduct(d1,d1) < (double)0.02) { vectorSubtract( c, a, d1 ); vectorSubtract( d, a, d2 ); } else { vectorSubtract( c, a, d2 ); if (dotProduct(d2,d2) < (double)0.02) { vectorSubtract( d, a, d2 ); } } crossProduct( d1, d2, plane ); if ( vectorNormalize( plane, plane ) == 0 ) { return false; } plane[3] = -dotProduct( a, plane ); return true; } /* * drop cube */ public static pBrush dropCube(pMatrix matrix,Color color){ Vector cube = new Vector(); // initialize cube Vector v_points = new Vector(); Vector v_faces = new Vector(); v_points.addElement(new double[]{1,-1,1,1}); v_points.addElement(new double[]{1,1,1,1}); v_points.addElement(new double[]{-1,1,1,1}); v_points.addElement(new double[]{-1,-1,1,1}); v_points.addElement(new double[]{1,-1,-1,1}); v_points.addElement(new double[]{1,1,-1,1}); v_points.addElement(new double[]{-1,1,-1,1}); v_points.addElement(new double[]{-1,-1,-1,1}); v_faces.addElement(new int[]{0,1,2,3}); v_faces.addElement(new int[]{4,7,6,5}); v_faces.addElement(new int[]{0,4,5,1}); v_faces.addElement(new int[]{7,3,2,6}); v_faces.addElement(new int[]{1,5,6,2}); v_faces.addElement(new int[]{0,3,7,4}); pBrush brush = new pBrush(); for (int f=0;f 0.9) continue; double sin = Math.sin(theta); double cos = Math.cos(theta); matrix.push(); matrix.rotatey(theta); matrix.scale(32,128,256 + 256 * (i/(double)levels)); matrix.translate(rad + rad * (i/(double)levels),0,0); matrix.rotatez(-Math.PI/3 * (i/(double)levels)); map.brushes.addElement( dropCube(matrix, new Color( (int)(0xFF*i/(double)levels), (int)(0xFF*(sin+1)/2), (int)(0xFF*(cos+1)/2) ) ) ); matrix.pop(); } matrix.pop(); } matrix.push(); matrix.translate(0,(-levels/2.0)*300 - 300.0,0); for(int i=10;i>4;i--){ matrix.translate(0,+256,0); matrix.rotatey(Math.PI/4); matrix.push(); matrix.scale(i*50,16,i*50); map.brushes.addElement(dropCube(matrix, new Color( (int)(0xFF*i/11.0), (int)(0xFF*i/11.0), (int)(0xFF*i/11.0) ) ) ); matrix.pop(); } matrix.pop(); matrix.push(); matrix.scale(10,1100,10); map.brushes.addElement(dropCube(matrix,Color.black)); matrix.pop(); return map; } }