how do I control a shape with mouse and change the coordinates of the shape ?

hi all, I want to create a PShape that will be moved around on screen with the mouse. What is more , I want to be able all its vertices to be moved according to mouse movement so as to use this information in a function. So far I have come across the PShape::translate() member function, and while this does the translation for drawing purposes, the vertices of the shapes do not change in value.

PShape sh;

void setup() {
  size(640,360,P2D);

  sh = createShape();
  sh.setStroke(color(0));
  sh.setStrokeWeight(1);
  sh.setFill(color(0,0,0,0));
  sh.beginShape();
  sh.vertex(-25, -25);
  sh.vertex(-25, 25);
  sh.vertex(25, 25);
  sh.vertex(25, -25);
  sh.endShape(CLOSE);
}

void draw() {
  background(255);

  float x=mouseX;
  float y=mouseY;
  sh.translate(x,y);

  functionThatUsesPShape(sh); //function where I need the vertices of the PShape
  shape(sh); //draw the shape on screen

  sh.translate(-x,-y); //undo the translation because it is cumulative
}

inside functionThatUsesPShape() I println the values of vertex(0) of my PShape, but its coordinates are stuck at (-25,-25), so no changes. what do I do wrong?

thank you for your help

Tagged:

Answers

Sign In or Register to comment.