Guest User

Processing spawn and extra control window

a guest
Nov 28th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. import toxi.geom.*;
  2. import peasy.*;
  3. import controlP5.*;
  4.  
  5. ControlFrame cf;
  6. PeasyCam cam;
  7. Stick bob;
  8.  
  9. ArrayList <Stick> allBobs;
  10.  
  11. void settings() {
  12. size(800,600,P3D);
  13. }
  14. void setup(){
  15. cf = new ControlFrame(this, 300, 400, "Controls");
  16.  
  17. smooth();
  18. allBobs = new ArrayList <Stick> ();
  19.  
  20. cam = new PeasyCam(this,100);
  21.  
  22.  
  23. }
  24.  
  25. void draw(){
  26. background(0);
  27. allBobs.clear();
  28.  
  29.  
  30. Vec3D v = new Vec3D (0,0,0);
  31. Vec3D iniVel = new Vec3D (10,0,0);
  32. bob = new Stick(v,iniVel,ITE,"A");
  33.  
  34. allBobs.add(bob);
  35. stroke(255);
  36. noFill();
  37. strokeWeight(1);
  38. // box(600);
  39.  
  40. for(Stick b : allBobs){
  41. b.run();
  42.  
  43. }
  44. }
  45.  
  46. //
  47.  
  48. class Stick{
  49.  
  50. Vec3D loc;
  51. Vec3D oriLoc;
  52. Vec3D vel;
  53.  
  54. int generations;
  55. String type;
  56.  
  57. Stick(Vec3D _loc, Vec3D _vel, int _generations, String _type){
  58.  
  59. loc = _loc;
  60. oriLoc = _loc.copy();
  61. vel = _vel;
  62. generations = _generations;
  63. type =_type;
  64.  
  65.  
  66. //stack of functions that get executed only once
  67. updateDir();
  68. updateLoc();
  69. spawn();
  70.  
  71.  
  72. }
  73.  
  74. void run(){
  75. display();
  76. }
  77.  
  78. void spawn(){
  79.  
  80. if (generations >0){
  81.  
  82. if(type =="A"){
  83. Vec3D v = loc.copy();
  84. Vec3D iniVel = vel.copy();
  85. Stick newBob = new Stick(v,iniVel,generations-1,"A");
  86. allBobs.add(newBob);
  87.  
  88. Vec3D v2 = loc.copy();
  89. Vec3D iniVel2 = vel.copy();
  90. Stick newBob2 = new Stick(v2,iniVel2,generations-1,"B");
  91.  
  92. allBobs.add(newBob2);
  93.  
  94. }
  95.  
  96. if(type =="B"){
  97. Vec3D v = loc.copy();
  98. Vec3D iniVel = vel.copy();
  99. Stick newBob = new Stick(v,iniVel,generations-1,"C");
  100. allBobs.add(newBob);
  101.  
  102. }
  103.  
  104. if(type =="C"){
  105. Vec3D v = loc.copy();
  106. Vec3D iniVel = vel.copy();
  107. Stick newBob = new Stick(v,iniVel,generations-1,"A");
  108. allBobs.add(newBob);
  109.  
  110. }
  111. }
  112.  
  113.  
  114. }
  115.  
  116. void updateDir(){
  117.  
  118. if(type == "A"){
  119. float angle1 = radians(0);
  120. float angle2 = radians(ANGLE1);
  121. float angle3 = radians(0);
  122. vel.rotateX(angle1);
  123. vel.rotateY(angle2);
  124. vel.rotateZ(angle3);
  125. vel.normalize();
  126. vel.scaleSelf(100);
  127. }
  128.  
  129. if(type == "B"){
  130. float angle1 = radians(0);
  131. float angle2 = radians(0);
  132. float angle3 = radians(ANGLE2);
  133. vel.rotateX(angle1);
  134. vel.rotateY(angle2);
  135. vel.rotateZ(angle3);
  136. vel.normalize();
  137. vel.scaleSelf(20);
  138. }
  139. if(type == "C"){
  140. float angle1 = radians(ANGLE3);
  141. float angle2 = radians(0);
  142. float angle3 = radians(0);
  143. vel.rotateX(angle1);
  144. vel.rotateY(angle2);
  145. vel.rotateZ(angle3);
  146. vel.normalize();
  147. vel.scaleSelf(40);
  148. }
  149. }
  150.  
  151. void updateLoc(){
  152. loc.addSelf(vel);
  153. }
  154.  
  155. void display(){
  156. stroke(255,0,0);
  157. strokeWeight(4);
  158. point(loc.x,loc.y,loc.z);
  159. stroke(255);
  160. strokeWeight(1);
  161. line(loc.x,loc.y,loc.z,oriLoc.x, oriLoc.y,oriLoc.z);
  162.  
  163. }
  164. }
  165.  
  166. //
  167.  
  168.  
  169. float ANGLE1 = 0, ANGLE2 = 0, ANGLE3 = 0;
  170. int ITE=0;
  171.  
  172. //void ui(){
  173. //controlP5 = new ControlP5(this);
  174. // controlWindow = controlP5.begin("controlP5window",100,100,400,200);
  175. //Slider s01 = controlP5.addSlider("ANGLE1", 0, 90, 20, 20, 100, 10);
  176. //s01.setWindow(controlWindow);
  177. //}
  178.  
  179. class ControlFrame extends PApplet {
  180.  
  181. int w, h;
  182. String name;
  183. PApplet parent;
  184. ControlP5 cp5;
  185.  
  186. public ControlFrame(PApplet _parent, int _w, int _h, String _name) {
  187. super();
  188. parent = _parent;
  189. w=_w;
  190. h=_h;
  191. name=_name;
  192. PApplet.runSketch(new String[]{this.getClass().getName()}, this);
  193. }
  194.  
  195. public void settings() {
  196. size(w, h);
  197. }
  198.  
  199. public void setup() {
  200. surface.setLocation(10, 10);
  201. cp5 = new ControlP5(this);
  202.  
  203. cp5.addSlider("ANGLE1")
  204. .plugTo(parent, "ANGLE1")
  205. .setRange(-180, 180)
  206. .setValue(90)
  207. .setPosition(50, 50)
  208. .setSize(200, 30);
  209. cp5.addSlider("ANGLE2")
  210. .plugTo(parent, "ANGLE2")
  211. .setRange(-180, 180)
  212. .setValue(90)
  213. .setPosition(50, 100)
  214. .setSize(200, 30);
  215. cp5.addSlider("ANGLE3")
  216. .plugTo(parent, "ANGLE3")
  217. .setRange(-180, 180)
  218. .setValue(90)
  219. .setPosition(50, 150)
  220. .setSize(200, 30);
  221. cp5.addSlider("ITE")
  222. .plugTo(parent, "ITE")
  223. .setRange(0, 30)
  224. .setValue(10)
  225. .setPosition(50, 200)
  226. .setSize(200, 30);
  227. }
  228. void draw() {
  229. background(190);
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment