[Solved]Different Output in java and javascript

I was running this code in java and javascript mode but I was getting two different output. I am using windows 7 64bit and processing 2.1 64 bit version. Can anyone test this code for me ?

Problem is the rotation of the rectangles, you would notice that in these two different modes. Actually 3D rotation is not working in javascript mode.

Rectangle[] R = new Rectangle[50];
void setup() {
  size(600, 300, P3D);
  for (int i=0;i<R.length;i++) {
    R[i] = new Rectangle((int)random(0, width), (int)random(0, height));
  }
}

void draw()
{
  background(0);
  for (int i=0;i<R.length;i++) {
    R[i].display();
    R[i].update(random(0, 0.2));
    if (R[i].y>height )
    {
      R[i].y=0;
    }
  }
}

class Rectangle {
  float x, y, z, angle, v, xspeed, depth;
  color c;
  Rectangle(float x, float y)
  {
    this.x = x;
    this.y = y;
    c= (color)random(#000000);
    angle = random(0, PI);
    v = random(0.5, 1.5);
    //xspeed = 2*cos(x);
    //depth = random(0, 1000);
    z = map(depth, 0, 100, 0, 1000);
  }

  void display() {
    noStroke();
    fill(c);
    pushMatrix();
    translate(x, y);
    pushMatrix();
    rotate(angle, x, y, 0);
    rect(0, 0, 10, 10);
    popMatrix();
    popMatrix();
  }

  void update(float _angle) {
    angle= angle +_angle;
    y = y + v;
    x = x + sin(random(-PI/8, PI/8));
  }
}

Answers

  • edited December 2013 Answer ✓

    I couldn't find out a fix for rotate()! :o3

    JavaScript is still very similar to Processing 1.5.1.
    And indeed, running your code under the old version, renders crazy results! @-)

    Anyways, I'll leave ya w/ the modifications I was trying to:

    /** 
     * Confetti (v2.02)
     * by  blyk (2013/Dec)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1830/
     * different-output-in-java-and-javascript
     */
    
    static final int NUM = 50;
    Rectangle[] rects = new Rectangle[NUM];
    
    void setup() {
      size(600, 300, P3D);
    
      frameRate(60);
      smooth(8);
      noStroke();
      rectMode(CORNER);
    
      for (int i = 0; i != NUM; rects[i++] = new Rectangle());
    }
    
    void draw() {
      background(0);
    
      for (Rectangle r: rects) r.script();
    }
    
    class Rectangle {
      static final float STEP = .2;
      static final short DIAM = 10;
    
      float x = random(width - DIAM), y = random(height - DIAM);
      float a = random(PI);
    
      final float v = random(.5, 1.5);
      final color c = (color) random(#000000);        // Java
      //final color c = (int) random(#000000) | #000000;  // JS
    
      void script() {
        update();
        display();
      }
    
      void display() {
        fill(c);
    
        pushMatrix();
    
        rotate(a, x, y, 0);
        rect(x, y, DIAM, DIAM);
    
        popMatrix();
      }
    
      void update() {
        x += sin(random(-PI/8, PI/8));
        if ((y += v) > height)   y = -DIAM;
        a += random(STEP);
      }
    }
    
  • Thanks GoToLoop :)

Sign In or Register to comment.