# Object stop upon collision?

**URL:** <https://discourse.processing.org/t/object-stop-upon-collision/6498>\
**Category:** Coding Questions\
**Created:** [December 11, 2018, 2:21am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498 "2018-12-11T02:21:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinM](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MartinM](https://discourse.processing.org/u/MartinM)\
**Post date:** [December 11, 2018, 2:21am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/1 "2018-12-11T02:21:00Z")

</div>

I am currently having a problem with collision, I do not want an object to pass through another of which I understand the basics of. For instance:

```auto
nt[] player1;
int[] player2;
int p1width = 50;
int p1height = 50;
int p2width = 50;
int p2height = 50;
int sandbagwidth = 30;
int sandbagheight = 200;
int sand11 =200;
int sand12 = 0;
void setup(){
  size(1200, 600);
  player1= new int[] {250, 250};
  player2= new int[] {930, 250};
  
}

void draw(){
  //draw players
  fill(0);
  rect(0,0,width/2,height);
  fill(255,0,0);
  rect(600,0,width/2,height);
  //move players
  fill(255);
  rect(player1[0], player1[1], p1width, p1height);
  if (keyPressed) {
    switch(keyCode) {
    case LEFT:
      player1[0] -=10;
      break;
    case RIGHT:
      player1[0] +=10;
      break;
    case UP:
      player1[1] -=10;
      break;
    case DOWN:
      player1[1] +=10;
      break;
    }
  }
  
  
    
   if(player1[0]+25>1150){
     player1[0]-=25;
   }
   if(player1[0]-25<0){
     player1[0]+=50;
   }
   
  
   if(player1[1]+25>550){
     player1[1]-=25;
   }
   if(player1[1]-25<0){
     player1[1]+=50;
   }
   
   
   fill(0,255,0);
   rect(sand11, sand12, sandbagwidth, sandbagheight);
   
   if(player1[0] + p1width/2 > sand11 - sandbagwidth/2
   && player1[0] - p1width/2 < sand11 + sandbagwidth/2
   && player1[1] + p1width/2 > sand12 - sandbagheight
   && player1[1] - p1width/2 < sand12 + sandbagheight){
     player1[0]-=1;
   }
  }

```

This would be good however I don’t want it reverse the direction of the object by one on all sides, that doesn’t stop an object from getting through. So to try to make it so if it hits the right wall for instance it adds 1 direction, I tried splitting it up. But splitting up the collision code makes it move on its own:

```auto
if(player1[0] + p1width/2 > sand11 - sandbagwidth/2
   
   && player1[1] + p1width/2 > sand12 - sandbagheight
   ){
     player1[0]-=1;
   }

```

So instead of doing reverse direction is there any way I can stop moving object instead? Or could someone lend me a hand on better understanding how to get collision working for all sides

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [December 11, 2018, 7:21am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/2 "2018-12-11T07:21:33Z")

</div>

In your current example, what is `player1[0]-=1;`? Maybe you meant to implement direction inversion, which requires multiplication instead of subtraction: `player1[0] *= 1;`

Would this example give you an idea of another approach: [https://processing.org/examples/bouncybubbles.html](https://processing.org/examples/bouncybubbles.html) This link calculates change of direction using core Physics concepts. You are welcome to explore this an any other examples to better understand previous implementation. If something is not clear, you can try asking here in this post.

Kf

---

<div class="post-metadata">

**Author:** ![MartinM](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MartinM](https://discourse.processing.org/u/MartinM)\
**Post date:** [December 11, 2018, 1:16pm UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/3 "2018-12-11T13:16:02Z")

</div>

player1[0]=-1 is the movement of one space backwards after collision. But -1 is only to the left in direction which is ok if I collide into the left of the block. But if I collide into the right it should go +1 so it bounces to the right, which is effectively not what my code is doing which is why I tried splitting up the collision between the left, right, and bottom walls. Doing player1[0] x= 1 effectively does nothing.

Basically I want to have if collides with left -1 in x direction, if it is right +1 in x direction, if it hits bottom -1 in y direction, etc.

I used this same collision code to pick up objects which I am having no trouble with but picking things up is different then collision, for each side the item is grabbed the result is the same. For each side of collision the result should not be the same. I have other collisions working too including wall collisions but I cannot for the life of me figure out how to get these two objects to collide properly.

---

<div class="post-metadata">

**Author:** ![MartinM](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MartinM](https://discourse.processing.org/u/MartinM)\
**Post date:** [December 12, 2018, 2:14am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/4 "2018-12-12T02:14:14Z")

</div>

[took a different approach but still not working, posted similar question here](https://stackoverflow.com/questions/53735054/collision-for-intended-target-is-instead-colliding-along-axis-only)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 12, 2018, 2:46am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/5 "2018-12-12T02:46:24Z")

</div>

i think what the idea was to suggest following structure:  
you have a  
final float speed =10.0;  
float dir = 1 or -1 or 0 like go left, right, stop  
so the player movement will be like  
player.x += speed\*dir;

later you will find that bouncing in an angle requires more, like  
speedx and speedy…

may i suggest you search little bit more here for examples ( ok, take the easy ones )

---

<div class="post-metadata">

**Author:** ![MartinM](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MartinM](https://discourse.processing.org/u/MartinM)\
**Post date:** [December 12, 2018, 3:42am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/6 "2018-12-12T03:42:40Z")

</div>

I managed to fix the collision direction in a different way, however as soon as I added a second object on the same axis it is registering the whole axis as a collision. That and holding down the key instead of pressing will glitch you through the object eventually:

```auto
if (keyCode==LEFT&&player1[0] + p1width/2 > sand11 - sandbagwidth/2
    && player1[0] - p1width/2 < sand11 + sandbagwidth/2
    && player1[1] + p1width/2 > sand12 - sandbagheight-25
    && player1[1] - p1width/2 < sand12 + sandbagheight-25) {
    direction[0]=false;
    direction[1]=false;
    direction[2]=false;
    direction[3]=false;
    player1[0] +=1;
   
  }
  else if (keyCode==RIGHT&&player1[0] + p1width/2 > sand11 - sandbagwidth/2
    && player1[0] - p1width/2 < sand11 + sandbagwidth/2
    && player1[1] + p1width/2 > sand12 - sandbagheight-25
    && player1[1] - p1width/2 < sand12 + sandbagheight-25) {
    direction[0]=false;
    direction[1]=false;
    direction[2]=false;
    direction[3]=false;
    player1[0] -=1;
   
  }
  else if (keyCode==UP&&player1[0] + p1width/2 > sand11 - sandbagwidth/2
    && player1[0] - p1width/2 < sand11 + sandbagwidth/2
    && player1[1] + p1width/2 > sand12 - sandbagheight-25
    && player1[1] - p1width/2 < sand12 + sandbagheight-25) {
    direction[0]=false;
    direction[1]=false;
    direction[2]=false;
    direction[3]=false;
    player1[1] +=1;
   
  }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 12, 2018, 3:48am UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/7 "2018-12-12T03:48:45Z")

</div>

( sorry )

above is a keyboard code combining both player geometry  
with a UP DOWN keys  
i think it should be separated for each player

---

<div class="post-metadata">

**Author:** ![MartinM](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MartinM](https://discourse.processing.org/u/MartinM)\
**Post date:** [December 12, 2018, 1:51pm UTC](https://discourse.processing.org/t/object-stop-upon-collision/6498/8 "2018-12-12T13:51:02Z")

</div>

[Full working solution here](https://stackoverflow.com/questions/53735054/collision-for-intended-target-is-instead-colliding-along-axis-only) Similar case can be used in example here on this site, TOPIC CLOSED, unless someone must tell me something else
