Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float x1, y1;
- float x2, y2;
- float diameter1;
- float diameter2;
- float spring = 0.05;
- float gravity = 0.03;
- float friction = -0.9;
- float vx1;
- float vy1;
- float vx2;
- float vy2;
- void setup() {
- frameRate(100);
- x1 = width/2;
- y1 = height/5;
- diameter1 = 70;
- x2 = width/4;
- y2 = height/2;
- diameter2 = 70;
- size(640, 360);
- // noStroke();
- background(204);
- //fill(0, 0);
- }
- void draw() {
- frameRate(10);
- background(124);
- vy1 += gravity;
- vx1 += 0.1;
- x1 += vx1;
- y1 += vy1;
- if (x1 + diameter1/2 > width) {
- x1 = width - diameter1/2;
- vx1 *= friction;
- }
- else if (x1 - diameter1/2 < 0) {
- x1 = diameter1/2;
- vx1 *= friction;
- }
- if (y1 + diameter1/2 > height) {
- y1 = height - diameter1/2;
- vy1 *= friction;
- }
- else if (y1 - diameter1/2 < 0) {
- y1 = diameter1/2;
- vy1 *= friction;
- }
- vy2 += gravity;
- vx2 += 0.1;
- x2 += vx2;
- y2 += vy2;
- if (x2 + diameter2/2 > width) {
- x2 = width - diameter2/2;
- vx2 *= friction;
- }
- else if (x2 - diameter2/2 < 0) {
- x2 = diameter2/2;
- vx2 *= friction;
- }
- if (y2 + diameter2/2 > height) {
- y2 = height - diameter2/2;
- vy2 *= friction;
- }
- else if (y2 - diameter2/2 < 0) {
- y2 = diameter2/2;
- vy2 *= friction;
- }
- float dx1 = x2 - x1;
- float dy1 = y2 - y1;
- fill(232,19,19);
- line(x1, y1, x2, y2);
- line(x1, y1, abs(x1 - x2), abs(y2 - y1));
- line(abs(x1 - x2), abs(y2 - y1), x2, y2);
- float distance1 = sqrt(dx1*dx1 + dy1*dy1);
- float minDist1 = diameter2/2 + diameter1/2;
- text("Dist X:"+dx1, 100, 100);
- text("Dist Y:"+dy1, 100, 110);
- if((abs(dy1) < minDist1)&&(abs(dx1) < minDist1)){
- fill(18,16,175);
- ellipse(100, 100, diameter2, diameter2);
- }
- if (distance1 < minDist1) {
- line (0,0,x1,y1);
- line (0,0,x2,y2);
- line (0,0,dx1,dy1);
- float angle = atan2(dy1, dx1);
- float targetX1 = x1 + cos(angle) * minDist1;
- float targetY1 = y1 + sin(angle) * minDist1;
- float ax1 = (targetX1 - x2) * spring;
- float ay1 = (targetY1 - y2) * spring;
- vx1 -= ax1;
- vy1 -= ay1;
- x2 += ax1;
- y2 += ay1;
- }
- float dx2 = x1 - x2;
- float dy2 = y1 - y2;
- float distance2 = sqrt(dx2*dx2 + dy2*dy2);
- float minDist2 = diameter1/2 + diameter2/2;
- if (distance2 < minDist2) {
- float angle = atan2(dy2, dx2);
- float targetX2 = x2 + cos(angle) * minDist2;
- float targetY2 = y2 + sin(angle) * minDist2;
- float ax2 = (targetX2 - x1) * spring;
- float ay2 = (targetY2 - y1) * spring;
- vx2 -= ax2;
- vy2 -= ay2;
- x1 += ax2;
- y1 += ay2;
- }
- fill(232,19,19);
- text(x1, 50, 25);
- text(y1, 50, 35);
- ellipse(x1, y1, diameter1, diameter1);
- fill(245,240,94);
- text(x2, 150, 25);
- text(y2, 150, 35);
- ellipse(x2, y2, diameter2, diameter2);
- }
- =========================================
- float x1, y1;
- float x2, y2;
- float diameter1;
- float diameter2;
- float gravity = 0.025;
- float friction = -0.8;
- float spring = 0.1;
- float vx1;
- float vy1;
- float vx2;
- float vy2;
- void setup() {
- x1 = width/2;
- y1 = -height;
- diameter1 = 70;
- x2 = width/2;
- y2 = -height;
- diameter2 = 70;
- size(640, 360);
- noStroke();
- background(204);
- //fill(0, 0);
- }
- void draw() {
- background(204);
- vy1 += gravity;
- x1 += vx1;
- y1 += vy1;
- if (x1 + diameter1/2 > width) {
- x1 = width - diameter1/2;
- vx1 *= friction;
- }
- else if (x1 - diameter1/2 < 0) {
- x1 = diameter1/2;
- vx1 *= friction;
- }
- if (y1 + diameter1/2 > height) {
- y1 = height - diameter1/2;
- vy1 *= friction;
- }
- else if (y1 - diameter1/2 < 0) {
- y1 = diameter1/2;
- vy1 *= friction;
- }
- vy2 += gravity;
- x2 += vx2;
- y2 += vy2;
- if (x2 + diameter2/2 > width) {
- x2 = width - diameter2/2;
- vx2 *= friction;
- }
- else if (x2 - diameter2/2 < 0) {
- x2 = diameter2/2;
- vx2 *= friction;
- }
- if (y2 + diameter2/2 > height) {
- y2 = height - diameter2/2;
- vy2 *= friction;
- }
- else if (y2 - diameter2/2 < 0) {
- y2 = diameter2/2;
- vy2 *= friction;
- }
- float dx1 = x2 - x1;
- float dy1 = y2 - y1;
- float distance1 = sqrt(dx1*dx1 + dy1*dy1);
- float minDist1 = diameter2/2 + diameter1/2;
- if (distance1 < minDist1) {
- float angle = atan2(dy1, dx1);
- float targetX1 = x1 + cos(angle) * minDist1;
- float targetY1 = y1 + sin(angle) * minDist1;
- float ax1 = (targetX1 - x2) * spring;
- float ay1 = (targetY1 - y2) * spring;
- vx1 -= ax1;
- vy1 -= ay1;
- x2 += ax1;
- y2 += ay1;
- }
- float dx2 = x1 - x2;
- float dy2 = y1 - y2;
- float distance2 = sqrt(dx2*dx2 + dy2*dy2);
- float minDist2 = diameter1/2 + diameter2/2;
- if (distance2 < minDist2) {
- float angle = atan2(dy2, dx2);
- float targetX2 = x2 + cos(angle) * minDist2;
- float targetY2 = y2 + sin(angle) * minDist2;
- float ax2 = (targetX2 - x1) * spring;
- float ay2 = (targetY2 - y1) * spring;
- vx2 -= ax2;
- vy2 -= ay2;
- x1 += ax2;
- y1 += ay2;
- }
- //float angle = atan2(mouseX, mouseY);
- fill(232,19,19);
- text(x1, 50, 50);
- text(y1, 50, 60);
- ellipse(x1, y1, diameter1, diameter1);
- fill(245,240,94);
- text(x2, 150, 50);
- text(y2, 150, 60);
- ellipse(x2, y2, diameter2, diameter2);
- }
Advertisement
Add Comment
Please, Sign In to add comment