Object within object, move function.
in
Programming Questions
•
11 months ago
Stupid topic, but here it goes:
Can anybody please put me on the right track?
I have a small creature and I would like to make many versions of it (array). Their pupils follow the mouse. I would like to add a move function, like a jiggle, but not break the pupil function (pupil following the mouse). What I've got so far:
- Susuwatari[] susues = new Susuwatari[1];
- SusuEyes eyes;
- float bx;
- float by;
- float swSize;
- void setup() {
- size(500, 500);
- smooth();
- swSize=40;
- bx=width/2;
- by=height/2;
- eyes = new SusuEyes(bx, by, swSize);
- for (int i = 0; i < susues.length; i++) {
- susues[i] = new Susuwatari(swSize, bx, by);
- }
- }
- void draw() {
- background(200);
- for (int i =0; i<susues.length; i++) {
- susues[i].display();
- }
- eyes.display();
- }
- class SusuEyes {
- float x;
- float y;
- PVector eye;
- PVector m;
- float easing;
- SusuEyes(float newBx, float newBy, float newSwSize) {
- bx = x = newBx;
- by = y = newBy;
- easing = 0.2;
- eye = new PVector(x, y);
- swSize = newSwSize;
- }
- void display() {
- fill(0);
- noStroke();
- m = new PVector(mouseX, mouseY);
- if (dist(m.x, m.y, eye.x, eye.y) > (swSize/3)/2) {
- m.sub(eye);
- m.normalize();
- m.mult((swSize/3)/2);
- m.add(eye);
- }
- x = x + (m.x - x) * easing;
- y = y + (m.y - y) * easing;
- ellipse(x-swSize/4.54, y-swSize/25, swSize/16, swSize/16);
- ellipse(x+swSize/4.54, y-swSize/25, swSize/16, swSize/16);
- }
- }
- class Susuwatari {
- float hair;
- Susuwatari(float newSwSize, float newBx, float newBy) {
- bx = newBx;
- by = newBy;
- swSize = newSwSize;
- hair = swSize*0.01;
- }
- void display() {
- //Body
- fill(50);
- noStroke();
- ellipse(bx, by, swSize, swSize);
- //filter(BLUR, 2);
- //Hair
- stroke(0);
- strokeWeight(hair);
- noFill();
- line(bx, by+swSize/5.5, bx, by+swSize/1.5);
- line(bx, by-swSize/3, bx, by-swSize/1.8);
- line(bx-swSize/2.2, by, bx-swSize/1.4, by);
- line(bx+swSize/2.2, by, bx+swSize/1.3, by);
- line(bx-swSize/20, by-swSize/3, bx-swSize/10, by-swSize/1.6);
- line(bx+swSize/20, by-swSize/3, bx+swSize/10, by-swSize/1.4);
- line(bx-swSize/20, by+swSize/5, bx-swSize/10, by+swSize/1.7);
- line(bx+swSize/20, by+swSize/5, bx+swSize/10, by+swSize/1.5);
- line(bx-swSize/2.2, by+swSize/20, bx-swSize/1.4, by+swSize/10);
- line(bx+swSize/2.2, by+swSize/20, bx+swSize/1.7, by+swSize/10);
- line(bx+swSize/2.2, by-swSize/20, bx+swSize/1.4, by-swSize/10);
- line(bx-swSize/2.2, by-swSize/20, bx-swSize/1.5, by-swSize/10);
- line(bx-swSize/2.3, by-swSize/10, bx-swSize/1.5, by-swSize/5);
- line(bx-swSize/2.2, by+swSize/10, bx-swSize/1.5, by+swSize/5);
- line(bx+swSize/2.3, by+swSize/10, bx+swSize/1.5, by+swSize/5);
- line(bx+swSize/2.2, by-swSize/10, bx+swSize/1.5, by-swSize/5);
- line(bx+swSize/2.5, by+swSize/6, bx+swSize/1.8, by+swSize/3);
- line(bx+swSize/2.3, by-swSize/6, bx+swSize/1.8, by-swSize/4);
- line(bx-swSize/2.3, by+swSize/6, bx-swSize/1.8, by+swSize/4);
- line(bx-swSize/2.4, by-swSize/6, bx-swSize/1.8, by-swSize/3);
- line(bx-swSize/2.8, by-swSize/4, bx-swSize/2.2, by-swSize/3);
- line(bx+swSize/3.2, by+swSize/4.5, bx+swSize/2.2, by+swSize/2.6);
- line(bx-swSize/2.7, by+swSize/4.5, bx-swSize/2.2, by+swSize/2.6);
- line(bx-swSize/3.4, by-swSize/3.5, bx-swSize/2.4, by-swSize/2);
- line(bx+swSize/3.4, by-swSize/3.5, bx+swSize/2.8, by-swSize/2.4);
- line(bx+swSize/2.6, by-swSize/4.5, bx+swSize/2, by-swSize/2.6);
- line(bx+swSize/3.8, by+swSize/3.8, bx+swSize/2.4, by+swSize/2);
- line(bx-swSize/3.8, by+swSize/3.8, bx-swSize/2.8, by+swSize/2.2);
- line(bx-swSize/4.2, by-swSize/3.2, bx-swSize/3, by-swSize/2.2);
- line(bx+swSize/4.4, by-swSize/3.2, bx+swSize/3.2, by-swSize/1.8);
- line(bx-swSize/4.4, by+swSize/3.2, bx-swSize/3.2, by+swSize/1.8);
- line(bx+swSize/4.4, by+swSize/3.2, bx+swSize/3.2, by+swSize/2);
- line(bx+swSize/6.5, by+swSize/3.8, bx+swSize/5, by+swSize/1.8);
- line(bx-swSize/7, by+swSize/3.8, bx-swSize/5, by+swSize/1.6);
- line(bx+swSize/7, by-swSize/3.3, bx+swSize/5, by-swSize/1.6);
- line(bx-swSize/7, by-swSize/3.3, bx-swSize/4.2, by-swSize/1.6);
- //Eyes
- fill(255);
- noStroke();
- //eye#1
- beginShape();
- vertex(bx-swSize/20, by);
- bezierVertex(bx-swSize/10, by+swSize/4, bx-swSize/2.5, by+swSize/5, bx-swSize/2.5, by);
- bezierVertex(bx-swSize/2.5, by-swSize/3, bx, by-swSize/3, bx-swSize/20, by);
- endShape(CLOSE);
- //eye#2
- beginShape();
- vertex(bx+swSize/20, by);
- bezierVertex(bx+swSize/10, by+swSize/4, bx+swSize/2.5, by+swSize/5, bx+swSize/2.5, by);
- bezierVertex(bx+swSize/2.5, by-swSize/3, bx, by-swSize/3, bx+swSize/20, by);
- endShape(CLOSE);
- }
- }
1