Processing IDE Dersleri | Processing IDE Kurulumu | Processing IDE Programlama Dersleri
Vücudun hareketini kontrol etmek için vektörlerin kullanılmasının gösterilmesi Bu örnek, nesneye yönelik değildir Bir nesnedeki vektörleri kullanarak hareketin nasıl simüle edileceğine ilişkin bir örnek için AccelerationWithVectors'a bakın.
PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape's acceleration
void setup() {
size(640,360);
location = new PVector(100,100);
velocity = new PVector(1.5,2.1);
gravity = new PVector(0,0.2);
}
void draw() {
background(0);
// Add velocity to the location.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);
// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) {
// We're reducing velocity ever so slightly
// when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
location.y = height;
}
// Display circle at location vector
stroke(255);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}
Parçacıklar her döngüde draw () aracılığıyla üretilir, yerçekimiyle düşer ve zamanla kaybolur Bir ParticleSystem nesnesi, değişken boyutlu (ArrayList) bir parçacık listesini yönetir.
ParticleSystem ps;
void setup() {
size(640, 360);
ps = new ParticleSystem(new PVector(width/2, 50));
}
void draw() {
background(0);
ps.addParticle();
ps.run();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
stroke(255, lifespan);
fill(255, lifespan);
ellipse(position.x, position.y, 8, 8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
Daniel Shiffman'dan Çoklu Parçacık Sistemleri. * Fare konumunda bir parçacık patlaması oluşturmak için fareye tıklayın. * Her patlama, Parçacıklar ve Çılgın Parçacıklar (Parçacık'ın bir alt sınıfı) içeren bir parçacık sisteminin bir örneğidir. Kalıtım ve Polimorfizm'in kullanımına burada dikkat edin.
ArrayList<ParticleSystem> systems;
void setup() {
size(640, 360);
systems = new ArrayList<ParticleSystem>();
}
void draw() {
background(0);
for (ParticleSystem ps : systems) {
ps.run();
ps.addParticle();
}
if (systems.isEmpty()) {
fill(255);
textAlign(CENTER);
text("click mouse to add particle systems", width/2, height/2);
}
}
void mousePressed() {
systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY)));
}
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ParticleSystem(int num, PVector v) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.copy(); // Store the origin point
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards, because we are deleting while iterating
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
void addParticle() {
Particle p;
// Add either a Particle or CrazyParticle to the system
if (int(random(0, 2)) == 0) {
p = new Particle(origin);
}
else {
p = new CrazyParticle(origin);
}
particles.add(p);
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
return particles.isEmpty();
}
}
// A subclass of Particle
class CrazyParticle extends Particle {
// Just adding one new variable to a CrazyParticle
// It inherits all other fields from "Particle", and we don't have to retype them!
float theta;
// The CrazyParticle constructor can call the parent class (super class) constructor
CrazyParticle(PVector l) {
// "super" means do everything from the constructor in Particle
super(l);
// One more line of code to deal with the new variable, theta
theta = 0.0;
}
// Notice we don't have the method run() here; it is inherited from Particle
// This update() method overrides the parent class update() method
void update() {
super.update();
// Increment rotation based on horizontal velocity
float theta_vel = (velocity.x * velocity.mag()) / 10.0f;
theta += theta_vel;
}
// This display() method overrides the parent class display() method
void display() {
// Render the ellipse just like in a regular particle
super.display();
// Then add a rotating line
pushMatrix();
translate(position.x, position.y);
rotate(theta);
stroke(255, lifespan);
line(0, 0, 25, 0);
popMatrix();
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(255, lifespan);
fill(255, lifespan);
ellipse(position.x, position.y, 8, 8);
}
// Is the particle still useful?
boolean isDead() {
return (lifespan < 0.0);
}
}
Özyineleme yoluyla basit bir ağaç benzeri yapı oluşturur. Dallanma açısı, yatay fare konumunun bir fonksiyonu olarak hesaplanır. Açıyı değiştirmek için fareyi sola ve sağa hareket ettirin.
float theta;
void setup() {
size(640, 360);
}
void draw() {
background(0);
frameRate(30);
stroke(255);
// Let's pick an angle 0 to 90 degrees based on the mouse position
float a = (mouseX / (float) width) * 90f;
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2,height);
// Draw a line 120 pixels
line(0,0,0,-120);
// Move to the end of that line
translate(0,-120);
// Start the recursive branching!
branch(120);
}
void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
popMatrix();
}
}
Bir dizi görüntünün gösterilmesi hareket yanılsaması yaratır. On iki görüntü yüklenir ve her biri bir döngüde ayrı ayrı görüntülenir.
int numFrames = 12; // The number of frames in the animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
void setup() {
size(640, 360);
frameRate(24);
images[0] = loadImage("PT_anim0000.gif");
images[1] = loadImage("PT_anim0001.gif");
images[2] = loadImage("PT_anim0002.gif");
images[3] = loadImage("PT_anim0003.gif");
images[4] = loadImage("PT_anim0004.gif");
images[5] = loadImage("PT_anim0005.gif");
images[6] = loadImage("PT_anim0006.gif");
images[7] = loadImage("PT_anim0007.gif");
images[8] = loadImage("PT_anim0008.gif");
images[9] = loadImage("PT_anim0009.gif");
images[10] = loadImage("PT_anim0010.gif");
images[11] = loadImage("PT_anim0011.gif");
// If you don't want to load each image separately
// and you know how many frames you have, you
// can create the filenames as the program runs.
// The nf() command does number formatting, which will
// ensure that the number is (in this case) 4 digits.
//for (int i = 0; i < numFrames; i++) {
// String imageName = "PT_anim" + nf(i, 4) + ".gif";
// images[i] = loadImage(imageName);
//}
}
void draw() {
background(0);
currentFrame = (currentFrame+1) % numFrames; // Use % to cycle through frames
int offset = 0;
for (int x = -100; x < width; x += images[0].width) {
image(images[(currentFrame+offset) % numFrames], x, -20);
offset+=2;
image(images[(currentFrame+offset) % numFrames], x, height/2);
offset+=2;
}
}
Animasyonları değiştirmek için fare düğmesine basın. GIF resimlerini yüklemeyi, görüntülemeyi ve canlandırmayı gösterir. Animasyonlu GIF'leri görüntülemek için bir program yazmak kolay olurdu, ancak görüntüleme sırası ve görüntüleme hızı üzerinde çok fazla kontrole izin vermezdi.
Animation animation1, animation2;
float xpos;
float ypos;
float drag = 30.0;
void setup() {
size(640, 360);
background(255, 204, 0);
frameRate(24);
animation1 = new Animation("PT_Shifty_", 38);
animation2 = new Animation("PT_Teddy_", 60);
ypos = height * 0.25;
}
void draw() {
float dx = mouseX - xpos;
xpos = xpos + dx/drag;
// Display the sprite at the position xpos, ypos
if (mousePressed) {
background(153, 153, 0);
animation1.display(xpos-animation1.getWidth()/2, ypos);
} else {
background(255, 204, 0);
animation2.display(xpos-animation1.getWidth()/2, ypos);
}
}
// Class for animating a sequence of GIFs
class Animation {
PImage[] images;
int imageCount;
int frame;
Animation(String imagePrefix, int count) {
imageCount = count;
images = new PImage[imageCount];
for (int i = 0; i < imageCount; i++) {
// Use nf() to number format 'i' into four digits
String filename = imagePrefix + nf(i, 4) + ".gif";
images[i] = loadImage(filename);
}
}
void display(float xpos, float ypos) {
frame = (frame+1) % imageCount;
image(images[frame], xpos, ypos);
}
int getWidth() {
return images[0].width;
}
}