Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect Collision between rope and collider
#1
I have 3 ropes in my scene, all of them inside one Obisolver, and they collide with the ground and also some walls. I want to make a condition that return true when all those ropes didn't collide with any wall (but still colliding with the ground), I tried the script below but it didn't work properly, I mean the condition return false even a rope is still colliding with the wall, I think it's because there's 3 ropes in the same ObiSolver, how can I solve this please?

Code:
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       frame = e;

       for (int i = 0; i < e.contacts.Count; ++i)
       {
           if (e.contacts.Data[i].distance < 0.1f)
           {
               Component thecollider;
               if (ObiColliderBase.idToCollider.TryGetValue(e.contacts.Data[i].other, out thecollider))
               {

                   if (thecollider.CompareTag("Wall"))
                   {
                       TouchingWall = true;
                   }
                   else
                   {
                       TouchingWall = false;
                   }

               }
           }
       }

   }
Reply
#2
Quote:I want to make a condition that return true when all those ropes didn't collide with any wall

Your code will return true if the last contact in the list is against a wall, false otherwise. You constantly overwrite TouchingWall. Its value will always be that of the last contact.

Instead you should set TouchingWall to true at the beginning of the method, then set it to false when you find at least one contact against the wall.  Actually, renaming it to NotTouchingWall would be better Guiño, since you want "true" when no rope is colliding against a wall.


Code:
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       frame = e;
       NotTouchingWall = true;
       for (int i = 0; i < e.contacts.Count; ++i)
       {
           if (e.contacts.Data[i].distance < 0.1f)
           {
               Component thecollider;
               if (ObiColliderBase.idToCollider.TryGetValue(e.contacts.Data[i].other, out thecollider))
               {

                   if (thecollider.CompareTag("Wall"))
                       NotTouchingWall = false;
               }
           }
       }

   }

Btw, it does not matter how many actors (ropes) are there in a solver, the list of contacts returned is for all particles in the solver.
Reply
#3
(19-05-2020, 06:32 PM)josemendez Wrote: Your code will return true if the last contact in the list is against a wall, false otherwise. You constantly overwrite TouchingWall. Its value will always be that of the last contact.

Instead you should set TouchingWall to true at the beginning of the method, then set it to false when you find at least one contact against the wall.  Actually, renaming it to NotTouchingWall would be better Guiño, since you want "true" when no rope is colliding against a wall.


Code:
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       frame = e;
       NotTouchingWall = true;
       for (int i = 0; i < e.contacts.Count; ++i)
       {
           if (e.contacts.Data[i].distance < 0.1f)
           {
               Component thecollider;
               if (ObiColliderBase.idToCollider.TryGetValue(e.contacts.Data[i].other, out thecollider))
               {

                   if (thecollider.CompareTag("Wall"))
                       NotTouchingWall = false;
               }
           }
       }

   }

Btw, it does not matter how many actors (ropes) are there in a solver, the list of contacts returned is for all particles in the solver.

Thank you!
Reply
#4
And about the ropes collision between each other (particle collisions), how can I distinguish when a rope collide with itself and when it collide with other rope?
Reply
#5
(19-05-2020, 11:46 PM)Kifwat Wrote: And about the ropes collision between each other (particle collisions), how can I distinguish when a rope collide with itself and when it collide with other rope?

See "Retrieving the actor involved in a contact" in the manual:
http://obi.virtualmethodstudio.com/tutor...sions.html
Reply