**update: i ended up just drawing it with vertex and then rotating the entire object
translate(l.x, l.y);
rotate(-atan2(v.y,v.x) );
rect(0, 0, 55, 10);
popMatrix();
how do you call out the corners of those rectangles? Say I want a line that comes from the top left corner of the rectangle, how do i define that point?
thanks!
- float G = 5;
- boolean showVectors = true;
- Attractor[] attractor = new Attractor[5];
- Ball[] balls = new Ball[1];
- //////////////////////////////////////////////////////////////
- void setup() {
- size(800, 800);
- smooth();
- for (int i = 0; i < attractor.length; i++) {
- attractor[i] = new Attractor( random(width), random(height) );
- }
- for (int i=0; i <balls.length; i++) {
- balls[i] = new Ball(random(width), random(height) );
- }
- }
- void draw() {
- background(128);
- for (int i=0; i < attractor.length; i++) {
- attractor[i].update();
- }
- for (int i = 0; i < balls.length; i++) {
- balls[i].update();
- }
- }
- void keyPressed() {
- saveFrame("ballForce01_####.png");
- }
- void mousePressed() {
- setup();
- }
- //////////////////////////////////////////////////////////////
- class Ball {
- PVector l, v, a;
- // ball constructor
- Ball(float _x, float _y) {
- l = new PVector(_x,_y);
- v = new PVector();
- }
- // ball functions
- void update() {
- a = new PVector();
- for (int i = 0; i < attractor.length; i++) {
- a.add(attractor[i].getAccel(l.x,l.y));
- }
- v.add(a);
- l.add(v);
- render();
- }
- void render() {
- stroke(0);
- strokeWeight(1);
- if (showVectors) {
- drawVector();
- }
- }
- void drawVector() {
- pushMatrix();
- // transform to the velocity vector
- translate(l.x, l.y);
- rotate(-atan2(v.y,v.x) );
- line(0,0,25,0);
- popMatrix();
- }
- }
- //////////////////////////////////////////////////////////////
- class Attractor {
- float x,y;
- Attractor(float _x, float _y ) {
- x = _x;
- y = _y;
- }
- void update() {
- stroke(255,0,0);
- strokeWeight(1);
- line(x-4, y, x+4, y);
- line(x, y-4, x, y+4);
- }
- PVector getAccel(float px, float py) {
- PVector a = new PVector(0,0,0);
- float d2 = sq(dist(px,py,x,y));
- if (d2 > 10) {
- a.x = G * (x-px) / d2;
- a.y = G * (y-py) / d2;
- }
- return a;
- }
- }
1