Problem with translate and rotate
in
Programming Questions
•
6 months ago
Hello,
The program I'm trying to make is very simple. The first dot on the screen is in the center, and the second dot rotates around the first one, and the third dot rotates around the second one. However, the issue that I'm having is that the third dot rotates around the first one. I just can't seem to think of a way to get the dots to translate around the previous dot.
EDIT:
I fixed the problem. For lines 21-25, I changed the code to:
- x = spacing;
- y = spacing;
- // pushMatrix();
- translate(points.get(num - 1).x, points.get(num - 1).y);
- rotate(radians(angle));
- ellipse(x, y, diam, diam);
- // popMatrix();
The pushMatrix/popMatrix was causing the problem, as well as having spacing inside of ellipse().
I'm having a new problem though. Each dot is dependent on the previous dot. So, if the first dot has an angle of 45, the second dot will have an angle of 90. I'd like the position of the dot to be dependent, but I want the rotation to be completely independent from the previous dot.
New Code:
- class Point {
- float x;
- float y;
- float angle;
- float diam;
- float spacing;
- float gravity;
- int num;
- Point (float initX, float initY, int initNum) {
- x = initX;
- y = initY;
- num = initNum;
- diam = 10;
- angle = 45;
- spacing = 10;
- gravity = 0.1;
- }
- void render() {
- fill(0);
- noStroke();
- if (num != 0) {
- x = 0;
- y = spacing;
- if (angle > 360) {
- angle = 0;
- }
- ellipseMode(CENTER);
- translate(points.get(num - 1).x, points.get(num - 1).y);
- rotate(radians(angle));
- println(num + " " + angle);
- ellipse(x, y, diam, diam);
- } else {
- ellipse(x, y, diam, diam);
- }
- }
- void update() {
- if (num != 0) {
- angle++;
- }
- }
- }
- ArrayList<Point> points;
- void setup() {
- size(500, 500);
- background(255);
- points = new ArrayList();
- int i = 0;
- while (i <= 5) {
- points.add(new Point(250, 250, i));
- i++;
- }
- }
- void draw() {
- background(255);
- for(Point p : points) {
- p.render();
- p.update();
- }
- }
Old code:
- class Point {
- float x;
- float y;
- float angle;
- float diam;
- float spacing;
- int num;
- Point (float initX, float initY, int initNum) {
- x = initX;
- y = initY;
- num = initNum;
- diam = 10;
- angle = 180;
- spacing = 50;
- }
- void render() {
- fill(0);
- noStroke();
- if (num != 0) {
- pushMatrix();
- translate(points.get(num - 1).x, points.get(num - 1).y);
- rotate(radians(angle));
- ellipse(spacing, spacing, diam, diam);
- popMatrix();
- } else {
- ellipse(x, y, diam, diam);
- }
- }
- void update() {
- if (num != 0) {
- angle += random(0, 1.1);
- }
- }
- }
- ArrayList<Point> points;
- void setup() {
- size(500, 500);
- background(255);
- points = new ArrayList();
- int i = 0;
- while (i <= 2) {
- points.add(new Point(250, 250, i));
- i++;
- }
- }
- void draw() {
- background(255);
- for(Point p : points) {
- p.render();
- p.update();
- }
- }
1