public void draw() { stroke(0, 0, 0); change = round(random(1, 10)); if (change == 10) { direction = round(random(0, 8)); point(pointX, pointY); point(pointX + 1, pointY + 1); point(pointX - 1, pointY - 1); point(pointX - 1, pointY + 1); point(pointX + 1, pointY - 1); stroke(200, 200, 200); point(pointX + 1, pointY); point(pointX - 1, pointY); point(pointX, pointY + 1); point(pointX, pointY - 1); stroke(0, 0, 0); } if (direction == 1) { speed.set(0, -1, 0); } if (direction == 2) { speed.set(1, 0, 0); } if (direction == 3) { speed.set(0, 1, 0); } if (direction == 4) { speed.set(-1, 0, 0); } if (direction == 5) { speed.set(-1, -1, 0); } if (direction == 6) { speed.set(1, 1, 0); } if (direction == 7) { speed.set(-1, 1, 0); } if (direction == 8) { speed.set(1, -1, 0); } if (pointX == width) { pointX = 1; } if (pointX == 0) { pointX = width - 1; } if (pointY == height) { pointY = 1; } if (pointY == 0) { pointY = height - 1; } pointX = pointX + speed.x; pointY = pointY + speed.y; point(pointX, pointY); }
// 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.set(r * sin(theta), r * cos(theta), 0); // Polar to cartesian conversion loc.add(origin); // Make sure the location is relative to the pendulum's origin }