01-05-2024, 12:35 PM
Hi, the idea is simple, to improve the game feel when character make contact with the softbody.
I would like to have softbody handle OnContact by iterate the contacts and add the incoming contact impulse with a mult value,
so that we can have a squashed feel when character step on a slime.
When it works, it is very cool to look at !
But eventually it can be very easy to spiral into a dead performance degradation, since each time you got contact, you have to iterate the contacts, and then iterate the particles to detect which particle is near to add the bounce force.
For example: 10 contact, 1000 particles, that is 10000 iterations each time we got contact. Wow!
How can I optimize this?
I would like to have softbody handle OnContact by iterate the contacts and add the incoming contact impulse with a mult value,
so that we can have a squashed feel when character step on a slime.
When it works, it is very cool to look at !
But eventually it can be very easy to spiral into a dead performance degradation, since each time you got contact, you have to iterate the contacts, and then iterate the particles to detect which particle is near to add the bounce force.
For example: 10 contact, 1000 particles, that is 10000 iterations each time we got contact. Wow!
How can I optimize this?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
using Unity.Mathematics;
namespace SPIKE.kIntegration
{
//when got some contact, be squashed by that contact in normal direction
[RequireComponent(typeof(ObiActor))]
public class ObiBouncer : MonoBehaviour
{
[SerializeField] ObiSolver _solver;
[SerializeField] float _propagate = 0.5f, _bounceForceMult = 10,_clampBounceForce=10;
[SerializeField] bool _debug;
ObiActor actor;
void Awake()
{
actor = GetComponent<ObiActor>();
}
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)
{
Vector3 contactOnCollider = contact.pointB;
contactOnCollider = _solver.transform.TransformPoint(contactOnCollider);
Vector3 contactNm = contact.normal;
#if UNITY_EDITOR
if (_debug)
{
Debug.DrawLine(contactOnCollider, contactOnCollider + contactNm, Color.yellow, 2f);
}
#endif
//iterate the particles
for (int i = 0; i < actor.solverIndices.Length; ++i)
{
int solverIndex = actor.solverIndices[i];
//add bounce force based on the propagate distance
if (Vector3.Distance(actor.GetParticlePosition(solverIndex), contactOnCollider)
< _propagate)
{
var squashForce = contact.normal * math.min( contact.normalImpulse * _bounceForceMult,_clampBounceForce);
actor.solver.velocities[solverIndex] += squashForce;
}
}
}
}
}
}
}