fix rope multiple colliders
This commit is contained in:
parent
94a871d706
commit
14f78873ef
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,7 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a2f35f1528d14a74697feb586c5f86f2
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -101,6 +101,9 @@ public class RopeSimulator : MonoBehaviour
|
||||||
|
|
||||||
var colliderComponent = ropeCollider.AddComponent<CircleCollider2D>();
|
var colliderComponent = ropeCollider.AddComponent<CircleCollider2D>();
|
||||||
colliderComponent.radius = ropeRadius;
|
colliderComponent.radius = ropeRadius;
|
||||||
|
|
||||||
|
var rigidBody = ropeCollider.AddComponent<Rigidbody2D>();
|
||||||
|
rigidBody.isKinematic = true;
|
||||||
}
|
}
|
||||||
CreateOrderArray();
|
CreateOrderArray();
|
||||||
}
|
}
|
||||||
|
@ -221,23 +224,31 @@ public class RopeSimulator : MonoBehaviour
|
||||||
moveDir.Normalize();
|
moveDir.Normalize();
|
||||||
|
|
||||||
Vector2 initialPos = new Vector2(point.position.x, point.position.y);
|
Vector2 initialPos = new Vector2(point.position.x, point.position.y);
|
||||||
|
bool shouldBreak = false;
|
||||||
for (int i = 0 ; i < stepsRequired; i++)
|
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);
|
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);
|
point.position.Set(newPos.x, newPos.y, point.position.z);
|
||||||
Collider2D collider = Physics2D.OverlapCircle(point.position, ropeRadius, staticColliderMask);
|
|
||||||
if (collider == null) continue;
|
|
||||||
|
|
||||||
// A static collider was met, dont move any further
|
foreach (var collider in Physics2D.OverlapCircleAll(point.position, ropeRadius, staticColliderMask))
|
||||||
Vector2 resolvedPos = collider.ClosestPoint(initialPos);
|
{
|
||||||
|
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;
|
if (Vector2.Distance(initialPos, resolvedPos) < ignoreResolveThreshold) continue;
|
||||||
Vector2 finalPos = resolvedPos - penetrationDir * ropeRadius;
|
|
||||||
//Debug.Log($"resolved pos: {point.position}->{finalPos}");
|
Vector2 penetrationDir = (resolvedPos - new Vector2(point.position.x, point.position.y)).normalized;
|
||||||
point.position.Set(finalPos.x, finalPos.y, point.position.z);
|
Vector2 finalPos = resolvedPos - penetrationDir * ropeRadius;
|
||||||
break;
|
//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
|
// Move z position
|
||||||
|
@ -246,22 +257,26 @@ public class RopeSimulator : MonoBehaviour
|
||||||
|
|
||||||
private void HandleStaticCollidersOfPoint(Point p)
|
private void HandleStaticCollidersOfPoint(Point p)
|
||||||
{
|
{
|
||||||
Collider2D hitCollider = Physics2D.OverlapCircle(p.position, ropeRadius, staticColliderMask);
|
foreach (var hitCollider in Physics2D.OverlapCircleAll(p.position, ropeRadius, staticColliderMask))
|
||||||
if (hitCollider == null) return;
|
{
|
||||||
|
if (hitCollider == null) continue;
|
||||||
|
if (hitCollider.isTrigger) continue;
|
||||||
|
|
||||||
// Register the squeze force this rope particle is squezing the collider
|
// Register the squeze force this rope particle is squezing the collider
|
||||||
Vector2 pointPos = new Vector2(p.position.x, p.position.y);
|
Vector2 pointPos = new Vector2(p.position.x, p.position.y);
|
||||||
Vector2 resolvedPos = hitCollider.ClosestPoint(pointPos);
|
Vector2 resolvedPos = hitCollider.ClosestPoint(pointPos);
|
||||||
Vector2 penetration = resolvedPos - pointPos;
|
Vector2 penetration = resolvedPos - pointPos;
|
||||||
Vector2 finalPos = resolvedPos - penetration.normalized * ropeRadius;
|
Vector2 finalPos = resolvedPos - penetration.normalized * ropeRadius;
|
||||||
|
|
||||||
float squezeForce;
|
float squezeForce;
|
||||||
if (!colliderToSquezeForce.TryGetValue(hitCollider, out squezeForce))
|
if (!colliderToSquezeForce.TryGetValue(hitCollider, out squezeForce))
|
||||||
colliderToSquezeForce.Add(hitCollider, squezeForce + penetration.magnitude);
|
colliderToSquezeForce.Add(hitCollider, squezeForce + penetration.magnitude);
|
||||||
else
|
else
|
||||||
colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude;
|
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()
|
void CreateOrderArray()
|
||||||
|
|
Loading…
Reference in New Issue