// The Constructor (called when the object is first created) Particle(Vector3D a, Vector3D v, Vector3D l, float r_) { acc = a.copy(); vel = v.copy(); loc = l.copy(); r = r_; timer = 100.0f; }
// Function to update location public void update() { // As long as we aren't dragging the pendulum, let it swing! if (!dragging) { float G = 0.4f; // Arbitrary universal gravitational constant theta_acc = (-1 * G / r) * sin(theta); // Calculate acceleration (see: // http://www.myphysicslab.com/pendulum1.html) theta_vel += theta_acc; // Increment velocity theta_vel *= damping; // Arbitrary damping theta += theta_vel; // Increment theta } loc.setXY(r * sin(theta), r * cos(theta)); // Polar to cartesian conversion loc.add(origin); // Make sure the location is relative to the pendulum's origin }
void wander() { float wanderR = 10.0f; // radius for our "wander circle" float wanderD = 20.0f; // distance for our "wander circle" wandertheta += random(-1, 1); // randomly changet wander theta // now we have to calculate the new location to steer towards on the wander circle Vector3D v = vel.copy(); v.normalize(); // our heading float xoff = wanderD * v.x() + wanderR * cos(wandertheta); // x spot on circle based on heading float yoff = wanderD * v.y() + wanderR * sin(wandertheta); // y spot on circle based on heading Vector3D target = Vector3D.add(loc, new Vector3D(xoff, yoff)); // add the location acc.add(steer(target, false)); // steer towards it }
// look, we can have more than one constructor! Particle(Vector3D l) { acc = new Vector3D(0, 0.05f, 0); // ooooh, bad random, random bad vel = new Vector3D(random(-1, 1), random(-2, 0), 0); loc = l.copy(); r = 10.0f; timer = 100.0f; }
public void drag() { // If we are draging the ball, we calculate the angle between the // pendulum origin and mouse location // we assign that angle to the pendulum if (dragging) { Vector3D diff = Vector3D.sub(origin, new Vector3D(mouseX, mouseY)); // Difference between 2 points theta = atan2(-1 * diff.y, diff.x) - radians(90); // Angle relative to vertical axis } }
// a method that calculates a steering vector towards a target // takes a second argument, if true, it slows down as it approaches the target Vector3D steer(Vector3D target, boolean slowdown) { Vector3D steer; // the steering vector Vector3D desired = Vector3D.sub(target, loc); // a vector pointing from the location to the target float d = desired.magnitude(); // distance from the target is the magnitude of the vector // if the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { // normalize desired desired.normalize(); // two options for magnitude (1 -- based on distance, 2 -- maxspeed) if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed * (d / 100.0f)); else desired.mult(maxspeed); // STEERING VECTOR IS DESIREDVECTOR MINUS VELOCITY steer = Vector3D.sub(desired, vel); steer.limit(maxforce); // limit to maximum steering force } else { steer = new Vector3D(0, 0); } return steer; }
// This constructor could be improved to allow a greater variety of pendulums Pendul(Vector3D origin_, float r_) { // Fill all variables origin = origin_.copy(); r = r_; theta = 0.0f; // calculate the location of the ball using polar to cartesian conversion float x = r * sin(theta); float y = r * cos(theta); loc = new Vector3D(origin.x + x, origin.y + y); theta_vel = 0.0f; theta_acc = 0.0f; damping = 0.995f; // Arbitrary damping ballr = 16.0f; // Arbitrary ball radius }
// function to display void render() { ellipseMode(CENTER); noStroke(); fill(0, 100, 255, timer); ellipse(loc.x(), loc.y(), r, r); }
// function to update location void update() { vel.add(acc); loc.add(vel); timer -= 1.0f; }