Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Detect specific rope collision.
#11
(19-11-2020, 02:24 PM)canerozdemir Wrote: Oh, I missed that one. However, the outcome is still the same. FPS drop happens and the game becomes unplayable. This is the code: 


Code:
public class RopeCollision : MonoBehaviour
{
    private ObiSolver _solver;

    private ObiSolver.ObiCollisionEventArgs _collisionEvent;

    private void Awake()
    {
        _solver = GetComponent<ObiSolver>();
    }

    private void OnEnable ()
    {
        _solver.OnParticleCollision += Solver_OnParticleCollision;
    }

    private void OnDisable()
    {
        _solver.OnParticleCollision -= Solver_OnParticleCollision;
    }

    private void Solver_OnParticleCollision (object sender, ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (var contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                var theCollider = world.colliderHandles[contact.other].owner;
                if (theCollider)
                {
                    print(theCollider.name);
                }
            }
        }
    }
}

Well, you're writing the collider name to console once per contact, no wonder performance goes down the drain  Huh. Writing to the console is extremely slow.
Remove the print() line and it should run acceptably fast.

Edit: also, you're using the second index of the contact (which for particle-particle contacts, is a particle index) to access the collider handles array, which will result in an out of bounds exception.
Reply
#12
(19-11-2020, 02:25 PM)josemendez Wrote: Well, you're writing the collider name to console once per contact, no wonder performance goes down the drain  Huh. Writing to the console is extremely slow.
Remove the print() line and it should run acceptably fast.

Edit: also, you're using the second index of the contact (which for particle-particle contacts, is a particle index) to access the collider handles array, which will result in an out of bounds exception.

I wasn't paying attention to the documentation, I believe. I wasn't being careful, because I thought it would work like CollisionStay callback but I forgot that I am actually calling for a particle collision which is very expensive considering that I have over 4000 particles on the scene Gran sonrisa This code actually does what I want: 

Code:
private void Solver_OnParticleCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (var contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                var pa = _solver.particleToActor[contact.particle];
                var po = _solver.particleToActor[contact.other];
                if (pa.actor.gameObject != po.actor.gameObject)
                {
                    Debug.Log(pa.actor.gameObject.name + " collides with: " + po.actor.gameObject.name);
                }
            }
        }
    }

I can now receive an information about who collides with whom. Therefore, I can now detect when a rope is colliding with another one and I can use it to expand the gameplay further. Thank you for putting out the obvious that I was missing out Gran sonrisa.
Reply
#13
(19-11-2020, 02:41 PM)canerozdemir Wrote: I wasn't paying attention to the documentation, I believe. I wasn't being careful, because I thought it would work like CollisionStay callback but I forgot that I am actually calling for a particle collision which is very expensive considering that I have over 4000 particles on the scene Gran sonrisa This code actually does what I want: 

Code:
private void Solver_OnParticleCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
    {
        var world = ObiColliderWorld.GetInstance();
        foreach (var contact in e.contacts)
        {
            // this one is an actual collision:
            if (contact.distance < 0.01)
            {
                var pa = _solver.particleToActor[contact.particle];
                var po = _solver.particleToActor[contact.other];
                if (pa.actor.gameObject != po.actor.gameObject)
                {
                    Debug.Log(pa.actor.gameObject.name + " collides with: " + po.actor.gameObject.name);
                }
            }
        }
    }

I can now receive an information about who collides with whom. Therefore, I can now detect when a rope is colliding with another one and I can use it to expand the gameplay further. Thank you for putting out the obvious that I was missing out Gran sonrisa.

That's 100% correct. Glad you got it working! Sonrisa.

Forgive me for not giving you the solution straight away. I'm a bit hesitant of giving ready-made solutions for this kind of thing, as understanding the code you're using is paramount. Later down the road, fixing someone else's code that you didn't get to properly understand is often harder than writing your own Sonrisa
Reply
#14
(19-11-2020, 02:50 PM)josemendez Wrote: That's 100% correct. Glad you got it working! Sonrisa.

Forgive me for not giving you the solution straight away. I'm a bit hesitant of giving ready-made solutions for this kind of thing, as understanding the code you're using is paramount. Later down the road, fixing someone else's code that you didn't get to properly understand is often harder than writing your own Sonrisa

Ah yes, I can't argue with you there. However, you have been more than helpful to me in this couple of weeks. I never expected a fast support like this when I started using the Obi Rope. Thank you again for being a reliable provider Sonrisa
Reply