-
Pulley Simulation..HELP
Hi
I am trying to simulate a line being effected by a pulley.
At the moment this is very simple. I have 3 plotted points. 1 The begining point, 2 the pully and 3 the end point. A line is drawn to each point so point 1 to point 2, point 2 to point 3.
I want to say if I move point 1 down then point 2 will stay static but point 3 will move up to point 2 by the equal amount of travel applied to point 1. Can do this easily just on the x axis but when I start mixing in the y axis as well I can't figure out how to plot the new position of point 3. I am guessing I would have to use triganomatry (sp?)
Please help.
-
It's ok, I have figured it. I just use the x position of the pully and decrease the x position of point 3 whilst doing the same to the y until the x position of 3 is equal to that of the pully. The nearer I get to the pully the more x I decrease. I would like some help later as I will be applying forces and pressures to the pulley system so would like the pulleys to act in different ways. I.e The pulley could be attached to a weight and the top of the pulley connected to a tow point via a elastic chord. So the pulley would move somewhat until the elastic overcomes the weight.
Actually any thoughts and tips would be good
-
Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
public class PulleyTest extends JPanel implements ChangeListener {
PulleyModel model = new PulleyModel(200,50,40,400);
Rectangle weight = new Rectangle(65, 40);
int bitterEndHeight = 100;
public void stateChanged(ChangeEvent e) {
bitterEndHeight = ((JSlider)e.getSource()).getValue();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// draw pulley
g2.setPaint(Color.blue);
int dia = model.diameter;
Point p = model.loc;
double x = p.x - dia/2;
double y = p.y - dia/2;
Ellipse2D.Double pulley = new Ellipse2D.Double(x, y, dia, dia);
g2.draw(pulley);
// mark center
g2.setPaint(Color.orange);
g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
//
int h = getHeight();
int bitterLength = h - bitterEndHeight - p.y;
double loadLength = model.getLoadLength(bitterLength);
//System.out.printf("bitterLength = %d loadLength = %.1f%n",
// bitterLength, loadLength);
// check calculations
double totalLength = bitterLength + Math.PI*dia/2 + loadLength;
//System.out.printf("totalHeight = %.1f%n", totalLength);
// locate and draw weight
weight.setLocation(p.x+dia/2-weight.width/2, (int)(p.y+loadLength));
g2.setPaint(Color.green.darker());
g2.draw(weight);
// draw line
g2.setPaint(Color.black);
x = p.x-dia/2;
g2.draw(new Line2D.Double(x, p.y, x, p.y+bitterLength));
x = p.x+dia/2;
g2.draw(new Line2D.Double(x, p.y, x, p.y+loadLength));
}
private JSlider getSlider() {
JSlider slider = new JSlider(JSlider.VERTICAL,
10, 280, bitterEndHeight);
slider.addChangeListener(this);
return slider;
}
public static void main(String[] args) {
PulleyTest test = new PulleyTest();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test);
f.add(test.getSlider(), "After");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class PulleyModel {
int diameter;
Point loc;
int lineLength;
PulleyModel(int x, int y, int dia, int line) {
loc = new Point(x, y);
diameter = dia;
lineLength = line;
}
public double getLoadLength(int bitterLength) {
// loadLength = lineLength - bitterLength - circumference/2
return lineLength - bitterLength - Math.PI*diameter/2;
}
}