fix rope multiple colliders

This commit is contained in:
Sveske Juice 2024-02-03 01:57:18 -08:00
parent 94a871d706
commit 14f78873ef
3 changed files with 2445 additions and 24 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a2f35f1528d14a74697feb586c5f86f2
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -101,6 +101,9 @@ public class RopeSimulator : MonoBehaviour
var colliderComponent = ropeCollider.AddComponent<CircleCollider2D>();
colliderComponent.radius = ropeRadius;
var rigidBody = ropeCollider.AddComponent<Rigidbody2D>();
rigidBody.isKinematic = true;
}
CreateOrderArray();
}
@ -221,12 +224,16 @@ public class RopeSimulator : MonoBehaviour
moveDir.Normalize();
Vector2 initialPos = new Vector2(point.position.x, point.position.y);
bool shouldBreak = false;
for (int i = 0 ; i < stepsRequired; i++)
{
Vector2 newPos = Vector2.MoveTowards(new Vector2(point.position.x, point.position.y), new Vector2(position.x, position.y), collisionCheckDist);
point.position.Set(newPos.x, newPos.y, point.position.z);
Collider2D collider = Physics2D.OverlapCircle(point.position, ropeRadius, staticColliderMask);
foreach (var collider in Physics2D.OverlapCircleAll(point.position, ropeRadius, staticColliderMask))
{
if (collider == null) continue;
if (collider.isTrigger) continue;
// A static collider was met, dont move any further
Vector2 resolvedPos = collider.ClosestPoint(initialPos);
@ -237,6 +244,10 @@ public class RopeSimulator : MonoBehaviour
Vector2 finalPos = resolvedPos - penetrationDir * ropeRadius;
//Debug.Log($"resolved pos: {point.position}->{finalPos}");
point.position.Set(finalPos.x, finalPos.y, point.position.z);
shouldBreak = true;
break;
}
if (shouldBreak)
break;
}
@ -246,8 +257,10 @@ public class RopeSimulator : MonoBehaviour
private void HandleStaticCollidersOfPoint(Point p)
{
Collider2D hitCollider = Physics2D.OverlapCircle(p.position, ropeRadius, staticColliderMask);
if (hitCollider == null) return;
foreach (var hitCollider in Physics2D.OverlapCircleAll(p.position, ropeRadius, staticColliderMask))
{
if (hitCollider == null) continue;
if (hitCollider.isTrigger) continue;
// Register the squeze force this rope particle is squezing the collider
Vector2 pointPos = new Vector2(p.position.x, p.position.y);
@ -262,6 +275,8 @@ public class RopeSimulator : MonoBehaviour
colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude;
p.position.Set(finalPos.x, finalPos.y, p.position.z);
}
}
void CreateOrderArray()