We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Need Help on interaction part!
Page Index Toggle Pages: 1
Need Help on interaction part! (Read 471 times)
Need Help on interaction part!
May 2nd, 2010, 5:26am
 
it is my final year project and here is the long code. I am almost finish it but getting stuck on the final part for few days already!!!Sad:(Sad

After typing the texts, they appeared and fell down. I want to make them as like as the project" rain text"





main tab

Catcher catcher;    // One catcher object
Timer timer;        // One timer object
Drop[] drops;       // An array of drop objects
int totalDrops = 0; // totalDrops
int noDrops = 0;
String s;
int noStore=0;
int noSpacebar=0;

PFont font;


String typing = " ";

String saved = " ";
int charcount = 0;


String store =  "";

import processing.video.*;

Capture video;

PImage backgroundImage;


void setup() {
 size(400,400);
 smooth();
 catcher = new Catcher(32); // Create the catcher with a radius of 32
 drops = new Drop[1000];    // Create 1000 spots in the array
 timer = new Timer(300);   // Create a timer that goes off every 2 seconds
 timer.start();

 font = createFont("Helvetica-Bold-24.vlw",20,true);
 textFont(font);
 s = "";
 drops[0]=new Drop(s);


 video = new Capture(this, width, height, 30);
 // Create an empty image the same size as the video
 backgroundImage = createImage(video.width,video.height,RGB);

 noStroke();
 background(255);
 fill(0,255,0);


}

void draw() {
 background(255);
 if (video.available()) {
   video.read();
 }

 loadPixels();
 video.loadPixels();
 backgroundImage.loadPixels();



 checkCol();


 updatePixels();
 catcher.setLocation(mouseX,mouseY);
 catcher.display();


 if (timer.isFinished()) {
   totalDrops ++ ;
   if (totalDrops >= drops.length) {
     totalDrops = 0;
   }
   timer.start();
 }
 if (noSpacebar >0){
   for (int i = 0; i < noSpacebar; i++ ) {

     drops[i].move();

     drops[i].display();
     if (catcher.intersect(drops[i])) {
       drops[i].caught();
       store += i+" ";

       noStore+=1;
       //println(store);
     }
   }
 }
}

//void words(){
void keyPressed(){
 typing+=key;
 if (keyCode ==32) {
   println(keyCode);
   noSpacebar+=1;
   //     }
   String[] typedList = split(typing, ' ');
   for (int i = 0; i < noSpacebar; i++ ) {
     String s= typedList[i];
     drops[noSpacebar] = new Drop(s);
   }
 }
}


void mousePressed() {

 backgroundImage.copy(video,0,0,video.width,video.height,0,0,video.width,video.h
eight);
 backgroundImage.updatePixels();
}

public void stop(){
 video.stop();//stop the object
 super.stop();
}

catcher tab

class Catcher {
 float r;   // radius
 color col; // color
 float x,y; // location


 Catcher(float tempR) {

   r = tempR;
   col = color(50,10,10,150);
   x = 0;
   y = 0;
 }

 void setLocation(float tempX, float tempY) {
   x = tempX;
   y = tempY;
 }

 void display() {
   stroke(1);
   strokeWeight(3);
   fill(255);
   ellipse(x,y,r*2,r*2);
 }

 // A function that returns true or false based on
 // if the catcher intersects a raindrop
 boolean intersect(Drop d) {
   // Calculate distance
   float distance = dist(x,y,d.x,d.y);

   // Compare distance to sum of radii
   if (distance < r + d.r) {
     return true;
   }
   else {
     return false;
   }
 }
}


drop tab


class Drop {

 float x,y;   // Variables for location of raindrop
 float speed; // Speed of raindrop
 color c;
 float r, f;     // Radius of raindrop
 String typeD;
 String stored;

 Drop(String s) {
   typeD = s;
   r = 8;                 // All raindrops are the same size
   x = random(width);     // Start with a random x location
   y = -r*4;              // Start a little above the window
   speed = random(0.3,0.9);   // Pick a random speed
   c = color(50,100,150);
  f= random(14,24);
 }

 // Move the raindrop down
 void move() {
   // Increment by speed
   y += speed;
 }
 void hold() {
   // Increment by speed
   y -= speed;
 }
 // Check if it hits the bottom
 boolean reachedBottom() {
   // If we go a little beyond the bottom
   if (y > height + r*4) {
     return true;
   }
   else {
     return false;
   }
 }

 // Display the raindrop
 void setLocation(float tempX, float tempY) {
   x = tempX;
   y = tempY;
 }

 void display() {
   textSize(f);
   fill(c);
   noStroke();
   text(typeD,x, y);

 }


 // If the drop is caught
 void caught() {
   // Stop it from moving by setting speed equal to zero
   speed = 0;
   // Set the location to somewhere way off-screen
   y = - 1000;

 }

}

timer tab
class Timer {

 int savedTime; // When Timer started
 int totalTime; // How long Timer should last

 int timeLeft;
 Timer(int tempTotalTime) {
   totalTime = tempTotalTime;
 }

 // Starting the timer
 void start() {
   // When the timer starts it stores the current time in milliseconds.
   savedTime = millis();
 }

 // The function isFinished() returns true if 5,000 ms have passed.
 // The work of the timer is farmed out to this method.
 boolean isFinished() {
   // Check how much time has passed
   int passedTime = millis()- savedTime;
   if (passedTime > totalTime) {
     return true;
   }
   else {
     return false;
   }
 }
}

color tracking tab
void checkCol(){

 for(int i = 0 ; i < width*height ; i++){
   color fgColor = video.pixels[i];
   color bgColor = backgroundImage.pixels[i];

   float r1 = red(fgColor);
   float g1 = green(fgColor);
   float b1 = blue(fgColor);
   int I1 = int((0.3*r1+0.59*g1+0.11*b1));

   float r2 = red(bgColor);
   float g2 = green(bgColor);
   float b2 = blue(bgColor);

   int I2 = int((0.3*r2+0.59*g2+0.11*b2));
   float diff = dist(r1,g1,b1,r2,g2,b2);
   int greydiff = abs(I1-I2);

   if (diff >= 50) {
     pixels[i] = color(0,0,0);
something here?
   }
   if (diff < 50) {
     // pixels[loc] = fgColor;
     pixels[i] = color(255,255,255);
something here?
   }

 }

}





Re: Need Help on interaction part!
Reply #1 - May 2nd, 2010, 5:29am
 
ie. if the background different>=50 , the texts stick on it. but if background different <50, the texts continue the falling.

I cannot make it in a simple code but cannot deal with the class item.
anyone can help?? thanks!!!
Page Index Toggle Pages: 1