28-04-2024, 09:19 AM
(This post was last modified: 28-04-2024, 09:21 AM by spikebor.
Edit Reason: add image
)
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?
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?
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);
}
}
}
}
}