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; }