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 & HelpSyntax Questions › two arrays of objects - HELP!!
Page Index Toggle Pages: 1
two arrays of objects - HELP!! (Read 206 times)
two arrays of objects - HELP!!
Jun 10th, 2009, 2:10pm
 
Hi! I'm starting with processing, and I've been trying to make a this game with processing...
But I just can't get past this problem.
When one of the objects of the class 'Bomb' generated by the array list bomb[] hits the other object of the class 'Target' generated by the array list target[] It is purposed to appen "something", but I just can't define that colison in sintax...
Can anybody help me!?!!?  :-/      


////////////////////////////////////
int alturadocursor= 50;            
int numBomb = 0;                    
Bomb []bomb = new Bomb[numBomb];
int numTarget =2;
Target []target = new Target[numTarget];


void setup () {
 size (350,300);  
 for(int i = 0; i<numTarget; i++) {
 target[i] = new Target(random(60, 140),random (180,220),5);
   
}
}

void draw(){  
 noCursor();
 smooth();
 background (255);
 mouse();
 for(int i = 0; i<bomb.length; i++) {
 bomb[i].display();
 bomb[i].movimento();  
 
}
 for(int i = 0; i<numTarget;i++) {
 target[i].displaymosca1();
 target[i].movemosca();
 }
}

void mousePressed(){
 Bomb b = new Bomb(mouseX,alturadocursor,1.2);
 bomb = (Bomb[]) append (bomb,b);  
 }
 
 void manage() {
   for(int i = 0; i<numTarget; i++) {
   target[i] = new Target(random(60, 140),random (180,220),5);
   }
   
   
}
   
void mouse(){
 strokeCap(ROUND);
 line(mouseX,46,mouseX+width/5,46);
 line(mouseX+width/5,46,mouseX+width/5,40);
 line(mouseX-width/5,46,mouseX-width/5,40);
 line(mouseX,46,mouseX-width/5,46);
 fill(#6C1510);
 beginShape();
 vertex(mouseX-5,46);
 vertex(mouseX-2,43);
 vertex(mouseX+2,43);
 vertex(mouseX+5,46);
 vertex(mouseX+5,52);
 vertex(mouseX-5,52);
 endShape(CLOSE);
 }

///////////////////////////////CLASS BOMB

class Bomb {
 float x,y;//pos x e y da bomba
 float g; //gravidade
 float c1,c2; //cor
 float t1,t2;// tamanho da bomba quer quando é menor ou igual a height;
 float bd = 50;
 
 Bomb(float xpos, float ypos, float gravidade) {
   x=xpos;
   y=ypos;
   g=gravidade;  
 }

void display() {
   
   t1=1;
   t2=random(0,bd);
   c1=0;
   c2=random(255);
   if(y<height){        
     fill(c1);
     ellipse(x,y,t1,t1);
   }
   else {              
     fill(c2);
     ellipse(x,y,t2,t2);
     bd=bd-0.5;
   }
 }  
void movimento() {
   y=y+g;
   if (y>=height) {
     y=height;
   }
 }
}

////////////////////CLASS TARGET

class Target {
 float a;    
 float b;    
 float tam;  
 float xoscilation = 0.1;
 float xoincrement =0.01;
 float a1;
 float a2;
 float w;
 float updw=0.5;
 float v;

 
 Target(float px, float py, float t){
   a=px;
   b=py;
   tam=t;  

 }
 void displaymosca1(){
   fill(0);
   ellipse(a,b,tam,tam);
   frameRate(600);
   w=random(b-5,b+5);
   line(a,b,a-7,w);
   line(a,b,a+7,w);  

 }

 void movemosca(){
   a = a + random(-3, 3);
   
   b = b + random(-1, 1);
 
 if (a<=0){
   a=0;
 }
 if (a>=width){
 a = width;  
 if (b<=60){
 b=60;
 }
 if (b>=height){
 b=height;
 }  
}
}
}
Re: two arrays of objects - HELP!!
Reply #1 - Jun 10th, 2009, 3:30pm
 
Looks like you posted your question 4 times  Shocked

Anyway one crude method is as follows:

You should first decide which of your objects is going to do the checking - the bomb or the target.  Then in the bomb or target's movement routine you go through and compare the position of the bomb with the position of each target.  Probably the simplest method of collision detection is to check whether the bomb is within a certain radius of the target:

float dx = bombX - targetX;
float dy = bombY - targetY;
// pythagoras Smiley
float distance = sqrt(dx*dx + dy*dy)
if (distance < radius) {
 // hit
}

The only problem you may run into with this approach is if you're dealing with a lot of targets and bombs it could prove a little processor intensive; in which case there are ways of optimising the process... Needless to say collision detection is a well documented topic - you should find plenty of resources out there; if not for Processing then for other languages (e.g. Actionscript) that could easily be converted into Processing Wink
Re: two arrays of objects - HELP!!
Reply #2 - Jun 11th, 2009, 2:09am
 
A quick optimisation would be to
Code:

float dx = bombX - targetX;
float dy = bombY - targetY;
// pythagoras
float distance = (dx*dx + dy*dy)
if (distance < radius * radius) {
// hit
}

replacing a costly square root with a less costly multiplication.
Smiley
Page Index Toggle Pages: 1