We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everybody,
I want to drew a graphical interface to control the parameters of few oscillators that I'm going to build through the Minim library. I created a class which consists of a circle with three other little circles inside and this object rotates around itself. (let's say a wheel with three points on it). With the display() function inside the class I drew in draw() three of these wheels.
Now, what I want to do is to get the current position on the screen of the smaller rotating circles of each wheel, in order to calculate the distance between each one of them and the other small circles of the other wheels. The aim is to map each value of the distance with a parameter of my oscillators.
If I calculate the distance in the void display() I cannot use it outside this function and I cannot do it in draw() for example, because the coordinates of the points are valid only in display().
How can I get and use the current position od each small circle?
Wheel wheelA;
Wheel wheelB;
Wheel wheelC;
void setup() {
size(800, 800);
wheelA = new Wheel();
wheelB = new Wheel();
wheelC = new Wheel();
}
void draw() {
background(255);
pushMatrix();
wheelA.display(180, 180, 0.02);
popMatrix();
pushMatrix();
wheelB.display(620, 180, 0.03);
popMatrix();
pushMatrix();
wheelC.display(400, 620, 0.01);
popMatrix();
}
class Wheel {
float posX;
float posY;
float ang= 0;
float aVel;
float xA;
float yA;
float xB;
float yB;
float xC;
float yC;
// The constructor defined
Wheel() {
}
void display(float posX, float posY, float aVel) {
xA = 0;
yA = 80;
xB= 55;
yB = -55;
xC = -55;
yC = -55;
//Wheel - big circle
stroke(0);
strokeWeight(3);
fill(255);
ellipseMode(CENTER);
translate(posX, posY);
rotate(ang);
ellipse(0, 0, 200, 200);
//small circles
ellipse(xA, yA, 10, 10);
ellipse(xB, yB, 10, 10);
ellipse(xC, yC, 10, 10);
ang=ang+aVel;
}
}
Answers
You haven't really made proper use of OOP (Object Oriented Programming).
Why exactly is your constructor function empty? In your current code, there really isn't any use of keeping a seperate class. Have you read the Objects tutorial?
What you should be doing is passing in the parameters to the constructor, and having each instance store its own values. If you read the tutorial, you will understand what I'm trying to say.
Another important note - you really must put the pushMatrix() and popMatrix() functions inside the display() function.
Thank you a lot for the answer!
Ok, I got this. I post the updated code. But I still don't understand how to get the current coordinates of the little circles. Printing the value of the global variables that I set for the x,y of the circles, the value doesn't change despite the display() function makes them rotate.
please don't pass the parameters that are inside the class to the display function - that defies the principle of class
as has been said, pass them to the constructor instead (see tutorial)
also: you can access values like this:
wheelC.posX
(apart from the fact that those values are not set in your code)@Chrisir Did you read his last post?
As to the question, I think your questions have been answered by now.
Or maybe not.
So, do you know trigonometry? You need it.
No, I haven't seen his post. It must have been posted while I was still working at my post
No use to give this
ax, ay, bx, by, cx, cy
to the constructor, they are the same for all objectsafaiK:
https://www.processing.org/reference/screenX_.html
and screenY
(or was it modelX and modelY?)
can give you coordinates after a matrix manipulation (like rotate)
just store
screenax, screenay, screenbx, screenby, screencx, screency
using screenX and screenY
I doubt if that is a problem - he may be planning to have different values later on.
Generally, it is not really recommended to use global variables if you're a beginner.
global variables!? No I would suggest to let them be part of the class
OK, that's what you meant. I misunderstood.
Thank you guys for the helpful answers. But even with the suggested improvements I still cannot solve my problem. I cannot get the current values of the coordinates of my objects, even if I put them in the constructor when I put it in a variable in draw() and print it, it's still not giving me the changing value of the coordinate.
ScreenX and screenY??
Now, you have two choices -