使用Actionscript 3/Flash进行流体/果冻(ish)模拟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Actionscript 3/Flash进行流体/果冻(ish)模拟相关的知识,希望对你有一定的参考价值。

Description & download at http://adamcoulombe.info/lab/as3/jellybox.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. }

以上是关于使用Actionscript 3/Flash进行流体/果冻(ish)模拟的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 Actionscript 3 / Flash中的半色调效果

ActionScript 3 Flash:使用AS3参数添加编译器设置

Actionscript 3 Flash 键盘事件

动画未在 actionscript 3.0 (flash cc) 中启动

ActionScript 3 flash中FLV视频的actionscript cuepoint监听器

ActionScript 3 Flash中的Maskenverlauf