# Collision in App Lab?

**URL:** <https://forum.code.org/t/collision-in-app-lab/31723>\
**Category:** Website and Tools Questions\
**Tags:** applab\
**Created:** [November 15, 2019, 8:27pm UTC](https://forum.code.org/t/collision-in-app-lab/31723 "2019-11-15T20:27:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinlimitededition](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@justinlimitededition](https://forum.code.org/u/justinlimitededition)\
**Post date:** [November 15, 2019, 8:27pm UTC](https://forum.code.org/t/collision-in-app-lab/31723/1 "2019-11-15T20:27:28Z")

</div>

The student I have is making a game where Yoshi collects coins. He wants the coin to move to a random location after Yoshi comes in contact with it. Any ideas on how this could be possible?  
Thanks in advance!

> **[Check out what I made](https://studio.code.org/projects/applab/t_hcxMWMHujyXLA7qPyyMpZ3eGnXctgpLbjG1zmZePY)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![kookooloopdaking](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/kookooloopdaking/32/5224_2.png) [@kookooloopdaking](https://forum.code.org/u/kookooloopdaking)\
**Post date:** [November 15, 2019, 8:45pm UTC](https://forum.code.org/t/collision-in-app-lab/31723/2 "2019-11-15T20:45:33Z")

</div>

Hello,  
if you have an if/then statement checking the collision of yoshi and the coin, you could then move the coin to a random position.  
ex:

function checkCollect() {

if (yoshi.isTouching(coin)) {

score++;  
do {

coin.x = randomNumber(0,400);  
coin.y = randomNumber(0,400);

} while (yohi.isTouching(coin));  
}  
}

---

<div class="post-metadata">

**Author:** ![justinlimitededition](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@justinlimitededition](https://forum.code.org/u/justinlimitededition)\
**Post date:** [November 15, 2019, 9:10pm UTC](https://forum.code.org/t/collision-in-app-lab/31723/3 "2019-11-15T21:10:39Z")

</div>

I thought is.Touching was only possible in Game Lab?

coin.x = randomNumber(0,400);  
coin.y = randomNumber(0,400);

wouldn’t work either because the coin is an image id and needs to be in a string.  
Thanks for the response but I don’t think that will work in this situation.

---

<div class="post-metadata">

**Author:** ![justinlimitededition](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@justinlimitededition](https://forum.code.org/u/justinlimitededition)\
**Post date:** [November 15, 2019, 9:11pm UTC](https://forum.code.org/t/collision-in-app-lab/31723/4 "2019-11-15T21:11:45Z")

</div>

I thought is.Touching was only possible in Game Lab?

coin.x = randomNumber(0,400);  
coin.y = randomNumber(0,400);

wouldn’t work either because the coin is an image id and needs to be in a string.  
Thanks for the response but I don’t think that will work in this situation.

---

<div class="post-metadata">

**Author:** ![infinitestasis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/infinitestasis/32/5047_2.png) [@infinitestasis](https://forum.code.org/u/infinitestasis)\
**Post date:** [November 15, 2019, 10:55pm UTC](https://forum.code.org/t/collision-in-app-lab/31723/5 "2019-11-15T22:55:56Z")

</div>

If collision support is not offered in app lab, I suggest implementing your own collision system. The simplest way is to use axis-aligned bounding box. It’s probably less efficient than using sprites but it works.

> **[2D collision detection](https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection)**
>
> One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not...
