...
Create a new program where a simple car follows the mouse on the screen from left to right. The car should be drawn from the side, and include wheels that rotate. You may use the example code to get started.
...
Code Block | ||
---|---|---|
| ||
int rotation; void setup() { size(900, 400); // def. window size } void draw() { rotation++; } void car(int x, int y) { fill(100, 0, 0); noStroke(); beginShape(); vertex(0, 0); vertex(5, -50); vertex(50, -50); vertex(70, -80); vertex(150, -80); vertex(190, -50); vertex(265, -45); vertex(270, 0); vertex(0, 0); endShape(); wheel(60, 0); wheel(210, 0); } void wheel(int x, int y) { int radius = 25; fill(0, 100, 0150); stroke(0); strokeWeight(7); ellipse(0, 0, radius*2, radius*2); strokeWeight(4); line(0-radius, 0, 0+radius, 0); line (0, 0-radius, 0, 0+radius); noStroke(); } |
Possible Solution:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
int rotation; intboolean lastMousePos;forward int= mouseMovementtrue; int boolean forwardxScale = true1; void setup() { size(900, 400); } void draw() { // def. window size } void draw() { background(255); int mouseMovement = mouseX-lastMousePospmouseX; // println(mouseX-lastMousePos); background(255); //wheel(width/2, height/2); car(mouseX, height/2); lastMousePos = mouseX; } void car(int x, int y) { rotation+= abs(mouseMovement); pushMatrix(); translate(x, y); if (mouseMovement<0) { forward = false; } pmousex gives us our mouse x value from the last frame. Subtracting from the current position give us the distance moved per frame if (mouseMovement<0) { // if the movement is negative, then we flip everthing backwards with a negative xcale xScale = -1; } if (mouseMovement>0) { // if the movement forwardis =positive, true;then our xscale }is positive xScale if(forward == false) { = 1; } rotation+= scale(-1,1abs(mouseMovement); car(mouseX, height/2); } void car(int x, int y) { println pushMatrix(forward); filltranslate(100x, 0y); scale(xScale, 01); noStroke fill(100); beginShape(); vertex(0, 0); vertex(5, -50); vertex(50, -50); vertex(70, -80); vertex(150, -80); vertex(190, -50); vertex(265, -45); vertex(270, 0); vertex(0, 0); endShape(); wheel(60, 0); wheel(210, 0); popMatrix(); } void wheel(int x, int y) { pushMatrix()int radius = 25; translate(x, yfill(150); rotatestroke(radians(rotation0)); int radius = 25pushMatrix(); filltranslate(0x, 100, 0y); stroke(0rotate(radians(rotation)); strokeWeight(7); ellipse(0, 0, radius*2, radius*2); strokeWeight(4); line(0-radius, 0, 0+radius, 0); line (0, 0-radius, 0, 0+radius); noStroke(); popMatrix(); } |
...