PImage base;
PImage midarm;
PImage secondarm;
int baserotation;
int midarmrotation;
int secondarmrotation;
int baseX ;
int baseY ;
int midarmX ;
int midarmY;
int secondarmX ;
int secondarmY ;
import controlP5.*;
ControlP5 cp5;
void setup() {
size(1920,1080);
base = loadImage("base.png");
midarm = loadImage("midarm.png");
secondarm= loadImage("secondarm.png");
image(base,baseX,baseY);
smooth();
frameRate(30);
translate(300,400);
cp5 = new ControlP5(this);
cp5.addSlider("sliderValue")
.setPosition(800,400)
.setSize(500,500)
.setRange(0,100)
.setNumberOfTickMarks(101)
;
}
void draw() {
drawmid ();
}
void drawmid (){
background(0);
stroke(255);
imageMode(CENTER);
midarmrotation= int(cp5.getController("sliderValue").getValue());
pushMatrix();
background(0);
translate(midarmX, midarmY);
rotate(midarmrotation);
image(midarm,-midarmX, -midarmY);
popMatrix();
}
I am building a controller for a robotic arm and require 2 of the images to rotate about the pivot point. The second arm is connected to the first and should move similar to this:
I can’t seem to get even the first arm to rotate about the pivot point (the middle of the bottom circle). Any help would be appreciated.
In theory you draw the first arm and use translate to go to its end. Then you rotate() the angle of the 2nd arm.
Then you translate half the width of the 2nd arm and draw it.
There is an example on the website in the example section with robotic arms iirc
/**
* Vector
* by Daniel Shiffman.
*
* Demonstration of some basic vector math: subtraction,
* normalization, scaling. Normalizing a vector sets
* its length to 1.
*/
void setup() {
size(640,360);
}
void draw() {
background(0);
// A vector that points to the mouse location
PVector mouse = new PVector(mouseX,mouseY);
// A vector that points to the center of the window
PVector center = new PVector(width/2,height/2);
// Subtract center from mouse which results in a
// vector that points from center to mouse
mouse.sub(center);
// Normalize the vector
mouse.normalize();
// Multiply its length by 150 (Scaling its length)
mouse.mult(150);
translate(width/2,height/2);
// Draw the resulting vector
stroke(255);
strokeWeight(4);
line(0,0,mouse.x,mouse.y);
//glv added:
PVector arm2 = new PVector(100, 0); //glv added
arm2.set(100, 0);
arm2.rotate(mouseY*TAU/height);
arm2.add(mouse);
line(mouse.x, mouse.y, arm2.x, arm2.y);
}
There is a learning curve to using PVectors but it is worth it!