We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to tickle the two colored ellipses when they touch together with the mouse but not sure how, here are my codes
boolean goingHorizontal = false; boolean goingVertical = false; boolean fade; float buffer = 10; float mx; float my; float easing = 0.05; float x = 33; float y = 60; float t, c; // X and Y coordinates of ellipse float hr, vr; // horizontal and vertical radius of the ellipse int radius = 24; int edge = 150; int inner = edge + radius; int alpha = 1, delta = 1; int value = 34;
void setup() { size(600,600);
}
void draw() { background(90); noFill(); fill(value);
fill(204, 120); ellipse(256, mouseX, width, height);
if (abs(mouseX - t) < hr && abs(mouseY - c) < vr) { t += random(-5, 5); c += random(-5, 5); } fill(0); text("tickle", t, c); }
float vert = dist(0, pmouseY, 0, mouseY); float horiz = dist(pmouseX,0,mouseX,0);
if(vert > horiz + buffer){ goingHorizontal = false; goingVertical = true; } else if(horiz > vert + buffer){ goingHorizontal = true; goingVertical = false; } else if(vert == horiz){ goingVertical = false; goingHorizontal = false; }
fill(value); ellipse(mx, 256, radius, radius);
fill(value); ellipse(214, my, radius, radius);
fill(value); ellipse(mx, 144, radius, radius);
fill(value); ellipse(471, my, radius, radius);
fill(204, 120);
rect(0, 0, width, height); fill(0);
// If cursor is over the ellipse change the position if ((mouseX >= x) && (mouseX <= x+55) && (mouseY >= y-24) && (mouseY <= y)) { x += random(-2, 2); y += random(-2, 2);
fill(40);
ellipse(my, mx, radius, radius);
}
noFill();
rect(240.33,125.66,203.33,144);
noFill(); rect(127.66,161.61,250,200);
noFill();
rect(256.33,291,214.60,184);
strokeWeight(1);
line(445,125.66,376,161); line(377,162,471,291); line(240,125,127,161); line(127,361,240,269); line(377,361,445.67,269.67); line(256,291,127,161); line(377,361,471,475); line(127,361,256,475);
if (abs(mouseX - mx) > 0.1) { mx = mx + (mouseX - mx) * easing; } if (abs(mouseY - my) > 0.1) { my = my + (mouseY- my) * easing; }
fill(214, 252, 46, alpha); ellipse(my, mx, radius, radius); twinkle();
mx = constrain(mx, 256, width - inner); my = constrain(my, 291, height - inner); noFill();
fill(150, 145, 232, alpha); ellipse(mx, my, radius, radius); twinkle(); }
void mouseMoved() { value = value + 1; if (value > 200) { value = 0; } }
void twinkle() { //fade in and out if (alpha == 0 || alpha == 190) { delta= -delta; } alpha += delta; }
Answers
what means tickle? vibrate?
ok, here we go:
do you mean when the two ellipses touch each other
or when one of the touches the mouse?
because the latter would be still possible although you display the ellipse at mouse pos
to vibrate just add a random value to x and y pos of mouse when it's near to mouse
like in
...+random(-5,5) for x and y of the ellipse position
Edit post, highlight code, press Ctrl-o.
yeah, dist( ) function is correct
before setup()
when drawing the ellipse: (replace the dots with your x and y values )
I am not sure what you code does though, but it looks extremely cool.
When you want to vibrate each ellipse, you need to have a separate tickleIsOn for each ellipse (tickleIsOn1, tickleIsOn2, tickleIsOn3... ). When the dist is small for each pair of ellipses (!!) then set both tickleIsOn to true (for both ellipses that are close).
(This would actually mean it is better to have arrays for the ellipses (or even a class Ellipse and an array of that class)).
@ysj237 --
Here is an example tutorial of circle/circle collision detection by Jeffrey Thompson:
HOWEVER, you said ellipse/ellipse, not circle/circle. Here's what Thompson says about that:
Still, once you work out the collision, adding the vibration behavior to any collision (point/rect, circle/square, etc.) is easy. Below, Thompson's sketch has been modified with a few extra lines so that
if(hit)
randomizes where the circle is drawn.Lines 53-55 are just dist()
@koogs -- True. I think Thompson shows the long form of every single collision detection calculation in his tutorials because he is teaching collision concepts through a geometry primer. You could replace
circleCircle
with:If you want to dig deeper into ellipse-ellipse collision -- which is HARD -- see: https://forum.processing.org/two/discussion/19110/calculate-ellipse-intersection