24-06-2024, 04:07 AM
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,
Here is another code subscribed the collision event, it is expected to return true when the collision happens. However, the false is always returned.
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
}
}
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);
}
}