Loading...
Logo
Processing Forum
hi there, in my code i have ellipses that travel accross the page, then when mouse is clicked rotate around my mouse point. however i also have a image that follows my mouse and i want that to stay still when i click. however i cannot find the right way to format my code so this does that, any ideas??


Copy code
  1. PImage bg;
  2. PImage mi;
  3. float tx = 0;
  4. float ty = 0;
  5. static final byte   dotCount = 15;
  6. static final byte   speed = 11;
  7. static final byte   size = 20;
  8. //
  9. static float angle = 0;
  10. //
  11. static final int[]  xn = new int [dotCount];
  12. static final int[]  yn = new int [dotCount];
  13. static final byte[] sn = new byte[dotCount];
  14. //
  15. void setup() {
  16.   size(1000, 500);
  17.   frameRate(60);
  18.   smooth();
  19.   noStroke();
  20.   bg = loadImage("space.png");
  21.   mi = loadImage("mouse.png");

  22.   background(bg);
  23.   //


  24.   //loop that continusly puts random numbers into the 'boxes' in the arays. saves
  25.   //time then putting them in one at a time.
  26.   for (byte i=0; i<dotCount; i++) {
  27.     xn[i] = (int) random (width);
  28.     yn[i] = (int) random (height);
  29.     sn[i] = (byte)random (1, speed);
  30.   }
  31. }

  32. //
  33. void draw() {
  34.   background (bg);


  35. //makes ellipses rotate around mouseX,mouseY

  36.   if (mousePressed) {
  37.     translate(mouseX, mouseY);
  38.     rotate( angle += PI/72 );
  39.     translate(-mouseX, -mouseY);
  40.   }
  41.   //
  42.   for (byte i=0; i<dotCount; i++) {
  43.     ellipse (xn[i], yn[i], size, size);
  44.     xn[i] = ( xn[i] + sn[i] ) % width;
  45.   }
  46. //spaceship that follows mouse
  47.   tx += (mouseX-tx)/40.0;
  48.   ty += (mouseY-ty)/40.0;


  49.   {   
  50.     translate(width/tx, height/ty);


  51.     angle = atan2(mouseY-width/40, mouseX-height/40);
  52.     rotate(angle);
  53.     image(mi, tx-50, ty+50, tx+50, ty+50);
  54.   }
  55. }

Replies(3)

Not too sure of what you ask (I am tired), but you can take a look at pushMatrix() and popMatrix().
i want the ellipses that are programed to rotate in a continous circle while the mouse is clicked, but the image mi to stay linked to the mouse. at the moment they are both just moving with the mouse when is been clicked?


as said put
Copy code
  1. pushMatrix()
before the
ellipses output (line 43)
and
Copy code
  1. popMatrix()
after it (after 64).

or similar...