# Collisions on moving objects

**URL:** <https://discourse.processing.org/t/collisions-on-moving-objects/24912>\
**Category:** Coding Questions\
**Created:** [October 26, 2020, 12:54pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912 "2020-10-26T12:54:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 12:54pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/1 "2020-10-26T12:54:42Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 1:19pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/2 "2020-10-26T13:19:23Z")

</div>

Use dist() command- see reference

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 1:35pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/3 "2020-10-26T13:35:14Z")

</div>

Thank you! It was actually that simple thanks!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 1:50pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/4 "2020-10-26T13:50:40Z")

</div>

Yeah, well.

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 1:50pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/5 "2020-10-26T13:50:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 1:51pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/6 "2020-10-26T13:51:31Z")

</div>

Could you maybe give an example op an array?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 1:52pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/7 "2020-10-26T13:52:20Z")

</div>

> [@MaxMax](#):
>
> can only use the X axis as an dist()

That’s for example

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 1:54pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/8 "2020-10-26T13:54:38Z")

</div>

> [@MaxMax](#):
>
> Could you maybe give an example op an array?

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 2:01pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/9 "2020-10-26T14:01:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 2:27pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/10 "2020-10-26T14:27:15Z")

</div>

> [@MaxMax](#):
>
> only has to stop when the objects hit eacother

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

Or obstacle against other obstacle?

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 2:38pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/11 "2020-10-26T14:38:47Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 2:45pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/12 "2020-10-26T14:45:55Z")

</div>

> [@MaxMax](#):
>
> the game should stop

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 3:05pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/13 "2020-10-26T15:05:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 3:08pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/14 "2020-10-26T15:08:56Z")

</div>

> [@MaxMax](#):
>
> carXPos, y,

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

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 26, 2020, 3:11pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/15 "2020-10-26T15:11:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2020, 4:31pm UTC](https://discourse.processing.org/t/collisions-on-moving-objects/24912/16 "2020-10-26T16:31:09Z")

</div>

That should work!  
Chrisir
