Vojtěch Zeman
Vojtěch Zeman

Reputation: 21

Continuous collision detection (CCD) in particle system in Unity, is that possible?

According to this article is possible to solve collision problems with fast moving objects in unity.

If you have particle system firing 1 sphere particle (ball throw for example) with collider and gravity modifier. And another moving collider with rigidbody (baseball bat for example), is there any possibility to make these object to NOT going through when they will collide in "high speed"? You cannot set "collision detection" on particle.

For better understainding I am adding two screenshots bellow.

Feel free for asking any question.

Collider(wall) is not moving, particle will bounce

Collider(wall) is moving towards ball with speed of "swing", particle goes through

Upvotes: 2

Views: 1253

Answers (1)

gbe
gbe

Reputation: 1007

The problem is because your objects are moving at a speed that they go through the collider. Say your object would move 10 units a frame, and it was 1 unit away from the collider. If the collider was 5 units long, it wouldn’t stop the moving object. This is because the next update will push it too far ahead of the next object.

First, don’t use a particle system for this. Just use a Rigidbody, because it is easier to handle in these situations.

You could solve this by doing a sweep test. A sweep test checks for colliders, where the object is going. You could just stop it from moving at the point it hit.

Rigidbody rb;

void Start()
{
    rb = GetComponent<Rigidbody>();
}
void Update()
{
    Sweep();
}
void Sweep()
{
    RaycastHit hit;
    if (Physics.SweepTest(rb.velocity, out hit, rb.velocity.magnitude))
    {
        rb.velocity = rb.velocity.normalized * hit.distance;
    }
}

Make sure this script is on your ‘baseball’, and add force somewhere in this script. Make sure that you are not using particle system, but you are using Rigidbody.

This was untested; let me know if it doesn’t work or you get an error.

Upvotes: 0

Related Questions