Class 在处理中使用类中的类

Class 在处理中使用类中的类,class,processing,Class,Processing,我正在尝试使用质点和弹簧创建处理动画。我能够定义质量点,它们按预期工作。但是,当我尝试创建一个介于两个质点之间的弹簧时,脚本失败,我被告知“未定义质点”。我很困惑,因为我已经定义了MassPoint,并且正在使用它创建我已经拥有的两个点。我需要在Spring类中以不同的方式声明它吗 MassPoint mp1 = new MassPoint(50, 50, 1.0, PI, 3); MassPoint mp2 = new MassPoint(60, 60, 0, PI, 3); Spring s

我正在尝试使用质点和弹簧创建处理动画。我能够定义质量点,它们按预期工作。但是,当我尝试创建一个介于两个质点之间的弹簧时,脚本失败,我被告知“未定义质点”。我很困惑,因为我已经定义了MassPoint,并且正在使用它创建我已经拥有的两个点。我需要在Spring类中以不同的方式声明它吗

MassPoint mp1 = new MassPoint(50, 50, 1.0, PI, 3);
MassPoint mp2 = new MassPoint(60, 60, 0, PI, 3);
Spring s1 = new Spring(mp1,mp2,30,1)

// Setup the Processing Canvas
void setup(){
  size(screen.width, screen.height);
  strokeWeight(1);
  stroke(#000000);
  frameRate( 15 );
  background(background_color);
  fill(#FFFFFF,alpha);

}

// Main draw loop
void draw(){
    background(background_color);
    mp1.move();
    mp2.move();
    console.log(mp1.xPos)
}

class MassPoint { 
  int xPos,yPos,mass; 
  float speed,angle;

  MassPoint (int x, int y, float s, float a, int m) {  
    xPos = x; 
    yPos = y;
    speed = s; 
    angle = a;
    mass = m; 
  } 

  void move(){
    xPos += sin(angle) * speed
    yPos -= cos(angle) * speed
    point(xPos,yPos)
  }
}


class Spring { 
  float length,strength;
  MassPoint mp1, mp2;

  Spring (MassPoint mp1, MassPoint mp2, float l, float s) {  
    xPos1 = mp1.xPos; 
    yPos1 = mp1.yPos;
    xPos2 = mp2.xPos; 
    yPos2 = mp2.yPos;
    length = l; 
    strength = s; 
  } 
}

您是从JavaScript还是其他什么东西移植的?那是一种奇怪的混合动力车。看看这是否有帮助

MassPoint mp1 = new MassPoint(50, 50, 1.0, PI, 3);
MassPoint mp2 = new MassPoint(60, 60, 0, PI, 3);
Spring s1 = new Spring(mp1,mp2,30,1);

// Setup the Processing Canvas
void setup(){
  size(600, 400);
  strokeWeight(1);
  stroke(#000000);
  frameRate( 15 );
  background(0);
  fill(#FFFFFF);

}

// Main draw loop
void draw(){
    background(0);
    mp1.move();
    mp2.move();
    println(mp1.yPos);
}

class MassPoint { 
  int xPos,yPos,mass; 
  float speed,angle;

  MassPoint (int x, int y, float s, float a, int m) {  
    xPos = x; 
    yPos = y;
    speed = s; 
    angle = a;
    mass = m; 
  } 

  void move(){
    xPos += sin(angle) * speed;
    yPos -= cos(angle) * speed;
    point(xPos,yPos);
  }
}


class Spring { 
  float length,strength;
  float xPos1, yPos1, xPos2, yPos2;
  MassPoint mp1, mp2;

  Spring (MassPoint mp1, MassPoint mp2, float l, float s) {  
    xPos1 = mp1.xPos; 
    yPos1 = mp1.yPos;
    xPos2 = mp2.xPos; 
    yPos2 = mp2.yPos;
    length = l; 
    strength = s; 
  } 
}

您是否使用2.0之前的早期版本的Processing?也许在早期版本中,您需要在使用类之前声明类,但现在已经不是这样了。要验证这一点,请将所有类声明移到文件的顶部

尝试2.1.1和3.3中的代码会出现错误“将screen.width和screen.height更改为displayWidth和displayHeight”,建议使用旧版本。请参阅有关这些更改的信息。此外,未定义调用
console.log()
,请改用
println()

下面是我在Processing 2.1.1中测试的代码版本。没有重力或阻尼,但显示出有趣的弹簧行为

MassPoint mpS = new MassPoint(100, 100, 0, PI, 20);
MassPoint mp0 = new MassPoint(100, 100, 0, PI, 20);
MassPoint mp1 = new MassPoint(50,  100, 0, PI, 10);
MassPoint mp2 = new MassPoint(50, 150, 1, PI, 5);
Spring s1 = new Spring(mp0,mp1,50,0.0001);
Spring s2 = new Spring(mp1,mp2,50,0.0001);
int background_color = 64;
int alpha = 255;

// Setup the Processing Canvas
void setup(){
  size(displayWidth, displayHeight);
  strokeWeight(1);
  stroke(#000000);
  //frameRate( 15 );
  background(background_color);
  fill(#FFFFFF,alpha);

}

// Main draw loop
void draw(){
    background(background_color);

    // Spring 0 is static and cannot move
    mp0.xPos = mpS.xPos;
    mp0.yPos = mpS.yPos;

    // Spring 1 and 2 can move
    mp1.move();
    mp2.move();
    s1.move();
    s2.move();

    // Red when compressed, green when stretched
    stroke( constrain(127-4*round(s1.springStretch),0,255), constrain(4*round(s1.springStretch)-127,0,255), 0 );
    line(mp0.xPos, mp0.yPos, mp1.xPos, mp1.yPos);

    stroke( constrain(127-4*round(s2.springStretch),0,255),     constrain(4*round(s2.springStretch)-127,0,255), 0 );
    line(mp1.xPos, mp1.yPos, mp2.xPos, mp2.yPos);

    stroke(0);
    fill(1);
    ellipse(mp0.xPos, mp0.yPos, mp0.mass, mp0.mass);
    ellipse(mp1.xPos, mp1.yPos, mp1.mass, mp1.mass);
    ellipse(mp2.xPos, mp2.yPos, mp2.mass, mp2.mass);

    //println(mp1.xPos);
}

class MassPoint { 
  float xPos,yPos,mass; 
  float xSpeed,ySpeed;

  MassPoint (int x, int y, float s, float a, int m) {  
    xPos = x; 
    yPos = y;
    xSpeed =  (s * sin(a)); 
    ySpeed = -(s * cos(a)); 
    mass = m; 
  } 

  void move(){
    xPos += xSpeed;
    yPos += ySpeed;
    point(xPos,yPos);
  }

  void applyForce( float fx, float fy )
  {
    float x = fx / mass;
    float y = fy / mass;
    xSpeed = xSpeed + x;
    ySpeed = ySpeed + y;
    xPos = xPos + x;
    yPos = yPos + y;
  }
}

class Spring { 
  float springLength, springStrength, springStretch;
  MassPoint mp1, mp2;

  Spring (MassPoint mp1, MassPoint mp2, float l, float s) {  
    this.mp1 = mp1;
    this.mp2 = mp2;
    springLength = l; 
    springStrength = s; 
    springStretch = 0;
  } 
  void move(){
    float xDelta = mp2.xPos - mp1.xPos;
    float yDelta = mp2.yPos - mp1.yPos;
    float dist = sqrt( (xDelta*xDelta) + (yDelta*yDelta) );
    springStretch = (dist-springLength);
    float power = springStretch * springStrength;
    mp1.applyForce(  xDelta*power,  yDelta*power );
    mp2.applyForce( -xDelta*power, -yDelta*power );
  }
}

这里使用的是javascript语言吗?谢谢!此代码适用于我的处理版本。虽然看起来我可能想在某个时候更新它!