Obi Official Forum
Help Segregation of instructions to each rope at the time of collision - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Help Segregation of instructions to each rope at the time of collision (/thread-2768.html)



Segregation of instructions to each rope at the time of collision - NakanHogeri - 26-02-2021

I'm currently trying to change the color of two Obi Rope in the same Solver at their own timing when they collide with other objects.

I pasted the script below on each rope and tried it, and when one rope collided with another object, the color of the other rope was the same.

Code:
private void Solver_OnCollision(ObiSolver solver, ObiSolver.ObiCollisionEventArgs e)
    {
        foreach (Oni.Contact contact in e.contacts)
        {
            if (contact.distance < 0.025f)
            {
                    ChangeColorDark();
            }
        }
    }


Obi version 6.0, Surface-based collisions are enabled on each rope.

If I had two Obi Solvers in the scene and one rope as a child object of one Solver, the current script would work, but it would be very costly.

I would be very grateful if you could let me know if you have an elegant solution.

Thanks.


RE: Segregation of instructions to each rope at the time of collision - josemendez - 26-02-2021

Hi,

Collision callbacks return all contacts for all actors in the solver,  you can't just grab any contact and then change the color of the current actor, as the contact might be generated by a different one. What you want to do instead is:

- Iterate over all contacts in the solver just once! (not once per rope, as that would quickly become really slow and impractical as you point out).
- See which actor is involved in the contact.
- Change the color of that actor.

The manual contains examples on how to retrieve the actor involved in a contact, see:
http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html


RE: Segregation of instructions to each rope at the time of collision - NakanHogeri - 26-02-2021

Thank you for your reply.

When I tried to call the function to change the color of each rope from the script assigned to Solver, I wrote the following code and got an error.

Code:
private void Solver_OnCollision(ObiSolver solver, ObiSolver.ObiCollisionEventArgs e)
    {
        foreach (Oni.Contact contact in e.contacts)
        {
            if (contact.distance < 0.025f)
            {
          var collidedActor = solver.particleToActor[contact.bodyA];
              if (collidedActor != null)
              {
            RopeController ropecon = collidedActor.GetComponent<RopeController>();
                ropecon.ChangeColorDark();
              }
            }
        }
    }

I think GetComponent works with Obi Collider, but doesn't it work with Obi Rope?
And is there an alternative?

I'm sorry for the beginner-smelling question, but thank you.


RE: Segregation of instructions to each rope at the time of collision - josemendez - 26-02-2021

GetComponent() is a Unity function, works with any GameObject in unity, including ropes. However the "collidedActor" variable is a struct of type ParticleInActor, so you can't use GetComponent on it. You should do:
Quote:collidedActor.actor.GetComponent<RopeController>();

cheers!


RE: Segregation of instructions to each rope at the time of collision - NakanHogeri - 26-02-2021

(26-02-2021, 04:15 PM)josemendez Wrote: GetComponent() is a Unity function, works with any GameObject in unity, including ropes. However the "collidedActor" variable is a struct of type ParticleInActor, so you can't use GetComponent on it. You should do:

cheers!

It worked as expected!

Thank you very much!