Site3D.java

Java source for 2D Blobs, 212 lines. Raw file

import java.awt.image.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.awt.Image;
import java.awt.Font;
import java.util.Vector;  //  http://java.sun.com/products/jdk/1.1/docs/api/java.util.Vector.html




//---- MAIN CLASS ------------------------------------------------------------//

public class Site3D extends tinyptc
{
  //---- DATA
  
  //- Output buffer

  int size;

  int[] buf;

  //- Blob data

  int[] field;

  int[][] blob;
  int[] blob_x, blob_y, blob_z;

  //- 3D data

  int[][] envmap;

  double[][] vertex;

  int[] face_type;
  int[][] face;



  //--- drawFace
  
  //  Renders a 3D face into the buffer

  void drawFace(int index)
  {  
  
  }



  //---- rand
  
  //  Lewpen's random number algorithm - Creates random real numbers within a given range

  int rand_1 = 0x87361872, rand_2 = 0x12487, rand_3 = 0x8129743;

  public double rand(double min, double max)
  {
    rand_1 += rand_2^rand_3;  //  Lewpen's algo
    rand_2 ^= rand_3*rand_1;
    rand_3 *= rand_1+rand_2;
    
    rand_1 = (rand_1 << 2) | (rand_1 >> 30);  //  Rotate seeds by different ammounts
    rand_2 = (rand_2 << 7) | (rand_2 >> 25);
    rand_3 = (rand_3 << 9) | (rand_3 >> 23);

    rand_1 += 0x15214391;  //  Randomize the bits somewhat
    rand_2 += 0xF2248365;
    rand_3 += 0x42C427AA;

    //  Create random number 0..1234566;
    
    int r = ((rand_1+rand_2+rand_3)/3) % 1234567;

    //  Scale to the range [min..max)

    return min + (max-min)*(r+0.0) / 1234567.0;
  }



  //---- calcField
  
  //  Creates the potential field for the blobs

  public void calcField(int width, int height, int frame)
  {
    //- Clear field to a sine wave pattern

    int i, j, x, y,o;

    double k;

    for(o=0, j=0; j<height; j++) for(i=0; i<width; i++, o++)
    {
      k =
        0.600 * Math.sin((i*0.129+j*0.921+frame*0.5*0.921)*3.14159*0.01523 + 1.872) +
        0.200 * Math.sin((i*0.910+j*0.283+frame*0.5*0.528)*3.14159*0.01514 + 3.421) +
        0.200 * Math.sin((i*0.726+j*0.872+frame*0.5*1.247)*3.14159*0.01721 + 2.117) +
      0;

      field[o] = 10000 + (int)(10000.0*Math.sin(k*0.5*3.14159));
    }

    //- Put blobs in field
    
    for(i=0; i<20; i++)
    {
      for(y=0; y<100; y++) for(x=0; x<100; x++)
      {
        int r = x+blob_x[i]-50;
        int s = y+blob_y[i]-50;
        if(r >= 0 && r < width && s >= 0 && s < height)
        {
          field[r+width*s] += blob[y][x];
        }
      }
    }
  }



  //---- main
  
  //  Main loop

  public void main(int width, int height)
  {
    size = width * height;
    buf = new int[size];
    field = new int[size];
    blob = new int[100][100];
    
    blob_x = new int[20];
    blob_z = new int[20];
    blob_y = new int[20];

    int x, y, i, j;

    //- Create blob potential field

    double d, dd;

    for(j=0; j<100; j++) for(i=0; i<100; i++)
    {
      d = (i-50)*(i-50) + (j-50)*(j-50);

      dd = 1-Math.sqrt(d/50/50);
      if(dd < 0) dd = 0;

      if(d < 0.01) d = 0.01;
      d = 5000000 / d;

      blob[j][i] = (int)(d * dd);
    }

    //- Position blobs
    
    for(i=0; i<20; i++)
    {
      blob_x[i] = (int)rand(0, width);
      blob_y[i] = (int)rand(0, height);
      blob_z[i] = (int)rand(0, width);
    }

    //- Anim loop

    int frame = 0;

    while (true)
    {
      long time = System.currentTimeMillis();

      calcField(width, height, frame);

      //- Render

      int o;

      for(o=0, j=0; j<height; j++) for(i=0; i<width; i++, o++)
      {
        if(field[o] > 15000) buf[o] = 0x00FFCC00; else buf[o] = 0; // -= (buf[i]>>3) & 0x1F1F1F1F;
//        buf[i] = field[i];
      }

      //- Move
      
      for(i=0; i<20; i++)
      {
        blob_y[i] = blob_y[i] - (i%5) - 1;
        if(blob_y[i] < -50)
        {
          blob_x[i] = (int)rand(0, width);
          blob_y[i] = height+50+(int)rand(0, 20);
        }
      }

      update(buf);
      frame++;
    }
  }

}



//---- end of code -----------------------------------------------------------//