# Random motion with collision detection

**URL:** https://forum.kirupa.com/t/random-motion-with-collision-detection/264402
**Category:** flash
**Created:** [June 25, 2008, 7:49pm UTC](https://forum.kirupa.com/t/random-motion-with-collision-detection/264402 "2008-06-25T19:49:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ruffneck\_chiken](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@ruffneck\_chiken](https://forum.kirupa.com/u/ruffneck_chiken)
#### Post date: [June 25, 2008, 7:49pm UTC](https://forum.kirupa.com/t/random-motion-with-collision-detection/264402/1 "2008-06-25T19:49:08Z")

</div>

HI i am trying to to the above. I have two items on a stage and i only want them to move with in a certine area and bounce off each other when they meet. I found a good random movement code on this site but now im stuck…here is what i have:

onClipEvent (load) {  
//data you may want to change  
width = 425;  
height = 325;  
speed = Math.round(Math.random()\*2)+ 2;  
//initial positions  
x = this.\_x=Math.random()\*width;  
y = this.\_y=Math.random()\*height;  
x\_new = Math.random()\*width;  
y\_new = Math.random()_height;  
}  
onClipEvent (enterFrame) {  
//x movement  
if (x\_new\>this.\_x) {  
sign\_x = 1;  
} else {  
sign\_x = -1;  
}  
dx = Math.abs(x\_new-this.\_x);  
if ((dx\>speed) || (dx\<-speed)) {  
this.\_x += sign\_x_speed;  
} else {  
x\_new = Math.random()_width;  
}  
//y movement  
if (y\_new\>this.\_y) {  
sign\_y = 1;  
} else {  
sign\_y = -1;  
}  
dy = Math.abs(y\_new-this.\_y);  
if ((dy\>speed) || (dy\<-speed)) {  
this.\_y += sign\_y_speed;  
} else {  
y\_new = Math.random()\*height;  
}  
}

Thanks in advance for your help.

---

<div class="post-metadata">

### Author: ![dlgamer](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@dlgamer](https://forum.kirupa.com/u/dlgamer)
#### Post date: [June 25, 2008, 8:42pm UTC](https://forum.kirupa.com/t/random-motion-with-collision-detection/264402/2 "2008-06-25T20:42:19Z")

</div>

attach this code in your movie:

//collision detection  
for (var i = 0; i\<3; i++) {  
if (this == \_root[“ball”+i]) {  
continue;  
}  
if (this.hitTest(\_root[“ball”+i])) {  
if (x\_new\>this.\_x) {  
x\_new = this.\_x-(x\_new-this.\_x);  
} else {  
x\_new = this.\_x+(this.\_x-x\_new);  
}  
if (y\_new\>this.\_y) {  
y\_new = this.\_y-(y\_new-this.\_y);  
} else {  
y\_new = this.\_y+(this.\_y+y\_new);  
}  
}  
}  
//border detection  
if (this.\_x\>width || this.\_x\<0) {  
x\_new = Math.random()\*width;  
}  
if (this.\_y\>height || this.\_y\<0) {  
y\_new = Math.random()\*height;  
}

---

<div class="post-metadata">

### Author: ![ruffneck\_chiken](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@ruffneck\_chiken](https://forum.kirupa.com/u/ruffneck_chiken)
#### Post date: [June 26, 2008, 6:16pm UTC](https://forum.kirupa.com/t/random-motion-with-collision-detection/264402/3 "2008-06-26T18:16:25Z")

</div>

I pasted the code you provided into each of the movie clips and i recieved an error. From the error it seems that the code needed to be in the onclip event. I put it there and i didnt get the error anymore but it still doesnt seem to be working. From your code it appears that the movie clips need to be named ball1 and ball 2 correct? also the fla you included i couldnt open. I am using version MX 8

Idealy i would like to not use the code i provided. I would like to have 4 walls (wall1, wall2, wall3, wall4 and two moving clips (frame1 and fram2) and have the two frames bounce around off the walls and each other.

again thanks in advance for your help.

---

<div class="post-metadata">

### Author: ![dlgamer](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@dlgamer](https://forum.kirupa.com/u/dlgamer)
#### Post date: [June 27, 2008, 1:12am UTC](https://forum.kirupa.com/t/random-motion-with-collision-detection/264402/4 "2008-06-27T01:12:21Z")

</div>

As for your error, that is because the code has to be in your onEnterFrame clip event.

I think one problem with your idea is that you want to generate random motion but have the balls bounce off each other realistically I:-)

If you want a realistic effect why not make the direction they are headed when loaded random but any direction change after that to be based on the interactions between the walls + other balls on screen? I think this might be more of what you are looking for.
