collision with tornado
in
Programming Questions
•
1 year ago
Hi,
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.
void bounce() {
if (loc.x > width) {
speed.x = abs(speed.x)* -1;
}
if (loc.x < 0) {
speed.x = abs(speed.x);
}
if (loc.y > height) {
speed.y = abs(speed.y) * -1;
}
if (loc.y < rHeight) {
speed.y = abs(speed.y) ;
}
}
void move() {
//speed or movement of the tornado
if (speed.x > speedyness) {
speed.x = speedyness;
}
if (speed.x < -speedyness) {
speed.x = -speedyness;
}
if (speed.y > speedyness) {
speed.y = speedyness;
}
if (speed.y < -speedyness) {
speed.y = -speedyness;
}
//this gives the tornado it's funnle form.
loc.x += speed.x * 12.0 * (height - loc.y)/height;
loc.y += speed.y;
}
void display() {
fill(R, G, B);
ellipse(loc.x, loc.y, random (5), random (5));
}
}
class Cloud {
//GLOBAL VARIABLES
float cX;
float cY;
float cS;
float cloudSpeed;
//CONSTRUCTOR
Cloud(float tempcX, float tempcY, float tempcS, float tempSpeed) {
cX = tempcX;
cY = tempcY;
cS = tempcS;
cloudSpeed = tempSpeed;
}
//FUNCTIONS
void run() {
move();
display();
bounce();
}
void display() {
smooth();
noStroke();
fill(255, 75);
ellipse(cX, cY, cS, cS/6);
ellipse(cX+cS/8, cY-cS/16, cS, cS/5);
ellipse(cX+cS/8, cY+cS/16, cS, cS/5);
}
void move() {
cX = cX + cloudSpeed;
}
void bounce() {
if (cX > width+250) {
cX = -250;
}
}
}
class Cow {
//GLOBAL VARIABLES
float x;
float y;
float W;
float H;
float speed = 0.3;
//CONSTRUCTOR
Cow (float _x, float _y, float _W, float _H) {
x=_x;
y=_y;
W=_W;
H=_H;
}
//FUNCTIONS
void run () {
cow();
move();
bounce();
}
void cow() {
PImage cow;
cow = loadImage ("cow.png");
image(cow, x, y, W, H);
}
void move() {
x = x + speed;
}
void bounce() {
if (x > width) {
speed = speed * -1;
}
if (x < 0) {
speed = speed * -1;
}
}
}
1