发起人:刘大黑 初入职场

HK cityu交换中 想吃家里的红烧肉

回复 ( 1 )

  1. 匿名用户
    理由
    举报 取消

    没有看到你的代码,不知道你是如何处理四肢运动情况的。

    猜测出现这个腿部问题是与纵坐标有关。

    由于只是考虑左右移动时腿部的情况,只关心横坐标即可。

    一个简单的思路就是:

    当 mouseX > pmouseX 时,即向右运动时,设定腿部相对于身体的旋转角度为PI/4

    当mouseX < pmouseX 时,即向左运动时,设定腿部相对于身体的旋转角度为-PI/4

    当mouseX = pmouseX 时,即不动时,设定腿部相对于身体的旋转角度为0

    为避免太灵敏,可以设定一个范围(如-5~5):

    当mouseX – pmouseX > 5时,设定角度为PI/4

    当mouseX – pmouseX < -5时,设定角度为-PI/4

    当-5 <= mouseX – pmouseX <= 5时,设定角度为0

    示意图:

    参考代码:

    void setup(){
      size(600,400);
    }
    void draw(){
      colorMode(HSB,360,100,100);
      background(0,80,100);
      float w = 120;
      float l = w/3*5;
      DaBai db = new DaBai(w,l);
      db.leftArm.a=atan2(mouseY,mouseX);// 左手臂角度
      db.rightArm.a=PI-atan2(mouseY,width-mouseX);// 右手臂角度
      // 腿旋转角度设定(鼠标变动在-5~5之间时不旋转,防止太灵敏)
      if((mouseX-pmouseX)>5){
        db.leftLeg.a=PI/4;db.rightLeg.a=PI/4;//向右移动
      }else if((mouseX-pmouseX)<-5){
        db.leftLeg.a=-PI/4;db.rightLeg.a=-PI/4;//向左移动
      }else{
        db.leftLeg.a=0;db.rightLeg.a=0;// 不变
      }
      // 绳索
      stroke(30,75,75);strokeWeight(2);
      line(0,0,mouseX-db.body.w-db.leftArm.l*cos(db.leftArm.a),mouseY-db.body.l/2-db.leftArm.l*sin(db.leftArm.a));
      line(width,0,mouseX+db.body.w-db.rightArm.l*cos(db.rightArm.a),mouseY-db.body.l/2-db.rightArm.l*sin(db.rightArm.a));
      // 大白
      pushMatrix();
      translate(mouseX,mouseY);
      db.display();
      popMatrix();
    }
    
    class DaBai{
      float w,l;
      Head head;
      Body body;
      Arm leftArm;
      Arm rightArm;
      Leg leftLeg;
      Leg rightLeg;
      DaBai(float w,float l){
        this.w=w;this.l=l;
        head = new Head(w/3.5);
        body = new Body(l/3);
        leftArm = new Arm(l/5,0);
        rightArm = new Arm(l/5,0);
        leftLeg = new Leg(l/6,0);
        rightLeg = new Leg(l/6,0);
      }
      void display(){
        ellipseMode(RADIUS);
        // left arm
        pushMatrix();
        translate(-body.w,-body.l/2);
        rotate(leftArm.a);
        leftArm.display();
        popMatrix();
        // right arm
        pushMatrix();
        translate(body.w,-body.l/2);
        rotate(rightArm.a);
        rightArm.display();
        popMatrix();
        // left leg
        pushMatrix();
        translate(-body.w/3,body.l/1.25);
        rotate(leftLeg.a);
        leftLeg.display();
        popMatrix();
        // right leg
        pushMatrix();
        translate(body.w/3,body.l/1.25);
        rotate(rightLeg.a);
        rightLeg.display();
        popMatrix();
        // body
        body.display();
        // head
        pushMatrix();
        translate(0,-body.l);
        head.display();
        popMatrix();
      } 
    }
    class Head{
      float w,l;
      Head(float w){
        this.w=w;l=w*2/3;
      }
      void display(){
        ellipseMode(RADIUS);
        fill(0,0,100);stroke(0,0,0);strokeWeight(2);
        ellipse(0,0,w,l);// 脸
        fill(0,0,0);stroke(0,0,0);
        ellipse(-w/5*2,-l/10,l/10,l/10);//左眼
        ellipse(w/5*2,-l/10,l/10,l/10);// 右眼
        noFill();stroke(0,0,0);strokeWeight(2);
        bezier(-w/5*2,-l/10,0,l/20,0,l/20,w/5*2,-l/10);// 眼睛之间连线
        fill(20,80,80);stroke(20,80,80);strokeWeight(2);
        ellipse(-w/5*3,l/5,l/10,l/15);// 左腮红
        ellipse(w/5*3,l/5,l/10,l/15);// 右腮红
      } 
    }
    class Body{
      float w,l;
      Body(float l){
        this.l=l;w=l*1/1.2;
      }
      void display(){
        ellipseMode(RADIUS);
        fill(0,0,100);stroke(0,0,0);strokeWeight(2);
        ellipse(0,0,w,l);// 身体
        fill(30,10,100);stroke(0,10,60);strokeWeight(1);
        // 胸口徽章符号
        ellipse(w/5*2,-l/5*2,w/8,w/8);
        beginShape();
        vertex(w/5*2-w/8,-l/5*2);
        vertex(w/5*2-w/16,-l/5*2);
        vertex(w/5*2-w/16,-l/5*2-w/16);
        vertex(w/5*2+w/16,-l/5*2-w/16);
        vertex(w/5*2+w/16,-l/5*2);
        vertex(w/5*2+w/8,-l/5*2);
        endShape();
      }
    }
    class Arm{
      float w,l;
      float a;
      Arm(float w,float a){
        this.w=w;l=w*1/3;this.a=a;
      }
      void display(){
        ellipseMode(RADIUS);
        fill(0,0,100);stroke(0,0,0);strokeWeight(2);
        ellipse(0,0,w,l);
      } 
    }
    class Leg{
      float w,l;
      float a;
      Leg(float l,float a){
        this.l=l;w=l*1/2;this.a=a;
      }
      void display(){
        ellipseMode(RADIUS);
        fill(0,0,100);stroke(0,0,0);strokeWeight(2);
        ellipse(0,0,w,l);
      } 
    }
    

我来回答

Captcha 点击图片更换验证码