Processing uses a Cartesian coordinate system. It is important to note that the zero point is in the upper left corner in processing. The X axis is from left to right and the Y axis is from top to bottom. While this might seem odd, it comes from the tradition that bitmaps were always read in that order. In computer graphics the cartesian coordinate system is almost always used. This provides some convenience, so you can also move, scale, or even rotate coordinate systems. I can
In processing we can simplify the drawing of objects in many cases by assigning them their own local coordinate system. Imagine an animation of a car . The wheels should rotate naturally. moving across the screen. If you draw this per in processing you must always add the current position of the body to the wheels and then turn them. If you do this with the change of the coordinate system, this is a lot simpler. Here you just have to move the coordinate system, draw the car body and then move the coordinate system again for the respective wheelscould add a value to the the x and y coordinates for every detail of the car to move it across the screen. When it comes to the wheels, it would get complicated as we would not only change the position relative to the car, but rotate every detail in relation to the wheel axis! But if we modify the coordinate systems this becomes a lot simpler. Such coordinate system changes are called transformations. There are three basic types:
...
These two commands work according to the stack principle. This is an very old form of working with computer memory. Just imagine a stack of graph paper: when you call pushMatrix, the stack is increased by one sheet. On this page we would describe the current data of our coordinate system. Now you can rotate, scale and transform as you want. If you now want to bring the coordinate system back to the position of the last pushMatrix call, you must call popMatrix. Now this sheet is removed from the stack.
With an example program, this should be easer to understoodunderstand:
Code Block |
---|
void setup() { size(400,400); // def. window size strokeWeight(15); // line thickness } void draw() { background(255); // def. background colour float radius = dist(mouseX,mouseY,width/2,height/2); // calculate the distance from the mouse curser to the center of the screen radius = map(radius,0,width,1,4); // modify the radius to keep it within a specific range. pushMatrix(); translate(200,200); rotate(calcAngle()); scale(radius); smiley(); // function call popMatrix(); pushMatrix(); translate(30,30); scale(.2); smiley(); // function call popMatrix(); } // funktion void smiley() { noFill(); ellipse(0,0,180,180); // head fill(0); ellipse(0 - 30,0 - 30,20,20); // left eye ellipse(0 + 30,0 - 30,20,20); // right eye noFill(); arc(0,0,100,100,radians(20),radians(180-20)); // mouth } // calculate the angle from the screen middle to the mouse cursor // the angle is in radians float calcAngle() { return -atan2(mouseX - (width / 2),mouseY - (height / 2)); } |
Hier gibts gleich mehrere Neuigkeiten, Linie This example introduces a couple of new things starting on line 13:
float radius = dist(mouseX,mouseY,width/2,height/2);
Hier wird die Distanz vom Mauszeiger zur Fenstermitte ermittelt(Siehe Here the distance from the mouse pointer to the window centre is determined (see Pythagoras).Linie 14:
radius = map(radius,0,width,1,4);
Hier eine Zahl auf einen anderen Zahlenbereich gemapt. Der Original-Bereich ist von 0-width. Der Ziel-Bereich ist von The original range is from 0 to the width of the window. The target range is from 1-4. Nun wird radius von Original-Bereich auf den Ziel-Bereich transformiert.
Aufgaben
Schreib ein Programm welches einen Smiley am Mauszeiger folgen lässt. Dazu soll ein kleineres Smiley um den grossen Smiley in einer Umlaufbahn kreisen.Now radius is transformed from original range to the target range.
-atan2(mouseX - (width / 2),mouseY - (height / 2));
The atan2 function returns the angle in radians at a given coordinate from the 0 point. In this example we shift the 0 point to the centre of the screen by subtracting width/2 from X and height/2 from Y.
Image Credit: wikipedia
Exercise 8
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);
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(150);
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;
boolean forward = true;
int xScale = 1;
void setup()
{
size(900, 400);
}
void draw()
{
background(255);
int mouseMovement = mouseX-pmouseX; //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 is positive, then our xscale is positive
xScale = 1;
}
rotation+= abs(mouseMovement);
car(mouseX, height/2);
}
void car(int x, int y) {
pushMatrix();
translate(x, y);
scale(xScale, 1);
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) {
int radius = 25;
fill(150);
stroke(0);
pushMatrix();
translate(x, y);
rotate(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();
}
|