Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Collision detection problom
#1
Sorry to bother you. I want to create a system where when a cloth net collides with a rigid body, it indicates whether a collision has occurred. If a collision occurs, the scene should be reinitialized. However, with the current code, even though collisions appear to happen on screen, there is no indication of a collision being detected.
Here is the code attached to the  obi solver,  
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandlerchuang : MonoBehaviour
{
    ObiSolver solver;
    public int contactCount;
    private bool collisionOccurred = false;

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

    void OnEnable()
    {
        solver.OnParticleCollision += Solver_OnCollision;
    }

    void OnDisable()
    {
        solver.OnParticleCollision -= Solver_OnCollision;
    }

    void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {

        contactCount = 0;
        collisionOccurred = false;

        //foreach (var contact in e.contacts)
        //{
        //    if (contact.distance < 0.001f)
        //    {
        //        collisionOccurred = true;
        //        contactCount++;
        //    }
        //}

        var world = ObiColliderWorld.GetInstance();

        // just iterate over all contacts in the current frame:
        foreach (Oni.Contact contact in e.contacts)
        {
            // if this one is an actual collision:
            Debug.Log("no bang chuang");
            if (contact.distance < 0.01)
            {
                ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
                if (col != null)
                {
                    Debug.Log("bang chuang");
                    collisionOccurred = true;
                    contactCount++;
                }
            }
        }

    }

    public bool IsCollisionOccurred()
    {
        return collisionOccurred;
    }

    public int GetContactCount()
    {
        return contactCount;
    }

    void OnDrawGizmos()
    {
        // Existing OnDrawGizmos code
    }
}
Here is another code subscribed the collision event,  it is expected to return true when the collision happens. However, the false is always returned.
Code:
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;
using Obi;

public class SatelliteEnvController : MonoBehaviour
{
   
    public ObiSolverInitializer obiSolverInitializer;

    public CollisionEventHandlerchuang collisionHandlerchuang;
    public CollisionEventHandler collisionHandler;


    void Start()
    {
   
        ResetScene();
        obiSolverInitializer.InitializeObiSolverPosition();

        collisionHandlerchuang = FindObjectOfType<CollisionEventHandlerchuang>();
        collisionHandler = FindObjectOfType<CollisionEventHandler>();

        if (collisionHandlerchuang == null)
        {
            Debug.LogError("CollisionEventHandlerchuang not found in the scene.");
        }
        if (collisionHandler == null)
        {
            Debug.LogError("CollisionEventHandler not found in the scene.");
        }

    }

    void FixedUpdate()
    {
        if (collisionHandlerchuang != null)
        {
            bool collisionOccurred = collisionHandlerchuang.IsCollisionOccurred();
            int contactCount = collisionHandlerchuang.GetContactCount();

            Debug.Log("Collision occurred: " + collisionOccurred);
            Debug.Log("Number of contacts: " + contactCount);

        }
    }

   
Reply
#2
Hi,

You're subscribed to the wrong event. Should be OnCollision, not OnParticleCollision.

OnParticleCollision returns collisions between particles, while OnCollision returns "regular" collisions (against colliders).

See the manual for details:http://obi.virtualmethodstudio.com/manual/6.3/scriptingcollisions.html
Quote:Contacts can happen between a simplex and a collider, or between two simplices. To request the simplex-collider contact list from the solver, subscribe to its OnCollision event. To retrieve the simplex-simplex contact list, subscribe to its OnParticleCollision event.

kind regards,
Reply
#3
(24-06-2024, 07:52 AM)josemendez Wrote: Hi,

You're subscribed to the wrong event. Should be OnCollision, not OnParticleCollision.

OnParticleCollision returns collisions between particles, while OnCollision returns "regular" collisions (against colliders).

See the manual for details:http://obi.virtualmethodstudio.com/manual/6.3/scriptingcollisions.html

kind regards,
Thank you, I got it!
Reply