mario60

Untitled

Mar 23rd, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. float x1, y1;
  2. float x2, y2;
  3. float diameter1;
  4. float diameter2;
  5.  
  6. float spring = 0.05;
  7. float gravity = 0.03;
  8. float friction = -0.9;
  9.  
  10. float vx1;
  11. float vy1;
  12. float vx2;
  13. float vy2;
  14.  
  15. void setup() {
  16. frameRate(100);
  17.  
  18. x1 = width/2;
  19. y1 = height/5;
  20. diameter1 = 70;
  21. x2 = width/4;
  22. y2 = height/2;
  23. diameter2 = 70;
  24. size(640, 360);
  25. // noStroke();
  26. background(204);
  27. //fill(0, 0);
  28. }
  29.  
  30. void draw() {
  31. frameRate(10);
  32. background(124);
  33.  
  34. vy1 += gravity;
  35. vx1 += 0.1;
  36. x1 += vx1;
  37. y1 += vy1;
  38. if (x1 + diameter1/2 > width) {
  39. x1 = width - diameter1/2;
  40. vx1 *= friction;
  41. }
  42. else if (x1 - diameter1/2 < 0) {
  43. x1 = diameter1/2;
  44. vx1 *= friction;
  45. }
  46. if (y1 + diameter1/2 > height) {
  47. y1 = height - diameter1/2;
  48. vy1 *= friction;
  49. }
  50. else if (y1 - diameter1/2 < 0) {
  51. y1 = diameter1/2;
  52. vy1 *= friction;
  53. }
  54.  
  55. vy2 += gravity;
  56. vx2 += 0.1;
  57. x2 += vx2;
  58. y2 += vy2;
  59. if (x2 + diameter2/2 > width) {
  60. x2 = width - diameter2/2;
  61. vx2 *= friction;
  62. }
  63. else if (x2 - diameter2/2 < 0) {
  64. x2 = diameter2/2;
  65. vx2 *= friction;
  66. }
  67. if (y2 + diameter2/2 > height) {
  68. y2 = height - diameter2/2;
  69. vy2 *= friction;
  70. }
  71. else if (y2 - diameter2/2 < 0) {
  72. y2 = diameter2/2;
  73. vy2 *= friction;
  74. }
  75.  
  76. float dx1 = x2 - x1;
  77. float dy1 = y2 - y1;
  78. fill(232,19,19);
  79. line(x1, y1, x2, y2);
  80. line(x1, y1, abs(x1 - x2), abs(y2 - y1));
  81. line(abs(x1 - x2), abs(y2 - y1), x2, y2);
  82.  
  83. float distance1 = sqrt(dx1*dx1 + dy1*dy1);
  84. float minDist1 = diameter2/2 + diameter1/2;
  85. text("Dist X:"+dx1, 100, 100);
  86. text("Dist Y:"+dy1, 100, 110);
  87. if((abs(dy1) < minDist1)&&(abs(dx1) < minDist1)){
  88. fill(18,16,175);
  89. ellipse(100, 100, diameter2, diameter2);
  90. }
  91. if (distance1 < minDist1) {
  92. line (0,0,x1,y1);
  93. line (0,0,x2,y2);
  94. line (0,0,dx1,dy1);
  95. float angle = atan2(dy1, dx1);
  96. float targetX1 = x1 + cos(angle) * minDist1;
  97. float targetY1 = y1 + sin(angle) * minDist1;
  98. float ax1 = (targetX1 - x2) * spring;
  99. float ay1 = (targetY1 - y2) * spring;
  100. vx1 -= ax1;
  101. vy1 -= ay1;
  102. x2 += ax1;
  103. y2 += ay1;
  104. }
  105.  
  106. float dx2 = x1 - x2;
  107. float dy2 = y1 - y2;
  108. float distance2 = sqrt(dx2*dx2 + dy2*dy2);
  109. float minDist2 = diameter1/2 + diameter2/2;
  110. if (distance2 < minDist2) {
  111. float angle = atan2(dy2, dx2);
  112. float targetX2 = x2 + cos(angle) * minDist2;
  113. float targetY2 = y2 + sin(angle) * minDist2;
  114. float ax2 = (targetX2 - x1) * spring;
  115. float ay2 = (targetY2 - y1) * spring;
  116. vx2 -= ax2;
  117. vy2 -= ay2;
  118. x1 += ax2;
  119. y1 += ay2;
  120. }
  121.  
  122.  
  123. fill(232,19,19);
  124. text(x1, 50, 25);
  125. text(y1, 50, 35);
  126. ellipse(x1, y1, diameter1, diameter1);
  127. fill(245,240,94);
  128. text(x2, 150, 25);
  129. text(y2, 150, 35);
  130. ellipse(x2, y2, diameter2, diameter2);
  131. }
  132.  
  133. =========================================
  134.  
  135. float x1, y1;
  136. float x2, y2;
  137. float diameter1;
  138. float diameter2;
  139.  
  140. float gravity = 0.025;
  141. float friction = -0.8;
  142. float spring = 0.1;
  143.  
  144. float vx1;
  145. float vy1;
  146. float vx2;
  147. float vy2;
  148.  
  149. void setup() {
  150.  
  151. x1 = width/2;
  152. y1 = -height;
  153. diameter1 = 70;
  154. x2 = width/2;
  155. y2 = -height;
  156. diameter2 = 70;
  157. size(640, 360);
  158. noStroke();
  159. background(204);
  160. //fill(0, 0);
  161. }
  162.  
  163. void draw() {
  164. background(204);
  165.  
  166. vy1 += gravity;
  167. x1 += vx1;
  168. y1 += vy1;
  169. if (x1 + diameter1/2 > width) {
  170. x1 = width - diameter1/2;
  171. vx1 *= friction;
  172. }
  173. else if (x1 - diameter1/2 < 0) {
  174. x1 = diameter1/2;
  175. vx1 *= friction;
  176. }
  177. if (y1 + diameter1/2 > height) {
  178. y1 = height - diameter1/2;
  179. vy1 *= friction;
  180. }
  181. else if (y1 - diameter1/2 < 0) {
  182. y1 = diameter1/2;
  183. vy1 *= friction;
  184. }
  185.  
  186. vy2 += gravity;
  187. x2 += vx2;
  188. y2 += vy2;
  189. if (x2 + diameter2/2 > width) {
  190. x2 = width - diameter2/2;
  191. vx2 *= friction;
  192. }
  193. else if (x2 - diameter2/2 < 0) {
  194. x2 = diameter2/2;
  195. vx2 *= friction;
  196. }
  197. if (y2 + diameter2/2 > height) {
  198. y2 = height - diameter2/2;
  199. vy2 *= friction;
  200. }
  201. else if (y2 - diameter2/2 < 0) {
  202. y2 = diameter2/2;
  203. vy2 *= friction;
  204. }
  205.  
  206. float dx1 = x2 - x1;
  207. float dy1 = y2 - y1;
  208. float distance1 = sqrt(dx1*dx1 + dy1*dy1);
  209. float minDist1 = diameter2/2 + diameter1/2;
  210. if (distance1 < minDist1) {
  211. float angle = atan2(dy1, dx1);
  212. float targetX1 = x1 + cos(angle) * minDist1;
  213. float targetY1 = y1 + sin(angle) * minDist1;
  214. float ax1 = (targetX1 - x2) * spring;
  215. float ay1 = (targetY1 - y2) * spring;
  216. vx1 -= ax1;
  217. vy1 -= ay1;
  218. x2 += ax1;
  219. y2 += ay1;
  220. }
  221.  
  222. float dx2 = x1 - x2;
  223. float dy2 = y1 - y2;
  224. float distance2 = sqrt(dx2*dx2 + dy2*dy2);
  225. float minDist2 = diameter1/2 + diameter2/2;
  226. if (distance2 < minDist2) {
  227. float angle = atan2(dy2, dx2);
  228. float targetX2 = x2 + cos(angle) * minDist2;
  229. float targetY2 = y2 + sin(angle) * minDist2;
  230. float ax2 = (targetX2 - x1) * spring;
  231. float ay2 = (targetY2 - y1) * spring;
  232. vx2 -= ax2;
  233. vy2 -= ay2;
  234. x1 += ax2;
  235. y1 += ay2;
  236. }
  237.  
  238.  
  239. //float angle = atan2(mouseX, mouseY);
  240.  
  241.  
  242.  
  243. fill(232,19,19);
  244. text(x1, 50, 50);
  245. text(y1, 50, 60);
  246. ellipse(x1, y1, diameter1, diameter1);
  247. fill(245,240,94);
  248. text(x2, 150, 50);
  249. text(y2, 150, 60);
  250. ellipse(x2, y2, diameter2, diameter2);
  251. }
Advertisement
Add Comment
Please, Sign In to add comment