{"id":29,"date":"2011-10-27T17:53:10","date_gmt":"2011-10-28T00:53:10","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=29"},"modified":"2011-10-27T17:56:13","modified_gmt":"2011-10-28T00:56:13","slug":"falling-cats","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/10\/falling-cats\/","title":{"rendered":"Falling Cats!"},"content":{"rendered":"<p><script type=\"application\/processing\">PImage  img;\n\nint    nbrParticles = 100;\n\nfloat  damping = .95;       \/\/ lower values act like cats are moving through more viscous fluid\nfloat  gravity = .1;        \/\/ higher values make cats drop faster, negative values makes them rise\nfloat  drift = .5;          \/\/ amount of brownian drift\nfloat  maxSpin = radians(9);\/\/ fastest spin speed is 9 degrees\n\nclass Particle\n{\n  float vx, vy, vspin;  \/\/ velocities - added\n  float x, y, spin;\n\n  Particle() {\n    x = random(width);\n    y = random(height);\n    vspin = random(-maxSpin, maxSpin);\n    spin = random(TWO_PI);\n    vx = 0;\n    vy = 0;\n  }\n\n  void render() {\n    pushMatrix();\n      translate(x,y);\n      rotate(spin);\n      image(img, -img.width\/2, -img.height\/2);\n    popMatrix();\n  }\n}\nParticle[] myFlakes = new Particle[nbrParticles];\n\nvoid setup()\n{\n    size(400,400);\n    img = loadImage(\"\/funny_cat_60.jpg\");\n    background(0);\n    for (int i=0; i < nbrParticles; ++i) {\n      myFlakes[i] = new Particle();\n    }\n}\n\nvoid draw()\n{\n    background(255);\n    for (int i=0; i < nbrParticles; ++i) {\n      Particle f = myFlakes[i];\n      \n      f.render();\n\n      \/\/ application of forces to velocities\n      f.vy += random(-drift,+drift);  \/\/ brownian motion\n      f.vx += random(-drift,+drift);\n      f.vy += gravity;                \/\/ gravity\n\n      \/\/ velocity affects position\n      f.x += f.vx;   \/\/ add vx to x\n      f.y += f.vy;   \/\/ add vy to y\n\n      \/\/ constant spin rate\n      f.spin += f.vspin;\n      \n      \/\/ apply damping (air friction) to movement velocities\n      f.vx *= damping;  \/\/ shortcut for vx = vx * damping\n      f.vy *= damping;\n\n      \/\/ wrap around\n      if (f.y > height+50) {\n        f.y = -50;\n      }\n      if (f.x > width+50) {\n        f.x = -50;\n      }\n      if (f.x < -50) {\n        f.x = width+50;\n      }\n    }\n}\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=falling_cats\">source<\/a><\/div>\n<p>First Post!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>source First Post!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":4,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}