I have a problem with method move() in Bolder class. The idea is that method move() creates a vector aiming to object of class spaceShip. But it doesn’t work and i can’t understand why. Also I have a problem with deleting objects of class Bolder if they intersecting. For this action responsible method destroy that return boolean variable in main function. Thank you in advance.
import java.util.List;
import java.util.ArrayList;
spaceShip ship;
boolean left = false;
boolean right = false;
boolean forward = false;
int e =0;
float maxSpeed = 0.25;
float maxQuantity = 4;
float accelL = -0.45;
float accelR = 0.45;
float accelF = -24;
float inercia;
PVector zero = new PVector(0, 0);
ArrayList<PVector> mas = new ArrayList <PVector> ();
ArrayList<PVector> goal = new ArrayList <PVector> ();
List<ArrayList<PVector>> ObjectNum = new ArrayList();
ArrayList<Bolder> bmas = new ArrayList<Bolder>();
void setup() {
size(1200, 700);
ship = new spaceShip();
for (int j = 0; j < maxQuantity; ++j) {
bmas.add(new Bolder(random( width/2, width ) * random(3), random(height/2, height) * random(3)));
mas = bmas.get(j).Shape();
goal.add(new PVector(random(width),random(height)));
}
}
void draw() {
ArrayList<PVector> Mass = new ArrayList <PVector> ();
background(0);
ship.render();
ship.edges();
/*
for (PVector m : mas) {
if (m == zero)
break;
Mass.add(m);
}
int j = 0;
while (!forward) {
mas.remove(j);
if (mas.get(j) == zero || j + 1 == mas.size() )
forward = true;
++j;
}
*/
for (int i = 1; i < bmas.size(); ++i) {
bmas.get(i-1).show(mas);
bmas.get(i-1).move();
bmas.get(i-1).edges();
bmas.get(i-1).destroy(bmas.get(i), bmas.get(i-1));
if (bmas.get(i-1).destroy(bmas.get(i), bmas.get(i-1)) == true) {
bmas.remove(bmas.get(i));
bmas.remove(i-1);
}
}
if (bmas.size() < maxQuantity) {
for (int i = bmas.size(); i < maxQuantity; ++i)
bmas.add(new Bolder(random(width), random(height)));
}
}
void keyPressed() {
if (keyCode == RIGHT) {
ship.turn(accelR);
right = true;
}
if (keyCode == LEFT) {
ship.turn(accelL);
left = true;
}
if (keyCode == UP) {
ship.forward(accelF);
}
}
class Bolder {
float phase = 0;
float noiseMax = 5;
float x;
float y;
float r;
ArrayList<PVector> mas = new ArrayList<PVector>();
PVector location;
PVector acceleration;
PVector velocity;
spaceShip ship = new spaceShip();
boolean Wa = false;
Bolder(float x, float y) {
this.x = x;
this.y = y;
location = new PVector(this.x, this.y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
ArrayList<PVector> Shape() {
for (float angle = 0; angle < TWO_PI; angle+=0.5) {
float xoff = map(cos(angle ), -1, 1, 0, noiseMax);
float yoff = map(sin(angle ), -1, 1, 0, noiseMax);
r = map(noise(xoff, yoff), 0, 1, 15, 45);
float x1 = r * cos(angle);
float y1 = r* sin(angle);
PVector xy1 = new PVector (x1, y1);
mas.add(xy1);
noiseMax += 0.001;
}
return mas;
}
void show(ArrayList<PVector> mas) {
push();
stroke(255);
noFill();
beginShape();
for (int i = 0; i < mas.size(); ++i) {
vertex(mas.get(i).x+location.x, mas.get(i).y+location.y);
}
endShape(CLOSE);
pop();
}
void edges() {
if (location.x > width)
location.x = 0;
if (location.x < 0)
location.x = width;
if (location.y > height)
location.y = 0;
if (location.y < 0)
location.y = height;
}
void move() {
ship.pos.sub(location);
ship.pos.setMag(0.20);
acceleration = ship.pos;
//acceleration.mult(acceleration.mag());
velocity.add(acceleration);
location.add(velocity);
velocity.limit(1);
}
boolean destroy(Bolder b, Bolder B){
float di = dist(b.location.x,b.location.y,B.location.x,B.location.y);
if(b.r + B.r > di)
Wa = true;
return Wa;
}
}
class spaceShip {
PVector pos = new PVector (width/2, height/2);
PVector dir = new PVector (1, 1);
float r = 10;
float heading = 0;
spaceShip() {
}
void render() {
push();
translate(pos.x, pos.y);
noFill();
stroke(255);
rotate(heading + PI/2);
triangle(-r, r, r, r, 0, -r);
pop();
}
void turn(float angle) {
heading += angle;
heading %= TWO_PI;
}
PVector Ment(float heading, float yspeed) {
PVector NEW = new PVector(cos(heading) * yspeed, sin(heading) * yspeed);
dir = NEW;
return dir;
}
void forward(float yspeed) {
dir = Ment(heading, yspeed);
pos.sub(dir);
}
void edges() {
if (pos.x > width)
pos.x = 0;
if (pos.x < 0)
pos.x = width;
if (pos.y > height)
pos.y = 0;
if (pos.y < 0)
pos.y = height;
}
}