// Attraction // Daniel Shiffman // A class for a draggable attractive body in our world class Dollar { float mass; // Mass, tied to size float G; // Gravitational Constant PVector loc; // Location boolean dragging = false; // Is the object being dragged? boolean rollover = false; // Is the mouse over the ellipse? PVector drag; // holds the offset for when object is clicked on // Doing oscillation with velocity float xtheta; float xtheta_vel; // Having two separate variables for x,y is not so great // This could be improved by using our Vector3D class! float ytheta; float ytheta_vel; Dollar(PVector l_,float m_, float g_) { loc = l_.get(); mass = m_; G = g_; drag = new PVector(0.0,0.0); xtheta = ytheta = 0.0f; xtheta_vel = 0.005; ytheta_vel = 0.007; } void go() { move(); render(); drag(); } PVector calcGravForce(Currency t) { PVector dir = PVector.sub(loc,t.getLoc()); // Calculate direction of force float d = dir.mag(); // Distance between objects d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects dir.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) float force = (G * mass * t.getMass()) / (d * d); // Calculate gravitional force magnitude dir.mult(force); // Get force vector --> magnitude * direction return dir; } void move() { //method to occelate the Dollar in time to inflation float amplitude = width/2-100; loc.x = amplitude * sin(xtheta); loc.x=width/2-loc.x; xtheta += xtheta_vel; amplitude = height/2-100; loc.y = amplitude * cos(ytheta); loc.y=height/2-loc.y; ytheta += ytheta_vel; } // Method to display void render() { ellipseMode(CENTER); stroke(0); if (dragging) fill (50); else if (rollover) fill(100); else fill(175,200); //ellipse(loc.x,loc.y,mass*2,mass*2); } // The methods below are for mouse interaction void clicked(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { dragging = true; drag.x = loc.x-mx; drag.y = loc.y-my; } } void rollover(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { rollover = true; } else { rollover = false; } } void stopDragging() { dragging = false; } void drag() { if (dragging) { loc.x = mouseX + drag.x; loc.y = mouseY + drag.y; } } }