topical media & game development

talk show tell print

graphic-processing-algorithm-Ch08-p196-MyObject.pde / pde



  class MyObject{
    float dim = 30; //size of object
    float x,y,z; //location member
    float ax,ay,az; //rotational values
    float sx=1, sy=1,sz=1; //scaling factors
    boolean picked = false; //picked status
    color c_face = color(255,255,255); //color of face
    color c_edge = color(0); //color of edge
    //translation
    void move3(float xin, float yin, float zin){
      x = xin;
      y = yin;
      z = zin;
    }
    //three rotations
    void rotate3(float axin, float ayin, float azin){
      ax = axin;
      ay = ayin;
      az = azin;
    }
    //scaling
    void scale3(float sxin, float syin, float szin){
      sx = sxin;
      sy = syin;
      sz = szin;
    }
    //determine whether an object is selected
    void pick(float xp, float yp){
      if(dist(xp,yp,screenX(x,y,z),screenY(x,y,z))<10)
        picked = true;
      else
        picked = false;
    }
    //draw the object
    void draw3(){
      stroke(c_edge);
      if(picked==true) //if picked
        fill(255,0,0); // then red
      else
        fill(c_face); // else its own color
      pushMatrix();
      translate(x,y,z);
      rotateX(ax);
      rotateY(ay);
      rotateZ(az);
      scale(sx,sy,sz);
      box(dim);
      popMatrix();
    }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.