# Collision detection problem!

**URL:** <https://forum.playcanvas.com/t/collision-detection-problem/9548>\
**Category:** Help & Support\
**Created:** [March 17, 2019, 12:18pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548 "2019-03-17T12:18:25Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 12:18pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/1 "2019-03-17T12:18:25Z")

</div>

I don’t know why collision detection sometimes not work!!!

```javascript
    if (result.other.tags.has('Ball')){        
        result.other.destroy();
    }

```

![Record_2019_03_17_15_45_58_870](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/8/8d1f0e9f955d842c549388de9f7a632b840fa756.gif)

![Record_2019_03_17_15_45_43_977](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/f/f87c42e049e1d907a1afec345c65cbc040ab5471.gif)

![Record_2019_03_17_18_08_28_939](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/4/43ecb3bbe919507bfe9771b6fec60997d461af9e.gif)

![Record_2019_03_17_18_09_04_724](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/2/2b40fed24a0391bd1c13adc55401ee77712c0d47.gif)

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 12:36pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/2 "2019-03-17T12:36:56Z")

</div>

Judging from the gif provided by you - the physics works correctly. What event do you use to detect another collision? [Contact](https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#event:contact) event?

EDIT:  
In my practice, I encountered a similar problem. When the player’s entity moved on a flat surface, the collisionstart event was triggered with a certain periodicity, although it had to be worked only once, at the very beginning. I assume that the following problem exists in the physics engine. When contact is made with another entity and if the contact point is at the junction of the triangles of the model (as in the screenshot below), the collision event does not work. These are my personal observations. Alternatively, you can try to increase the number of polygons for models and check result.  
 ![Sphere](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/a/a1dced364dfac3555a50b05907bf17fe2916fbce.jpeg)

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [March 17, 2019, 12:36pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/3 "2019-03-17T12:36:58Z")

</div>

Are you 100% sure that every ball has the ‘Ball’ tag (with the same letter casing)?

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 1:08pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/4 "2019-03-17T13:08:02Z")

</div>

Yes I’m sure  
I don’t know why sometimes not work!

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 1:18pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/5 "2019-03-17T13:18:05Z")

</div>

you can see my project  
[https://playcanvas.com/editor/scene/723409](https://playcanvas.com/editor/scene/723409)

first I used collision start  
then I used contact and not work

this is my code that attached to MainBall

```javascript
var DestroyMe = pc.createScript('destroyMe');

DestroyMe.prototype.initialize = function() {
   this.entity.collision.on('contact', this.onContact, this);
};

DestroyMe.prototype.onContact = function (result) {

    if (result.other.rigidbody && result.other.tags.has('Ball')){
       result.other.destroy();
    }
};

```

also when I use applyImpulse my code not work  
why should I use applyForce Instead of applyImpulse!

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 1:21pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/6 "2019-03-17T13:21:58Z")

</div>

Try changing the event type to [collisionstart](https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#event:collisionstart). And try to apply the method I wrote above.

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 1:24pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/7 "2019-03-17T13:24:02Z")

</div>

as I mentioned above first I used oncollision start when It not work I decided to use another method  
but not work

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 1:35pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/8 "2019-03-17T13:35:39Z")

</div>

You can try for each ball to make the BoundingSphere a radius slightly larger than the ball itself. Then, using the [intersectsBoundingSphere()](https://developer.playcanvas.com/en/api/pc.BoundingSphere.html#intersectsBoundingSphere) method, check overlapping of this sphere with the main ball. I personally did not check this method.

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 1:51pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/9 "2019-03-17T13:51:55Z")

</div>

oh thanks!!! You deserve a full pitcher beer from me!  
I was looking for equivalent of Physics.SphereCast that I use in unity

```
var sphere = new pc.BoundingSphere(this.entity.getPosition(),2);
if(sphere.intersectsBoundingSphere){
    this.entity.destroy();
}

```

how can I compare tag in this way?

```javascript
if(sphere.intersectsBoundingSphere && tag === 'Ball'){

}

```

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 2:59pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/10 "2019-03-17T14:59:46Z")

</div>

> [@Seyed\_Morteza\_Kamali](#):
>
> how can I compare tag in this way?
> 
> ```javascript
> if(sphere.intersectsBoundingSphere && tag === 'Ball'){
> 
> }
> 
> ```

It’s not so simple. 🙂 Now I will try to recreate the situation on the test scene.

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 17, 2019, 3:03pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/11 "2019-03-17T15:03:46Z")

</div>

thanks  
I also tested collision detection by mesh  
 ![3](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/b/b17dd51d499678706c520d6dff4cfd685d77a346.png)

but it kill the performance and balls move from another!

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 4:37pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/12 "2019-03-17T16:37:58Z")

</div>

I did an entity check by tags. In [this](https://playcanvas.com/editor/scene/694808) scene, you are interested in the BoundingTest and PlayerMovement scripts. In the PlayerMovement script, I selected the necessary code sections in the comments. In fact, 2 spheres are created, one of which checks interaction with the other. The entity ball is also transferred via an event to another script that checks the condition by tags. If the tag is equal to the `Ball`, then the entity is removed from the scene. There is only one problem - these spheres are created on each frame, this can affect performance. I did not deal with the issue of code optimization. 🙂

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 17, 2019, 4:42pm UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/13 "2019-03-17T16:42:06Z")

</div>

> [@Seyed\_Morteza\_Kamali](#):
>
> I also tested collision detection by mesh  
> ![3](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/b/b17dd51d499678706c520d6dff4cfd685d77a346.png)
> 
> but it kill the performance and balls move from another!

Yes, in this case, the total number of triangles on the stage increases significantly, which slows down render loop.

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 18, 2019, 4:17am UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/14 "2019-03-18T04:17:31Z")

</div>

thanks but it doesn’t work when speed is high! 😣  
 ![Record_2019_03_18_07_45_02_4](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/7/7bfbd069eb596e7fb516c07c6ebe1848aced6029.gif) ![Record_2019_03_18_07_45_10_382](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/9/99b4107d4128f115e49e4298f2df32c957d99c97.gif)

I attached this script to brown ball

```javascript
var BoundingTest = pc.createScript('boundingTest');

BoundingTest.attributes.add('radius', { type: 'number', default: 1 });

// initialize code called once per entity
BoundingTest.prototype.initialize = function() {

};

BoundingTest.prototype.update = function(dt) {
    this.sphere = new pc.BoundingSphere(this.entity.getPosition(), this.radius);
    this.app.fire('test', this.sphere, this.entity);
};

```

I attach this script to white ball

```javascript
var DestroyBall = pc.createScript('destroyBall');

// initialize code called once per entity
DestroyBall.prototype.initialize = function() {
 var self = this;
this.app.on('test', function(sph, entity) {
    if(self.sphere.intersectsBoundingSphere(sph) && entity.tags.has('Ball')) {
        entity.destroy();
    } else {
        console.log('Entity cannot be removed from the scene.');
    }
});   
};

// update code called every frame
DestroyBall.prototype.update = function(dt) {
        this.sphere = new pc.BoundingSphere(this.entity.getPosition(), this.radius);
};

```

@yaustar have you any Idea?

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 18, 2019, 6:34am UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/15 "2019-03-18T06:34:56Z")

</div>

Have you tried to make a radius for spheres a bit larger than the model itself?

---

<div class="post-metadata">

**Author:** ![Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/seyed_morteza_kamali/32/5391_2.png) [@Seyed\_Morteza\_Kamali](https://forum.playcanvas.com/u/Seyed_Morteza_Kamali)\
**Post date:** [March 18, 2019, 7:12am UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/16 "2019-03-18T07:12:32Z")

</div>

yes It seems worked  
but currently I have another problem  
I can’t call a function inside app  
for example:

```javascript
this.app.on('test', function(sph, entity) {
this.entity.destroy(); // I can't access to current class
});

```

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 18, 2019, 7:28am UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/17 "2019-03-18T07:28:07Z")

</div>

It’s simple. You have lost the execution context for the nested function. Save the context to a separate variable and access the application through it.

```javascript
var self = this;
this.app.on('test', function(sph, entity) {
    self.entity.destroy(); // I can't access to current class
});

```

---

<div class="post-metadata">

**Author:** ![MasalovAndreey](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/masalovandreey/32/7325_2.png) [@MasalovAndreey](https://forum.playcanvas.com/u/MasalovAndreey)\
**Post date:** [March 18, 2019, 7:44am UTC](https://forum.playcanvas.com/t/collision-detection-problem/9548/18 "2019-03-18T07:44:25Z")

</div>

Why do you need to refer to the entity to which the script is attached? In my example, I delete the entity that is passed as an argument to the function.
