We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi :) I'm trying to make an ellipse move horizontally when I press on it, then make it move back to its original position if pressed again. Right now, I only know how to make it move further to the right every time I press. Sorry, I couldn't find it in any beginner guides or videos.
Right now, it checks if the mouse was pressed on the circle, then it does the background(255) to get rid of previous circles and it draws a new circle. If I press outside of the circle, the circle stays where it is.
int circleX = 100;
int circleY = 100;
void setup() {
size(200,200);
background(255);
}
void draw() {
ellipseMode(CENTER);
stroke(0);
fill(175);
ellipse(circleX,circleY,50,50);
}
void mousePressed() {
if (dist(mouseX, mouseY,circleX,circleY)<=25) {
background(255);
circleX = circleX + 50;
}
else {
circleX = circleX;
}
}
Answers
Hi GoToLoop, thanks a lot :) It works as it should but it's so advanced and colourful now, haha. I think this is a bit above my level because I'm still a complete beginner (this is my second day with Processing).
static final int DIAM = 0100, RAD = DIAM>>1;
This means that integer DIAM / diam of circle will always be 100 pixels, right? What does RAD and the >>1 section mean?
x = constrain(x + (toRight? STEP : -STEP), RAD, width - RAD);
x is the x for the center of the ellipse? Could you maybe explain what happens here? :) Like, how does it know if it should move left or right.
Thanks again!
There is a boolean that tracks if the ellipse is moving to the right. It is always trying to move, one way or the other, but is constrained to remain within a certain range. The >>1 is a bit shift. He is doing a quick divide by two.
Oh! So RAD is radius and the diameter is divided in half to get the radius :) And width - RAD is the one that keeps the circle from moving out of the window?
Sorry for the newb questions.
>>1
is a slightly faster way to integer-divide by 2.0100
is octal for64
decimal btW.boolean
toRight.true
, it goes w/ STEP. Otherwise it goes w/ -STEP.?:
conditional operator:https://Processing.org/reference/conditional.html
https://processing.org/reference/constrain_.html
Yes. When it's on the left edge, x, the x position of the ellipse's center, is RAD distance away from 0, so the lower limit is RAD.
The right edge's limit is the width of the sketch minus the radius, which causes the center of the circle to stop when the edge of the circle touches the edge of the sketch.
Try using 0 and width as the limits to constrain to see the difference. Or -RAD and width+RAD.
Thank you! I'll be sure to read the links!!