Fluid/Jelly (ish) Simulation using Actionscript 3 / Flash


/ Published in: ActionScript 3
Save to your folder(s)

Description & download at http://adamcoulombe.info/lab/as3/jellybox.html


Copy this code and paste it in your HTML
  1. import traer.physics.Attraction;
  2. import traer.physics.Particle;
  3. import traer.physics.ParticleSystem;
  4. import traer.physics.Spring;
  5.  
  6. import com.cartogrammar.drawing.CubicBezier;
  7.  
  8. import flash.geom.Vector3D;
  9.  
  10. var s:ParticleSystem = new ParticleSystem(new Vector3D(0, 0, 0), .2);
  11. //var waterMolecules = new Array();
  12. var waterParticles = new Array();
  13. var sky = new Array();
  14. var earth = new Array();
  15. var magnet = s.makeParticle(20, new Vector3D(0, 0, 0));
  16. var hover=new Molecule();
  17. var points = new Array();
  18. var water = new Sprite();
  19.  
  20. init();
  21.  
  22. function init(){
  23. magnet.makeFixed();
  24. for (var i=0; i<20; i++){
  25. sky[i]=s.makeParticle(10, new Vector3D(90+(i*20), 100, 0)); sky[i].makeFixed();
  26. waterParticles[i]=s.makeParticle(0.5, new Vector3D(90+(i*20), 200, 0));
  27. earth[i]=s.makeParticle(10, new Vector3D(90+(i*20), 300, 0)); earth[i].makeFixed();
  28. //waterMolecules[i]=new Molecule();
  29. //addChild(waterMolecules[i]);
  30. if(i>0){
  31. s.makeSpring(waterParticles[i-1], waterParticles[i], 0.1 , .05, 0);
  32. s.makeSpring(sky[i], waterParticles[i], 0.05 , .05, 0);
  33. s.makeSpring(earth[i], waterParticles[i], 0.05 , .05, 0);
  34. if(i<19){
  35. s.makeAttraction(magnet, waterParticles[i], -500 , 0);
  36. }
  37. }
  38. }
  39. waterParticles[0].makeFixed();
  40. waterParticles[19].makeFixed();
  41. addChild(water);
  42. water.alpha = 0.8
  43. stage.addEventListener(Event.ENTER_FRAME,loop);
  44. }
  45.  
  46. function loop(e){
  47. s.tick(1);
  48. magnet.position.x = hover.x = mouseX;
  49. magnet.position.y = hover.y = mouseY;
  50.  
  51. water.graphics.clear();
  52. water.graphics.beginFill(0x007693);
  53. for(var i in waterParticles){
  54. points[i] = new Point(waterParticles[i].position.x, waterParticles[i].position.y);
  55. //waterMolecules[i].x = points[i].x,
  56. //waterMolecules[i].y = points[i].y
  57. }
  58. CubicBezier.curveThroughPoints(water.graphics,points);
  59. water.graphics.lineTo(points[0].x,stage.stageHeight-50);
  60.  
  61.  
  62. water.graphics.lineTo(points[19].x,points[19].y);
  63. water.graphics.lineTo(points[19].x,stage.stageHeight-50);
  64. water.graphics.lineTo(points[0].x,stage.stageHeight-50);
  65.  
  66. }

URL: http://adamcoulombe.info/lab/as3/jellybox.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.