class CAR { PVector loc = new PVector(random(0,width),random(150,height)); PVector acc = new PVector(0,0); PVector vel = new PVector(0,0); int dim = 20; float maxSpeed = 4.0; boolean collide = false; CAR() { } void create(){ smooth(); // loc = new PVector(mouseX, mouseY); ellipse(loc.x,loc.y, dim, dim); } void move(){ PVector acc = new PVector(0,0); PVector target = new PVector(width/2,0); PVector diff = PVector.sub(target,loc); diff.normalize(); float factor = 1.0; diff.mult(factor); acc.add(diff); if(loc.y-15>0){ loadPixels(); // println(get(int(loc.y)-1,int(loc.x))); if(pixels[int(loc.y-11)*width+int(loc.x)] < -1){ acc.y += 2; } }else{ loc.y = height; loc.x = random(0,width); } if(collide == true){ acc.y += 2; } vel.add(acc); if (vel.mag() > maxSpeed) { vel.normalize(); vel.mult(maxSpeed); } if(loc.y < 0-dim){ vel = new PVector(0,0); } loc.add(vel); } PVector componentVector (PVector vector, PVector directionVector) { //--! ARGUMENTS: vector, directionVector (2D vectors) //--! RETURNS: the component vector of vector in the direction directionVector //-- normalize directionVector directionVector.normalize(); directionVector.mult(vector.dot(directionVector)); return directionVector; } void collideEqualMass(CAR t) { float distanceBetween = t.loc.dist(loc); if(distanceBetween<15){ collide=true; PVector n = PVector.sub(t.loc,loc); n.normalize(); // Difference of velocities so that we think of one object as stationary PVector u = PVector.sub(vel,t.vel); // Separate out components -- one in direction of normal PVector un = componentVector(u,n); // Other component u.sub(un); // These are the new velocities plus the velocity of the object we consider as stastionary vel = PVector.add(u,t.vel); t.vel = PVector.add(un,t.vel); // println(vel); //println(t.vel); }else{ collide = false; } } }