So I have an array in which I create 'ants'. What I want to do is to be able to select one ant on the screen with a mouseclick and have a small rectangle around it so that you know you've selected it. This is the code I thought would do the job:
So the tornado is coming along nicely. I've inserted the image of a cow, made the cow also bounce of the edges. Next on the list I'm trying to make something happen when the tornado and cow collide. For instance, the cow starts rotating and blows away but it can be many things of course. I've read many posts here on collisions but could not figure out where to start in my code. Could somebody give me some tips/pointers/help me out?
Here is the code (it's a lot I know!):
import toxi.geom.*;
//DECLARE
Cow cowOne;
Cloud cloudSmallest;
Cloud cloudSmall;
Cloud cloudBig;
ArrayList ballCollection;
int ballAmount = 1;
void setup() {
size (800, 800);
smooth();
//INITIALIZE
cowOne = new Cow (760,770,30,30);
cloudSmall = new Cloud(width*4/5, height/7, 400, 0.1);
cloudSmallest = new Cloud(width*2/3, height*35/100, 190, 0.25);
cloudBig = new Cloud(width/5, height/10, 500, 0.03);
ballCollection = new ArrayList();
for (int i =0; i < ballAmount; i++) {
PVector origin = new PVector(100, random(400, 800), 0);
Ball myBall = new Ball(origin);
ballCollection.add(myBall);
}
}
void draw() {
background(176, 176, 176);
//This let's you increase or descrease the amount of balls in the tornado (or you could say the intensity of the tornado) with the up and down arrow keys.
if (keyPressed) {
if (key==CODED) {
if (keyCode==UP) {
Ball mb = (Ball) ballCollection.get((int) random (ballCollection.size()));
Ball mb2 = (Ball) ballCollection.get((int) random (ballCollection.size()));
PVector newBallLoc = new PVector();
newBallLoc.x = (mb.loc.x+mb2.loc.x)/2;
newBallLoc.y = (mb.loc.y+mb2.loc.y)/2;
Ball myBall = new Ball(newBallLoc);
myBall.loc.y += random (-50, 50);
ballCollection.add(myBall);
}
if (keyCode==DOWN) {
if (ballCollection.size() > 1) {
ballCollection.remove(0);
}
}
}
}
//CALL FUNCTIONALITY
cowOne.run();
cloudSmall.run();
cloudSmallest.run();
cloudBig.run();
for (int i =0; i < ballCollection.size(); i++) {
Ball mb = (Ball) ballCollection.get(i);
mb.run();
}
}
class Ball {
// GLOBAL VARIABLES
PVector loc = new PVector (0, 0, 0);
PVector speed = new PVector (0, 0, 0);
float speedyness = random (1, 2);
float tornadospeed;
float R = random (117, 120);
float G = random (117, 120);
float B = random (117, 120);
float rHeight = random (300, 350);
//CONSTRUCTOR
Ball(PVector _loc) {
loc = _loc;
speed.y = random (-2, 2);
}
//FUNCTIONS
void run() {
move();
align();
bounce();
display();
}
void align() {
Ball other = (Ball) ballCollection.get((int) random(ballCollection.size()));
// gives the balls the 'tornado-like' movement. The position relative to another ball is calculated. When the other.loc is bigger than the loc. of a ball the ball starts switching between random negative and positive speed.
// The tornadospeed which can be activated by the keys makes it possible to move the entire tornado to either the left or right.
// There is a speedlimit implemented because it can become difficult to control the tornado and keep it's tornado shape.
if (other.loc.x > loc.x) {
speed.x = random (-2, 3)+tornadospeed;
}
else {
speed.x = random (-3, 2)+tornadospeed;
}
if (keyPressed) {
if (key==CODED) {
if (keyCode==LEFT) {
tornadospeed = tornadospeed - 0.2;
}
if (keyCode==RIGHT) {
tornadospeed = tornadospeed + 0.2;
}
if (tornadospeed > 0.2) {
tornadospeed = 0.2;
}
if (tornadospeed < -0.2) {
tornadospeed = -0.2;
}
}
}
}
//bounce function to keep the tornado within the screen. rHeight is there to give the tornado a more dynamic top.
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));