Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Debug Collisions position and normal
#1
Pregunta 
Hi there, I can use this code to collide with the Obi softbody and push my character up,
I just have a problem with debugging the collision position and normal.
In this picture, the yellow line is the debug line, it does not look like it's drawing correct contact position or normal.
From my understanding, using solver.transform.TransformPoint and solver.transform.TransformDirection is the way to convert those local data to world space, but why it's incorrect?

[Image: 3uIgNQo.png]

Code:
[RequireComponent(typeof(ObiSolver))]
    public class ObiCollisionEventHandler : MonoBehaviour
    {
        [SerializeField] UnityEngine.Events.UnityEvent<Collider,Vector3,Vector3> _onCollision;
        [SerializeField] float _minImpulse=0.1f;
        [SerializeField] bool _debug;
        ObiSolver solver;

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

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

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

        void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
        {
            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:
                if (contact.distance < 0.01)
                {
                    if (contact.normalImpulse < _minImpulse) continue;

                    Vector3 contactMid = (contact.pointA + contact.pointB) / 2f;
                    contactMid = solver.transform.TransformPoint(contactMid);
                    Vector3 contactNm = contact.normal.normalized;
                    contactNm = solver.transform.TransformDirection(contactNm);

#if UNITY_EDITOR
                    if (_debug) {
                        Debug.DrawLine(contactMid, contactMid + contactNm, Color.yellow, 2f);
                    }
#endif
                    var obiCol = world.colliderHandles[contact.bodyB].owner as ObiCollider;
                    if (obiCol != null)
                    {                       
                        // do something with the collider.
                        _onCollision?.Invoke(obiCol.sourceCollider, contactMid, contactNm);
                    }
                }
            }
        }
    }
Reply
#2
Hi!

contact.pointA is a barycentric point on the simplex, averaging it together with contact.pointB (which is a point on the surface of the collider, expressed in solver space) doesn’t make any sense. See:

https://obi.virtualmethodstudio.com/manu...sions.html
Reply
#3
(28-04-2024, 06:53 PM)josemendez Wrote: Hi!

contact.pointA is a barycentric point on the simplex, averaging it together with contact.pointB (which is a point on the surface of the collider, expressed in solver space) doesn’t make any sense. See:

https://obi.virtualmethodstudio.com/manu...sions.html

Thank you, I got the position correct now.
I'm using Vector3 contactNm = contact.normal.normalized;
Is this correct? I think it should be inverted.

[Image: rkYK2ac.png]
Reply
#4
Hi,

Yes, it's correct. Normals are perpendicular to the surface the particles are colliding against -in this case, the character's collider- so they're pointing the right way. If you want to invert them, just negate them.

Note there is no need to normalize normals, they have unit length by definition.

kind regards,
Reply