This commit is contained in:
kimrdd 2024-02-03 11:06:55 +01:00
commit 63ce875dfc
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,23 +224,31 @@ 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);
if (collider == null) continue;
point.position.Set(newPos.x, newPos.y, point.position.z);
// A static collider was met, dont move any further
Vector2 resolvedPos = collider.ClosestPoint(initialPos);
foreach (var collider in Physics2D.OverlapCircleAll(point.position, ropeRadius, staticColliderMask))
{
if (collider == null) continue;
if (collider.isTrigger) continue;
if (Vector2.Distance(initialPos, resolvedPos) < ignoreResolveThreshold) continue;
// A static collider was met, dont move any further
Vector2 resolvedPos = collider.ClosestPoint(initialPos);
Vector2 penetrationDir = (resolvedPos - new Vector2(point.position.x, point.position.y)).normalized;
Vector2 finalPos = resolvedPos - penetrationDir * ropeRadius;
//Debug.Log($"resolved pos: {point.position}->{finalPos}");
point.position.Set(finalPos.x, finalPos.y, point.position.z);
break;
if (Vector2.Distance(initialPos, resolvedPos) < ignoreResolveThreshold) continue;
Vector2 penetrationDir = (resolvedPos - new Vector2(point.position.x, point.position.y)).normalized;
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;
}
// Move z position
@ -246,22 +257,26 @@ 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);
Vector2 resolvedPos = hitCollider.ClosestPoint(pointPos);
Vector2 penetration = resolvedPos - pointPos;
Vector2 finalPos = resolvedPos - penetration.normalized * ropeRadius;
// Register the squeze force this rope particle is squezing the collider
Vector2 pointPos = new Vector2(p.position.x, p.position.y);
Vector2 resolvedPos = hitCollider.ClosestPoint(pointPos);
Vector2 penetration = resolvedPos - pointPos;
Vector2 finalPos = resolvedPos - penetration.normalized * ropeRadius;
float squezeForce;
if (!colliderToSquezeForce.TryGetValue(hitCollider, out squezeForce))
colliderToSquezeForce.Add(hitCollider, squezeForce + penetration.magnitude);
else
colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude;
float squezeForce;
if (!colliderToSquezeForce.TryGetValue(hitCollider, out squezeForce))
colliderToSquezeForce.Add(hitCollider, squezeForce + penetration.magnitude);
else
colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude;
p.position.Set(finalPos.x, finalPos.y, p.position.z);
p.position.Set(finalPos.x, finalPos.y, p.position.z);
}
}
void CreateOrderArray()