tornado movement
in
Contributed Library Questions
•
1 year ago
Hi,
I have been doing processing for 2 weeks now, so I'm really new. I have been trying to create something of a 'tornado'. I have 2 problems. I want the tornado to have this random movements on the x-axis like in real life. All of a sudden it goes to the left a bit and then to the right. I can't seem to make this work.
The second thing is that the bounce isn't working I think. When I experiment with moving the tornado on the x axis it does not bounce but just moves of the screen.
If you have an idea, please let me know! The part of the code that concerns the tornado is below:
class Ball {
// GLOBAL VARIABLES
Vec3D loc = new Vec3D (0, 0, 0);
Vec3D speed = new Vec3D (random (-10, 10), 0, 0);
Vec3D acc = new Vec3D (0, 0, 0);
//CONSTRUCTOR
Ball(Vec3D _loc) {
loc = _loc;
}
//FUNCTIONS
void run() {
display();
move();
bounce();
flock();
}
void flock() {
separate(6);
align();
}
void align() {
Ball other = (Ball) ballCollection.get((int) random(ballCollection.size()));
if (other.loc.x > loc.x) {
speed.x = random (-15, 25);
}
else {
speed.x = random (-25, 15);
}
}
void separate(float magnitude) {
Vec3D steer = new Vec3D();
int count = 0;
for (int i = 0; i < ballCollection.size(); i++) {
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if (distance > 0 && distance < random(10, 20)) {
Vec3D diff = loc.sub(other.loc);
diff.normalizeTo(1.0/distance);
steer.addSelf(diff);
count++;
}
}
if (count > 0) {
steer.scaleSelf(1.0/count);
}
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void bounce() {
if (loc.x > width) {
speed.x = speed.x * -1;
}
if (loc.x < 0) {
speed.x = speed.x * -1;
}
if (loc.y > height) {
speed.y = speed.y * -1;
}
if (loc.y < 0) {
speed.y = speed.y * -1;
}
}
void move() {
speed.addSelf(acc);
speed.limit (4);
loc.addSelf(speed);
acc.clear();
}
void display() {
fill(255, 0, 0);
ellipse(loc.x, loc.y, random (2, 8), random (2, 8));
}
}
1