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 asap...project due tomorrow :(
Page Index Toggle Pages: 1
need help asap...project due tomorrow :( (Read 596 times)
need help asap...project due tomorrow :(
Nov 4th, 2009, 5:17pm
 
okay so i am almost done with this project for school but for some reason every time i release the mouse it is re-initializing the orbs in my program. the easiest way to understand this is to copy the code below and try it. i can't figure out why it is re-initializing any help would be amazing. thanks Smiley

edit: use the code from this post and the 2 replys or it wont run Smiley

Code:
int collisionDist = 50; //distance that orbs collide at
int listener; // tells which orb is selected

int lvl = 1;

Orb[] orbs;
int orbcount;
Point[] p;
float[] orbsx;
float[] orbsy;

int orbsize = 50; //width and height of each orb

boolean once = true; //bool variable to run code once during a given situation
boolean once2 = true;

int[] xtrail = new int[orbsize]; //array for x values of the trail effect
int[] ytrail = new int[orbsize]; //array for y values of the trail effect

PFont font;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////           SETUP             ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void setup(){
 size(960,600);
 smooth();
 //frameRate(1000);

 //here i'm initializing the x and y trail array
 for (int i = 0; i < xtrail.length; i ++ ) {
   xtrail[i] = 0;
   ytrail[i] = 0;
 }



 font = loadFont("Helvetica-40.vlw");

} // end setup

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////            DRAW             ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void draw(){
 background(0);
 
 runGame(lvl);

} // end draw

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////        CUSTOM FUNCTIONS     ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void initGame(){
   lvl = 0;
   textFont(font);
   textSize(36);
   textMode(SCREEN);
   text("Welcome to UnTangle! The point is to move the orbs around the screen", 10, height/2);
   text("so that none of the lines intersect. Press any key to start the game, and enjoy!", 10, height/2+50);
   
   while(keyPressed != true){
     lvl = 1;
   }
   runGame(lvl);
 
}

void runGame(int lvl2){
 if(once == true){
   switch(lvl2){
     case 1:
       orbcount = 5; //number of orbs on screen
       break;
     case 2:
       orbcount = 15;
       break;
     case 3:
       orbcount = 20;
       break;
     case 4:
       orbcount = 25;
       break;
     case 5:
       orbcount = 30;
       break;
     case 6:
       orbcount = 35;
       break;
     case 7:
       orbcount = 40;
       break;
     case 8:
       orbcount = 45;
       break;
     case 9:
       orbcount = 50;
       break;
     case 10:
       orbcount = 55;
       break;
     case 11:
       orbcount = 100;
       break;
   }

   p = new Point[orbcount];
   int intersectcount = 0;

   orbs = new Orb[orbcount]; //orbs array
   orbsx = new float[orbcount]; //array for initial x locations for each orb
   orbsy = new float[orbcount]; //array for initial y locations for each orb

   //here i'm assigning orbsx and orbsy values for the initial start values for each orb
   for(int i = 0; i < orbs.length; i++){
     orbsx[i] = round(random(orbsize/2,width-orbsize/2));
     orbsy[i] = round(random(orbsize/2,height-orbsize/2));
     orbs[i] = new Orb(orbsx[i],orbsy[i],orbsize);
   }
   for(int i = 0; i < orbs.length; i++){
     p[i] = new Point(orbs[i].orbx, orbs[i].orby);
   }
   once = false;
 }

 if(testCompletion() == false){
   drawLines();

   //displaying the orbs
   for(int i = 0; i < orbs.length; i++){
     orbs[i].display();
   }

   //initializing p array with orb x and y values
   for(int i = 0; i < orbs.length; i++){
     p[i] = new Point(orbs[i].orbx, orbs[i].orby);
     //println(p[i].x + ", " + p[i].y);
   }

   //spreading out the orbs so they don't overlap
   for(int i=0; i<orbs.length; i++){
     fixOverlap(i);
   }

   for(int i = 0; i < orbs.length; i++){

     if(dist(mouseX, mouseY, orbs[i].orbx, orbs[i].orby) < 25 && mousePressed){
       findListener();
       orbs[i].orbx = mouseX;
       orbs[i].orby = mouseY;
       detectCollision(i);
       for(int x=0; x<orbs.length; x++){
         detectCollisionByOthers(x);
       }
       stroke(255);
       fill(0);

       for (int x = 45; x < xtrail.length-1; x ++ ) {
         xtrail[x] = xtrail[x+1];
         ytrail[x] = ytrail[x+1];
       }
       xtrail[xtrail.length-1] = mouseX;
       ytrail[ytrail.length-1] = mouseY;

       for (int t = 45 ; t < xtrail.length; t ++ ) {
         ellipse(xtrail[t],ytrail[t],t,t);
       }
       orbs[i].activated = true;
       orbs[i].listening = true;
     }
     else if(!mousePressed){
       orbs[i].activated = false;
     }
   }

   int temp = 0;
   for(int j=0; j< orbcount; j++) {
     int temp1 = j+1;
     if(temp1 == orbcount){
       temp1 = 0;
     }
     line(p[j].x,p[j].y, p[temp1].x,p[temp1].y);
     for(int i=0; i< orbcount; i++) if(i!=j) {
       int temp2 = i+1;
       if(temp2 == orbcount){
         temp2 = 0;
       }
       Point pt=findIntersection(p[i],p[temp2], p[j],p[temp1]);
       if(pt != null && (pt != p[i] || pt != p[j] || pt != p[temp1] || pt != p[temp2])){
         temp++;
         println(temp);
       }
     }
   }

 }
 
Re: need help asap...project due tomorrow :(
Reply #1 - Nov 4th, 2009, 5:18pm
 
Code:
else if(testCompletion() == true){
drawLines();

//displaying the orbs
for(int i = 0; i < orbs.length; i++){
orbs[i].display();
}
rectMode(CENTER);
strokeWeight(5);
stroke(0);
fill(50);
rect(width/2, height/2, 360,200);

fill(255);
textFont(font);
textSize(40);
textMode(SCREEN);
text("You Win!", width/2-75, height/2);
delay(2000);
lvl++;
}
}


//fixOverlap is a function to spread out the orbs so they dont overap eachother
void fixOverlap(int temp){
for(int i=0; i<orbs.length; i++){
if (i != temp){
float tdist = dist(orbs[temp].orbx, orbs[temp].orby, orbs[i].orbx, orbs[i].orby);
if(tdist < collisionDist){
float originx = orbs[temp].orbx - orbs[i].orbx;
float originy = orbs[temp].orby - orbs[i].orby;
float theta = atan2(originy, originx);
float newx = collisionDist * -cos(theta) + orbs[temp].orbx;
float newy = collisionDist * -sin(theta) + orbs[temp].orby;
orbs[i].orbx = newx;
orbs[i].orby = newy;
//orbs[i].activated = true;
}
}
}
}

//this function detects collisions between the selected orb and any other orb
void detectCollision(int temp){
for(int i=0; i<orbs.length; i++){
if (i != listener){
float tdist = dist(orbs[listener].orbx, orbs[listener].orby, orbs[i].orbx, orbs[i].orby);
if(tdist < collisionDist-1){
float originx = orbs[listener].orbx - orbs[i].orbx;
float originy = orbs[listener].orby - orbs[i].orby;
float theta = atan2(originy, originx);
float newx = collisionDist * -cos(theta) + orbs[listener].orbx;
//newx = constrain(newx, 0+orbsize/2, width-orbsize/2);
float newy = collisionDist * -sin(theta) + orbs[listener].orby;
//newy = constrain(newy, 0+orbsize/2, height-orbsize/2);
orbs[i].orbx = newx;
orbs[i].orby = newy;
orbs[i].activated = true;
}
}
}
}

//this function detects collisions between any of the orbs other than the selected orb
void detectCollisionByOthers(int temp){
for(int i=0; i<orbs.length; i++){
if (i != temp){
float tdist = dist(orbs[temp].orbx, orbs[temp].orby, orbs[i].orbx, orbs[i].orby);
if(tdist < collisionDist-1){
float originx = orbs[temp].orbx - orbs[i].orbx;
float originy = orbs[temp].orby - orbs[i].orby;
float theta = atan2(originy, originx);
float newx = collisionDist * -cos(theta) + orbs[temp].orbx;
//newx = constrain(newx, 0+orbsize/2, width-orbsize/2);
float newy = collisionDist * -sin(theta) + orbs[temp].orby;
//newy = constrain(newy, 0+orbsize/2, height-orbsize/2);
orbs[i].orbx = newx;
orbs[i].orby = newy;
orbs[i].activated = true;
}
}
}
}

//reloads once variable whenthe mouse is released
void mouseReleased(){
once = true;
}

// finds which orb is selected
void findListener(){
for(int i=0; i<orbs.length; i++){
if(orbs[i].listening){
listener = i;
}
}
}

void drawLines(){
strokeWeight(10);
stroke(100,0,200);
for(int i = 0; i < orbs.length-1; i++){
line(orbs[i].orbx, orbs[i].orby,orbs[i+1].orbx, orbs[i+1].orby);
}
line(orbs[0].orbx, orbs[0].orby,orbs[orbs.length-1].orbx, orbs[orbs.length-1].orby);

}

boolean testCompletion(){
int temp = 0;
boolean temp3 = false;
for(int j=0; j< orbcount; j++) {
int temp1 = j+1;
if(temp1 == orbcount){
temp1 = 0;
}
line(p[j].x,p[j].y, p[temp1].x,p[temp1].y);
for(int i=0; i< orbcount; i++) if(i!=j) {
int temp2 = i+1;
if(temp2 == orbcount){
temp2 = 0;
}
Point pt=findIntersection(p[i],p[temp2], p[j],p[temp1]);
if(pt != null && (pt != p[i] || pt != p[j] || pt != p[temp1] || pt != p[temp2])){
temp++;
println(temp);
}
}
}
if(temp == orbcount*2){
temp3 = true;
}
return temp3;
}
//this function borrowed from this example:
// http://processing.org/discourse/yabb2/YaBB.pl?num=1252607024/1

Point findIntersection(Point p1,Point p2,
Point p3,Point p4) {
float xD1,yD1,xD2,yD2,xD3,yD3;
float dot,deg,len1,len2;
float segmentLen1,segmentLen2;
float ua,ub,div;

// calculate differences
xD1=p2.x-p1.x;
xD2=p4.x-p3.x;
yD1=p2.y-p1.y;
yD2=p4.y-p3.y;
xD3=p1.x-p3.x;
yD3=p1.y-p3.y;

// calculate the lengths of the two lines
len1=sqrt(xD1*xD1+yD1*yD1);
len2=sqrt(xD2*xD2+yD2*yD2);

// calculate angle between the two lines.
dot=(xD1*xD2+yD1*yD2); // dot product
deg=dot/(len1*len2);

// if abs(angle)==1 then the lines are parallell,
// so no intersection is possible
if(abs(deg)==1) return null;

// find intersection Pt between two lines
Point pt=new Point(0,0);
div=yD2*xD1-xD2*yD1;
ua=(xD2*yD3-yD2*xD3)/div;
ub=(xD1*yD3-yD1*xD3)/div;
pt.x=p1.x+ua*xD1;
pt.y=p1.y+ua*yD1;

// calculate the combined length of the two segments
// between Pt-p1 and Pt-p2
xD1=pt.x-p1.x;
xD2=pt.x-p2.x;
yD1=pt.y-p1.y;
yD2=pt.y-p2.y;
segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);

// calculate the combined length of the two segments
// between Pt-p3 and Pt-p4
xD1=pt.x-p3.x;
xD2=pt.x-p4.x;
yD1=pt.y-p3.y;
yD2=pt.y-p4.y;
segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);

// if the lengths of both sets of segments are the same as
// the lenghts of the two lines the point is actually
// on the line segment.

// if the point isnÔøΩt on the line, return null
if(abs(len1-segmentLen1)>0.01 || abs(len2-segmentLen2)>0.01)
return null;

// return the valid intersection
return pt;
}
class Point{
float x,y;
Point(float x, float y){
this.x = x;
this.y = y;
}

void set(float x, float y){
this.x = x;
this.y = y;
}
}
Re: need help asap...project due tomorrow :(
Reply #2 - Nov 4th, 2009, 5:19pm
 
Code:
class Orb{
boolean listening;
float orbx;
float orby;
float orbdia;
boolean activated;
float[] xvals = new float[360];
float[] yvals = new float[360];

Orb(float x, float y, float dia){
orbx = x;
orby = y;
orbdia = dia;
activated = false;

}


void display(){
if(activated == false){
strokeWeight(1);
stroke(100);
}
else if(activated == true){
strokeWeight(2);
stroke(255);
}

fill(0);
ellipseMode(CENTER);
ellipse(orbx, orby, 50,50);

}
}
Re: need help asap...project due tomorrow :(
Reply #3 - Nov 4th, 2009, 5:53pm
 
hmm everytime the mouse is released it is re-initializing...
good point to start is mouseReleased then...

just remove what is in there (once=true) , and it seems to work.

Page Index Toggle Pages: 1