# Collisions on moving objects

Hello, I am new to processing and I have to work on a school project. For my project, I need objects that are falling down to collide with my own moving character. I have been searching quite a bit but I can’t find how to work with collisions on moving objects. If someone knows how I can end my game if my obstacles and character touch each other that would be very helpful.
(This is my code so far)

float stripeYPos;
float b;
float obstacleYPos;

int x = 0;
int y = 450;

void setup() {
size (720, 600);
stripeYPos = -40;
obstacleYPos = -80;
b = -70;

//fill (#626161);
//rect (0, 0, 240, 600); // links weg

//fill (#F7F7F7);
//rect (240, 0, 240, 600); // midden weg

//fill (#626161);
//rect (480, 0, 240, 600); // rechts weg
}

void keyPressed() {
if ( key == ‘d’ ) {
x = x + 50;
}
if ( key == ‘a’)
x = x - 50;
}

void draw () {
background (#484646);
smooth();
strokeWeight(2);

line(240, 0, 240, 600);
line(480, 0, 480, 600);

obstacleYPos = obstacleYPos + 8;
if (obstacleYPos > height) {
obstacleYPos = -20;
}

stripeYPos = stripeYPos + 5;
if (stripeYPos > height + 70) {
stripeYPos = -20;
}

fill (#F7F7F7);
rect(100, stripeYPos, 40, 70); // all the moving stripes

rect(340, stripeYPos, 40, 70);

rect(580, stripeYPos, 40, 70);

fill(255);
ellipse(400, obstacleYPos, 72, 72); // Obstacle circle

fill (#F20F0F);
rect(100, obstacleYPos, 80, 80); // obstacle rectangle

fill (#F00F0F);
rect(mouseX+5, y-12, 50, 20); //x,y,w,h body

fill (#F00F0F);
rect(mouseX+5, y+60, 50, 20); //x,y,w,h body

fill (#F00F0F);
rect(mouseX, y-5, 60, 80); //x,y,w,h car

fill (#050505);
rect(mouseX-15, y+2, 15, 25); // top left tire

fill (#050505);
rect(mouseX-15, y+40, 15, 25); // bottom left tire

fill (#050505);
rect(mouseX+60, y+2, 15, 25); // top right tire

fill (#050505);
rect(mouseX+60, y+40, 15, 25); // bottom right tire
}

Use dist() command- see reference

Like if (dist(x,y 100, obstacleypos) < 44) {// hit
}

Thank you! It was actually that simple thanks!

Yeah, well.

Would be easier if you had all obstacles stored in an array

Is there a way where I can only use the X axis as an dist()? Because what happens now is that every time the objects meet on the Y axis the program stops

1 Like

Could you maybe give an example op an array?

1 Like

That’s for example

if(abs(x-obstacleX) > 44) …

When you have only 2 obstacles, not necessary

For example one array shows the x values for all obstacles

x[0] = 400;

then for loop over the array for loop i
rect… x[i]

See reference section array

Okay I now have this:

float stripeYPos;
float b;
float obstacleYPos;
float obstacleXPos;

float carXPos;
float carYPos;

float d;

int x = 0;
int y = 450;

void setup() {
size (720, 600);
stripeYPos = -40;
obstacleYPos = -80;
b = -70;
obstacleXPos = x;

carXPos = 0;

//fill (#626161);
//rect (0, 0, 240, 600); // links weg

//fill (#F7F7F7);
//rect (240, 0, 240, 600); // midden weg

//fill (#626161);
//rect (480, 0, 240, 600); // rechts weg
}

void keyPressed() {
if ( key == ‘d’ ) {
x = x + 50;
}
if ( key == ‘a’)
x = x - 50;
}

void draw () {
background (#484646);
smooth();
strokeWeight(2);

line(240, 0, 240, 600);
line(480, 0, 480, 600);

//if (dist(carXPos, y, obstacleXPos, obstacleYPos) < 3) {
// background (0);
// noLoop();
//}

if(abs(x-obstacleXPos) < 100)

obstacleYPos = obstacleYPos + 8;
if (obstacleYPos > height) {
obstacleYPos = -20;
}

stripeYPos = stripeYPos + 5;
if (stripeYPos > height + 70) {
stripeYPos = -20;
}

fill (#F7F7F7);
rect(100, stripeYPos, 40, 70); // all the moving stripes

rect(340, stripeYPos, 40, 70);

rect(580, stripeYPos, 40, 70);

fill(255);
ellipse(obstacleXPos + 400, obstacleYPos, 72, 72); // Obstacle circle

fill (#F20F0F);
rect(obstacleXPos + 100, obstacleYPos, 80, 80); // obstacle rectangle

fill (#F00F0F);
rect(x+5, y-12, 50, 20); //x,y,w,h body

fill (#F00F0F);
rect(x+5, y+60, 50, 20); //x,y,w,h body

fill (#F00F0F);
rect(x + carXPos, y-5, 60, 80); //x,y,w,h car

fill (#050505);
rect(x-15, y+2, 15, 25); // top left tire

fill (#050505);
rect(x-15, y+40, 15, 25); // bottom left tire

fill (#050505);
rect(x+60, y+2, 15, 25); // top right tire

fill (#050505);
rect(x+60, y+40, 15, 25); // bottom right tire
}

But it still doesn’t do what I want it to do, It only has to stop when the objects hit eacother

When you mean car against obstacle? check car against obstacle.

Or obstacle against other obstacle?

When the car hits an obstacle the game should stop and when you dodge an object your score should go up with 1 but I have no idea how I can fix this…

Okay, you have this noLoop() Statement already.

You could also go to another screen by setting a new variable state to 1 when the car crashes

Then in draw evaluate state: if ==0 the code you have in draw() now otherwise text() you lost

Yeah is see but the noLoop() statement start before the objects hit each other, maybe I am not using the dist statement right?

I dunno what your car is exactly, x,y OR carXpos,y ???

Its the body of the car but the car is mounted on the X axis
rect(x + carXPos, y-5, 60, 80); //x,y,w,h car

But I can remove the carXPos because it doesn’t add anything

1 Like

That should work!
Chrisir