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 › Collision Detection Error
Page Index Toggle Pages: 1
Collision Detection Error (Read 514 times)
Collision Detection Error
Apr 21st, 2009, 5:16pm
 
Ummm... I have a very funny and strange glitch, could someone help?

Basically, my program has a bunch of A.I. fish move around and eat each other. However, instead of eating on contact, they eat constantly, causing some fish to teleport all around and the other to grow constantly. I think the problem is in the eating code, here:
Code:
//check for fish within an area, and chase/run away
if(ident!=1) {
if((x+sizex<=fish1.sizex+fish1.x+50 || x>=fish1.x-50) && (y+sizey<=fish1.sizey+fish1.y ||y>=fish1.y)) {
if(sizex*sizey>fish1.sizex*fish1.sizey+30) {
if(isChasing=false) {
chase=random(.5,1.5);
xmov=chase*fish1.xmov;
ymov=chase*fish1.xmov;
isChasing=true;
}
stopChasing=true;
}
}
if(sizex*sizey<fish1.sizex*fish1.sizey-30) {
if(x>fish1.x && xmov<0 || x<fish1.x && xmov>0) {
xmov=-1.2*xmov;
ymov=-1.2*ymov;
} else {
xmov=xmov*1.2;
ymov=ymov*1.2;
}
}
}
if(ident!=2) {
if((x+sizex<=fish2.sizex+fish2.x+50 || x>=fish2.x-50) && (y+sizey<=fish2.sizey+fish2.y ||y>=fish2.y)) {
if(sizex*sizey>fish2.sizex*fish2.sizey+30) {
if(isChasing=false) {
chase=random(.5,1.5);
xmov=chase*fish2.xmov;
ymov=chase*fish2.xmov;
isChasing=true;
}
stopChasing=true;
}
if(sizex*sizey<fish2.sizex*fish2.sizey-30) {
if(x>fish2.x && xmov<0 || x<fish2.x && xmov>0) {
xmov=-1.2*xmov;
ymov=-1.2*ymov;
} else {
xmov=xmov*1.2;
ymov=ymov*1.2;
}
}
}
}
if(ident!=3) {
if((x+sizex<=fish3.sizex+fish3.x+50 || x>=fish3.x-50) && (y+sizey<=fish3.sizey+fish3.y ||y>=fish3.y)) {
if(sizex*sizey>fish3.sizex*fish3.sizey+30) {
if(isChasing=false) {
chase=random(.5,1.5);
xmov=chase*fish3.xmov;
ymov=chase*fish3.xmov;
isChasing=true;
}
stopChasing=true;
}
if(sizex*sizey<fish3.sizex*fish3.sizey-30) {
if(x>fish3.x && xmov<0 || x<fish3.x && xmov>0) {
xmov=-1.2*xmov;
ymov=-1.2*ymov;
} else {
xmov=xmov*1.2;
ymov=ymov*1.2;
}
}
}
}
if(ident!=4) {
if((x+sizex<=biggie.sizex+biggie.x+50 || x>=biggie.x-50) && (y+sizey<=biggie.sizey+biggie.y ||y>=biggie.y)) {
if(sizex*sizey>biggie.sizex*biggie.sizey+30) {
if(isChasing=false) {
chase=random(.5,1.5);
xmov=chase*biggie.xmov;
ymov=chase*biggie.xmov;
isChasing=true;
}
stopChasing=true;
}
if(sizex*sizey<fish1.sizex*fish1.sizey-30) {
if(x>biggie.x && xmov<0 || x<biggie.x && xmov>0) {
xmov=-1*xmov;
ymov=-1*ymov;
} else {
xmov=xmov;
ymov=ymov;
}
}
}
}
//stop fish after they're done chasing
if(stopChasing=false) {
isChasing=false;
}
stopChasing=false;
//check for collisions between the fish and eat the smaller one
if(ident!=1) {
if((x+sizex<=fish1.sizex+fish1.x || x>=fish1.x) && (y+sizey<=fish1.sizey+fish1.y ||y>=fish1.y)) {
if(sizex*sizey>fish1.sizex*fish1.sizey+30) {
fish1.v=true;
fish1.x=random(1000);
fish1.y=random(400);
fish1.sizex=20;
fish1.sizey=20;
expand(bites,bites.length+1);
bites[chompCount-1]=new Chomp(int(x),int(y));
sizex=sizex+5;
sizey=sizey+5;
}
}
}
if(ident!=2) {
if((x+sizex<=fish2.sizex+fish2.x || x>=fish2.x) && (y+sizey<=fish2.sizey+fish2.y ||y>=fish2.y)) {
if(sizex*sizey>fish2.sizex*fish2.sizey+30) {
fish2.v=true;
fish2.x=random(1000);
fish2.y=random(400);
fish2.sizex=20;
fish2.sizey=20;
expand(bites,bites.length+1);
bites[chompCount-1]=new Chomp(int(x),int(y));
sizex=sizex+5;
sizey=sizey+5;
}
}
}
if(ident!=3) {
if((x+sizex<=fish3.sizex+fish3.x || x>=fish3.x) && (y+sizey<=fish3.sizey+fish3.y ||y>=fish3.y)) {
if(sizex*sizey>fish3.sizex*fish3.sizey+30) {
fish3.v=true;
fish3.x=random(1000);
fish3.y=random(400);
fish3.sizex=20;
fish3.sizey=20;
expand(bites,bites.length+1);
bites[chompCount-1]=new Chomp(int(x),int(y));
sizex=sizex+5;
sizey=sizey+5;
}
}
}
if(ident!=4) {
if((x+sizex<=biggie.sizex+biggie.x || x>=biggie.x) && (y+sizey<=biggie.sizey+biggie.y ||y>=biggie.y)) {
if(sizex*sizey>biggie.sizex*biggie.sizey+30) {
biggie.v=true;
biggie.x=random(1000);
biggie.y=random(400);
biggie.sizex=20;
biggie.sizey=20;
expand(bites,bites.length+1);
bites[chompCount-1]=new Chomp(int(x),int(y));
sizex=sizex+5;
sizey=sizey+5;
}
}
}
Page Index Toggle Pages: 1