Loading...
Logo
Processing Forum
squarecrow's Profile
2 Posts
5 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    Hello, I did some searching on this and I'm still a little puzzled.

    I wrote something that translates to the center of the screen rotates about 3 axes, then paints a rectangular panel in 3D space.  I wanted to move this work inside a class that takes care of itself, which seemed to work until I moved the translate() statement inside the class.

    The old draw loop in the main class:
    1. void draw(){
        background(0);
        pushMatrix();
          translate(width / 2, height / 2, 100); // move to middle of screen
          myPanel.set_bearing(new float[] {PI * i / 30,PI * i / 30,PI * i / 30});
          myPanel.paint();
        popMatrix();
        i++;
      }
    Now instantiating a new version of the QuadForm myPanel passed the above translate() arguments in the constructor for later use each frame:
    1. myPanel = new QuadForm(width / 2, height / 2, 100);
    The new QuadForm.paint(), with translate().  It's important to note that the only change from the working version to the new version is translate(), which I have verified was passed the proper parameters.
    1. void paint(){
          fill(shapeColor);
          translate(cX, cY, cZ); // moving translate to QF causes failure to display
          rotateY(bearing[0]); // Transfer rotations to QF, no error
          rotateX(bearing[1]);
          rotateZ(bearing[2]);
         
          beginShape(QUADS);
            for(int i = 0; i < template.length; i++){
              vertex(template[i][0], template[i][1], template[i][2]);
            }
          endShape();
        }
      }
    Any clues as to why moving rotateX/Y/Z() inside the class should be fine but translate() is not?
    This class represents an "endless" grid that is supposed to scroll under a player ship in a game.  It rotates when the player turns.  There's a strange thing that happens, though; processor usage shoots way up whenever the grid is at 45deg angles to the window edges!  If I am drawing and throwing away the same number of lines per frame, why should the angle matter at all?
    1. class GridField{
        float gridSpacing;
        float hLinOrig;
        float vLinOrig;
        float xOffset;
        float yOffset;
        float directionRads;
        float renderLen;
        int index; float measure;
        float raw;
       
        GridField(float spacing, float xStart, float yStart){
          gridSpacing = spacing; xOffset = xStart; yOffset = yStart;
          directionRads = 0.0;
          renderLen = max(WINWIDTH,WINHEIGHT) * 1.5;
          calc_first_lines(xStart, yStart);
          index = 0;
        }
       
        void calc_first_lines(float iX, float iY){
          hLinOrig = - yOffset % gridSpacing;
          vLinOrig = - xOffset % gridSpacing;
        }
       
        void move(float dX, float dY){
          xOffset += dX; yOffset += dY;
        }
       
        void turn(float dir){
          raw = directionRads + dir;
          if(raw < 0){
            directionRads = TWO_PI + raw;
          }else if(raw > TWO_PI){
            directionRads = raw - TWO_PI;
          }else{directionRads = raw;}
          //if(abs(directionRads) > HALF_PI && yOffset > 0){yOffset *= -1;}//else
          //if(abs(directionRads) < HALF_PI && yOffset < 0){yOffset *= -1;}
        }
       
        void place(float iX, float iY, float iBearing){
          xOffset = iX; yOffset = iY; directionRads = iBearing;
        }
       
        void update(){
          pushMatrix();
            //rotate(directionRads);
            translate(wreckerXAnchor,wreckerYAnchor);
            rotate(directionRads);
            index = 1; measure = index * gridSpacing;
            calc_first_lines(xOffset, yOffset);
            line(-0.5 * renderLen, hLinOrig, 0.5 * renderLen, hLinOrig);
            line(vLinOrig, -0.5 * renderLen, vLinOrig, 0.5 * renderLen);
            while(measure < renderLen / 2){
              // draw horizontal lines
              line(-0.5 * renderLen, hLinOrig + measure, 0.5 * renderLen, hLinOrig + measure);
              line(-0.5 * renderLen, hLinOrig - measure, 0.5 * renderLen, hLinOrig - measure);
              // draw vertical lines
              line(vLinOrig + measure, -0.5 * renderLen, vLinOrig + measure, 0.5 * renderLen);
              line(vLinOrig - measure, -0.5 * renderLen, vLinOrig - measure, 0.5 * renderLen);
              // increment for next set of lines
              index++; measure = index * gridSpacing;
            }
            //println(vLinOrig); print(hLinOrig);
            //println(directionRads);
          popMatrix();
        }
      }