Hi guys,
I'm new to programming and I've been trying to figure out my mistakes on my own, but I couldn't quite understand this one. It seems that I'm having a problem to initialize a PVector Array within a class. I have tried:
class Bone {
PVector[] ends = new PVector[2];
ends[0] = new PVector();
ends[1] = new PVector();
FBox bone = new FBox(bonewidth, dist(ends[0].x, ends[0].y, ends[1].x, ends[1].y));
And also:
class Bone {
PVector[] ends = new PVector[2];
for (int i = 0; i < ends.length; i++) {
ends[i] = new PVector();
}
But neither have worked. Many times I get a message saying there's a curly bracket missing but I've counted them and I'm using the same number of {s as of }s. Does anyone have a clue?? Any help would be really appreciated!
Here's the whole code:
import fisica.*;
FWorld world;
final float tamMax = 70; //belly
final float tamMin = 30;
final int numlados = 10;
final float bellymutability = 4;
final float bonewidth = 5;
final float boneavglen = 40;
final float bonemutability = 5;
final float gravity = 0.005;
final float airFriction = 0.95;
class Belly {
float x, y;
int nsides;
ArrayList<PVector> points = new ArrayList<PVector>();
FPoly belly = new FPoly();
Belly(ArrayList<PVector> tpoints) {
points = tpoints;
createbelly();
}
Belly(float tx, float ty, int tnsides) {
x = tx;
y = ty;
nsides = tnsides;
points = bellyshape(tx, ty, nsides);
createbelly();
}
ArrayList bellyshape(float xloc, float yloc, int numsides) {
ArrayList<PVector> pontos = new ArrayList<PVector>();
float ang = 2*PI/numsides;
float r, X, Y;
for (int i = 0; i < numsides; i++) {
r = random(tamMin, tamMax);
X = r*cos(i*ang)+xloc;
Y = r*sin(i*ang)+yloc;
pontos.add(new PVector(X, Y));
}
return pontos;
}
void createbelly() {
for (int i = 0; i < points.size(); i++) {
float X = ((PVector)points.get(i)).x;
float Y = ((PVector)points.get(i)).y;
belly.vertex(X, Y);
}
}
Belly copybelly() {
return new Belly(points);
}
Belly modifybelly() {
ArrayList<PVector> pts = new ArrayList<PVector>();
for (int i = 0; i < points.size(); i++) {
float X = ((PVector)points.get(i)).x;
X += randomGaussian()*bellymutability;
float Y = ((PVector)points.get(i)).y;
Y += randomGaussian()*bellymutability;
pts.add(new PVector(X, Y));
}
return new Belly(pts);
}
}
class Bone {
PVector[] ends = new PVector[2];
ends[0] = new PVector();
ends[1] = new PVector();
FBox bone = new FBox(bonewidth, dist(ends[0].x, ends[0].y, ends[1].x, ends[1].y));
Bone(PVector[] tends) {
ends = tends;
}
Bone(ArrayList<PVector> tpoints) {
int n = int(random(0, tpoints.size()));
ends[0].x = ((PVector)tpoints.get(n)).x;
ends[0].y = ((PVector)tpoints.get(n)).y;
float rot = random(2*PI);
float len = boneavglen * randomGaussian() * bonemutability;
ends[1].x = ends[0].x + len * cos(rot);
ends[1].y = ends[0].y + len * sin(rot);
}
Bone(Belly b) {
int n = int(random(0, b.points.size()));
ends[0].x = ((PVector)b.points.get(n)).x;
ends[0].y = ((PVector)b.points.get(n)).y;
float rot = random(2*PI);
float len = boneavglen * randomGaussian() * bonemutability;
ends[1].x = ends[0].x + len * cos(rot);
ends[1].y = ends[0].y + len * sin(rot);
}
Bone copybone() {
return new Bone(ends);
}
}
void setup() {
size(400, 400);
smooth();
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 400);
world.setEdges();
Belly b1 = new Belly(100, 100, 15); //just simple things to test the code so far
Belly b2 = (b1.modifybelly());
b2.belly.adjustPosition(200.0, 0.0);
Bone bone1 = new Bone(b2);
world.add(b1.belly);
world.add(b2.belly);
world.add(bone1.bone);
}
void draw() {
background(100);
world.step();
world.draw(this);
}