当我编译代码时,我收到一条错误消息,指出velocity
(向量)无法解析或不是field
。有没有人对可能导致此错误的原因提出一些建议?
PVector gravity;
PVector wind;
PVector friction;
Ball b;
void setup(){
fullScreen();
b=new Ball();
}
void draw() {
background(240, 123, 50);
b.update();
//applying gravity to ball
gravity=new PVector(0, .981);
gravity.mult(mass);
b.applyForce(gravity);
//apply wind
wind=new PVector (5, 0);
b.applyForce(wind);
//apply friction
以下行是发生错误的地方。
friction=b.velocity.get();
friction.normalize();
float c=-0.01;
friction.mult(c);
b.applyForce(friction);
b.bounce();
b.display();
}
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
class Ball {
Ball() {
location=new PVector(width/2, height/2);
velocity=new PVector(0, 0);
acceleration=new PVector(0, 0);
mass=5;
diam=mass*20;
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void applyForce(PVector force){
PVector f=PVector.div(force,mass);
acceleration.add(f);
}
void bounce() {
if (location.y>=height-diam/2) {
//hitting floor
velocity.y*=-0.9;
location.y=height-diam/2;
} else if (location.y<=0) {
//striking top
location.y=0+diam/2;
velocity.y*=-0.9;
}
if (location.x<=0+diam/2) {
//hitting left
location.x=0+diam/2;
velocity.x*=-.9;
} else if (location.x>=width-diam/2) {
//hitting right
location.x=width-diam/2;
velocity.x*=-.9;
}
}
void display() {
ellipse(location.x, location.y, diam, diam);
}
}
感谢任何帮助。
答案 0 :(得分:1)
问题是velocity
不是field
的{{1}},它需要位于Ball
类内才能发挥作用。
尝试这样做:
Ball
而不是:
class Ball {
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
Ball() {
...
}
...
}
唯一的区别是我在课堂上包含了5个字段。