# Detecting object collision

Hello! I’m new to processing, and i’m wondering how can i compare two variables from different classes with each other. Can someone please guide me in the right direction…

if you are wondering here is my code:

float ang, speed, x, y;
int b_count;
int z_count = 10;
boolean up,down,left,right, shooting = false;

bullet[] b = new bullet[90000];
zombie[] z = new zombie[90000];

void setup() {
size(1200, 800);
smooth();
ang = 0;
speed = 3;
x = width/2;
y = height/2;

for(int i = 0; i < b.length;i++){
b[i] = new bullet();

}

for(int i = 0; i < z.length;i++){
z[i] = new zombie();

}

}

void draw() {
rectMode(CORNER);
background(0);
pushMatrix();
translate(x, y);
rotate(ang);
rect(-20, -7.5, 40, 15); // Center the rect
translate(20, 0); // Translate to default Polar Coordinate 0 radians
ellipse(0, 0, 40, 40); // Display direction
popMatrix();

for (int i = 0; i < b_count; i++){
b[i].show(x,y,ang);
b[i].move();
}

for (int i = 0; i < z_count; i++){
z[i].show();

}

//keys functions!
if(down == true){
x += cos(ang)*speed;
y += sin(ang)*speed;
}

if(up == true){
x -= cos(ang)*speed;
y -= sin(ang)*speed;
}

if(left == true){
ang -= 0.05;
}

if(right == true){
ang += 0.05;
}

}

//keys!!
void keyPressed() {
if(key == 's'){
down = true;
}

if(key == 'w'){
up = true;
}
if(key == 'a'){
left = true;
}
if(key == 'd'){
right = true;
}

///
if(key == ' '){
b_count++;
}

}

void keyReleased() {
if(key == 's'){
down = false;
}

if(key == 'w'){
up = false;
}
if(key == 'a'){
left = false;
}
if(key == 'd'){
right = false;
}

}

class bullet{
float bx,by,bw,bh,bang,bx_,by_;
int bc;
float bspeed = 5;

bullet(){

}
void show(float x_,float y_,float ang_){
if(bc == 0){
bx_ = x_;
by_ = y_;
bx = 0;
by = 0;
bang = ang_/2;
}

bc++;

pushMatrix();
translate(bx_,by_);
rotate(bang);
rectMode(CENTER);;    rect(bx,by,10,10);
popMatrix();

}

void move(){
bx -= cos(bang)*bspeed;
by -= sin(bang)*bspeed;

}

}

class zombie{
float zx = random(0,width);
float zy = random(0,height);
float r = 10;
float zspeed = 1;
boolean hit;

void show(){
ellipse(zx,zy,r*2,r*2);
}
}
1 Like

</> Preformatted text button from the editor menu

and for the 2 times 90 000 long arrays want give you a - -

a very basic approach for collision would be to calculate the distance between 2 objects,
on case of array of class elements pls use like z[i].zx or b[i].by

a minified test with 2 classes

A[] as = new A[5];
B[] bs = new B[10];
int loop = 0;

void setup() {
size(300, 300);
rectMode(CENTER);
for (int i = 0; i < as.length; i++) as[i] = new A(100+ i * 20, 100+ i * 20 );
for (int i = 0; i < bs.length; i++) bs[i] = new B();
noLoop();
println("press any key");
}

void draw() {
background(200, 200, 0);
for (int i = 0; i < as.length; i++) as[i].show();
for (int i = 0; i < bs.length; i++) bs[i].show();
check_close();
loop++;
}

void check_close() {
for (int i = 0; i < as.length; i++) {
for (int j = 0; j < bs.length; j++) {
float dmin = bs[j].diam/2 + as[i].diam/2 ;
if ( abs( bs[j].x - as[i].x ) < dmin &&
abs( bs[j].y - as[i].y ) < dmin  ) {
println("loop "+loop+" as["+i+"] and bs["+j+"] are close ");
println("x ", as[i].x, bs[j].x, " y ", as[i].y, bs[j].y, dmin );
}
}
}
}

class A {
float x, y;                 // from main
float diam = 5;             // local
A(float _x, float _y) {
x = _x;
y = _y;
}
void show() {
fill(0, 200, 0);
rect(x, y, diam, diam);
}
}

class B {
float x, y;                 // from main
float diam = 8;             // local
B() {}
void show() {
x=random(width-diam);
y=random(height-diam);
fill(200, 0, 0);
circle(x, y, diam);
}
}

void keyPressed() {
redraw();
}

for a serious collision detection see
Collision Detection here

2 Likes