我正在尝试使用质点和弹簧创建一个Processing动画。我能够定义质量点,它们按预期工作。然而,当我尝试在两个质点之间创建弹簧时,脚本失败并且我被告知“MassPoint未定义”。我很困惑,因为我已经定义了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;
}
}
答案 0 :(得分:0)
您是从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;
}
}
答案 1 :(得分:0)
您是否使用2.0之前的早期版本的Processing?也许在早期版本中,您需要在使用它们之前声明类,尽管它不再是这种情况。要验证这一点,请将所有类声明移到文件的顶部。
尝试2.1.1和3.3中的代码会出现错误"将screen.width和screen.height更改为displayWidth和displayHeight",这表示旧版本。请参阅有关这些更改的信息here。此外,未定义来电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 );
}
}