How can i draw the stick figure using Line in 3D?

Code

PShape head, body,leftleg, rightleg;

//head,body,arm1,arm2,leg1,leg2;
void setup() 
{
  size (400,300,P3D);
 // frame.setLocation(0,0);


 //parent shape
 // satori = createShape

 // PShape satori_head;
  head = createShape(SPHERE,20);
  head.setStroke(color(255));
  head.setStrokeWeight(4);
  head.setFill(color(126));

  //now unto the body
  body= createShape(LINE,0, 0,0,0,25,0);
  body.setStroke(color(255,0,0));
  body.setStrokeWeight(4);
  body.setFill(color(126));

  //drawing the left leg
  leftleg=createShape(LINE, 5,25,5,0,0,0);
  leftleg.setStroke(color(189,0,0));
  leftleg.setStrokeWeight(4);
  leftleg.setFill(color(126));

    //drawing the right leg
  //rightleg=createShape(LINE, 0,80,70,80,0,100);
  //rightleg.setStroke(color(200,0,0));
  //rightleg.setStrokeWeight(4);
  //rightleg.setFill(color(126));

}

void draw() {
  background(51);

//head translation
  translate(width/2, 50);
  shape(head);


//body translation
translate(-0,53,100);
shape(body);

//leftleg translation
translate(-0,53,100);
shape(body);

//rightleg translation
translate(-0,53,100);
shape(body);

}

like how does line in 3d works? How can i connect the line to the body to create legs and arms. Thank You ?

Answers

  • Go back edit your post

    Empty line before the entire code section and after it

    Select code with mouse

    Hit ctrl-o

    Oh click C in command bar

  • I did , thanks.

  • edited March 2017 Answer ✓

    @emmaliecious --

    Your problem is that you are trying to move the pieces around in space in two different ways -- once with createShape parameter, and then again later with translate commands. If the pieces is drawn off-center with createShape, then moving it further off-center with translate can do surprising things.

    Think carefully about how you want your pieces (head, body, leg) to be related to 0,0,0 when you create your shapes. Keep it simple. Then use translate() and rotate() and align each piece around the origin while building your stick figure.

    Notice that unless your pieces are different, you don't need to create one per limb. If you have a "leg" shape, you can arrange and draw it twice -- once for the left and for the right leg.

    Here is your sketch with a few corrections and a simple PeasyCam added to the top and setup -- now you can drag the figure with the mouse to inspect what you are doing.


    import peasy.*;
    PeasyCam camera;
    
    PShape head, body, arm, leg;
    
    void setup() 
    {
      size (400, 300, P3D);
      camera = new PeasyCam(this, 300);
      // frame.setLocation(0,0);
    
      // PShape head -- centered
      head = createShape(SPHERE, 20);
      head.setStroke(color(255));
      head.setStrokeWeight(4);
      head.setFill(color(126));
    
      // body -- above and below the center
      body = createShape(LINE, 0, -50, 0, 0, 50, 0);
      body.setStroke(color(255, 0, 0));
      body.setStrokeWeight(4);
      body.setFill(color(126));
    
      // arm -- extends down from the center
      arm = createShape(LINE, 0, 0, 0, 0, 50, 0);
      arm.setStroke(color(189, 0, 0));
      arm.setStrokeWeight(4);
      arm.setFill(color(126));
    
    
      // leg -- extends down from the center
      leg = createShape(LINE, 0, 0, 0, 0, 75, 0);
      leg.setStroke(color(189, 0, 0));
      leg.setStrokeWeight(4);
      leg.setFill(color(126));
    }
    
    void draw() {
      background(51);
    
      // body
      shape(body);
    
      // head
      pushMatrix();
      translate(0, -50, 0);
      shape(head);
      popMatrix();
    
      // arms
      pushMatrix();
      translate(0, -15, 0); // move arms from center up to shoulders
        pushMatrix();
          rotateZ(radians(55)); // swing left arm out
          shape(arm);
        popMatrix();
        pushMatrix();
          rotateZ(radians(-55)); // swing right arm out
          shape(arm);
        popMatrix();
      popMatrix();
    
      // legs
      pushMatrix();
        translate(0, 50, 0); // move legs down to bottom of body
        pushMatrix();
          rotateZ(radians(25)); // swing left leg out
          shape(leg);
        popMatrix();
        pushMatrix();
          rotateZ(radians(-25)); // swing right leg out
          shape(leg);
        popMatrix();
      popMatrix();
    
    }
    
Sign In or Register to comment.