Hello everybody. I’m making my own Triangle class that builds triangle objects with some unique features like collision detection, rotate (), centerMode () (which can be VERTEX, CENTROID, CIRCUNCENTER, etc), and a few other things I need.
My problem is that I cannot update the values of the vertices whenever I call my own rotate () method. I’m using an array of PVector to store the coordinates of the three vertices of the triangle (I need this to calculate collision adetection), so I cannot just use the Processing’s own rotate() method, because it doesn’t affect the coordinates values, only the rendering. Instead, I’m using trigonometry to calculate the rotation.
I broke down my code so you can analyse the problem:
PVector[] p = new PVector[3];
float centerX, centerY;
void setup() {
size(300, 300);
centerX = width/2;
centerY = height/2;
// Let's create a equilateral triangle with polar coordinates
for (int i = 0; i < p.length; i++) {
float x = 100 * cos(i * 2*THIRD_PI) + centerX;
float y = 100 * sin(i * 2*THIRD_PI) + centerY;
p[i] = new PVector(x, y);
}
}
void draw() {
background(-1);
line(0, height/2, width, height/2);
line(width/2, 0, width/2, height);
final float inc = frameCount * 0.01; // rotation increment
translate(centerX, centerY);
for (int i = 0; i < p.length; i++) {
float r = dist(centerX, centerY, p[i].x, p[i].y); // calculate each radius
float ang = atan2(p[i].x, p[i].y); // calculates the angle between each point and the origin
// we must add PI to offset the angle back to its original amplitude
float x = r * cos(PI + ang + inc);
float y = r * sin(PI + ang + inc);
fill(#FF0000);
ellipse(x, y, 10, 10);
}
}
What am i doing wrong? Am i missing something? Here’s a even smaller version with only one PVector point. It works just fine:
PVector p;
float centerX, centerY;
void setup() {
size(300, 300);
centerX = width/2;
centerY = height/2;
p = new PVector(50, 50);
}
void draw() {
background(-1);
final float inc = frameCount * 0.01;
translate(centerX, centerY);
float r = dist(centerX, centerY, p.x, p.y);
float ang = atan2(p.x, p.y);
float x = r * cos(PI + ang + inc);
float y = r * sin(PI + ang + inc);
fill(#FF0000);
ellipse(x, y, 10, 10);
}
In short, I want to be able to rotate every vertex of the triangle without using Processing’s rotate() method. Thank you in advance