Commit 60ada874 authored by rv3Dcv's avatar rv3Dcv

Bugfix multicollision solving

When multiple collisions occured in the same frame, Unity overrites their Collision Object of the OnCollisionEnter and OnCollisionStay methods with the latest collision. Since the Collision object is an reference type, the last collision got solved twice, while the second last collision got not solved. This bug is fixed now.
parent b41902f7
...@@ -248,6 +248,10 @@ PrefabInstance: ...@@ -248,6 +248,10 @@ PrefabInstance:
m_Modification: m_Modification:
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 0}
m_Modifications: m_Modifications:
- target: {fileID: 3537744961480436555, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
propertyPath: collisionMargin
value: 0.001
objectReference: {fileID: 0}
- target: {fileID: 3901088838006394844, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3} - target: {fileID: 3901088838006394844, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
propertyPath: doCollisionTestOne propertyPath: doCollisionTestOne
value: 0 value: 0
...@@ -340,8 +344,7 @@ PrefabInstance: ...@@ -340,8 +344,7 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: Simulation value: Simulation
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: m_RemovedComponents: []
- {fileID: 3537744961480436555, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
m_SourcePrefab: {fileID: 100100000, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
--- !u!1 &338937529 --- !u!1 &338937529
GameObject: GameObject:
...@@ -529,24 +532,6 @@ Transform: ...@@ -529,24 +532,6 @@ Transform:
m_CorrespondingSourceObject: {fileID: 8551267917678092659, guid: 71f983e886cdf6b418bb6e5402e7361d, type: 3} m_CorrespondingSourceObject: {fileID: 8551267917678092659, guid: 71f983e886cdf6b418bb6e5402e7361d, type: 3}
m_PrefabInstance: {fileID: 8551267918195034930} m_PrefabInstance: {fileID: 8551267918195034930}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1 &1678987259 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 4233146888435007114, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
m_PrefabInstance: {fileID: 173875006}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1678987260
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1678987259}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f02fb1d019bf486469f64a89aa7a9ec1, type: 3}
m_Name:
m_EditorClassIdentifier:
collisionMargin: 0.001
--- !u!1001 &1209666942526037230 --- !u!1001 &1209666942526037230
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
......
...@@ -968,8 +968,7 @@ PrefabInstance: ...@@ -968,8 +968,7 @@ PrefabInstance:
propertyPath: pullForceTestThree.y propertyPath: pullForceTestThree.y
value: 10 value: 10
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: m_RemovedComponents: []
- {fileID: 3537744961480436555, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
m_SourcePrefab: {fileID: 100100000, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 0e128b35dc3ea6746b6de609560d672d, type: 3}
--- !u!1 &1672042465 stripped --- !u!1 &1672042465 stripped
GameObject: GameObject:
...@@ -989,19 +988,6 @@ MonoBehaviour: ...@@ -989,19 +988,6 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
doStressTestOne: 0 doStressTestOne: 0
--- !u!114 &1672042467
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1672042465}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f02fb1d019bf486469f64a89aa7a9ec1, type: 3}
m_Name:
m_EditorClassIdentifier:
collisionMargin: 0.1
--- !u!1 &1725437441 stripped --- !u!1 &1725437441 stripped
GameObject: GameObject:
m_CorrespondingSourceObject: {fileID: 6340281792987779153, guid: 88fa8b72e818c4d42a9cc41e7ad138d0, type: 3} m_CorrespondingSourceObject: {fileID: 6340281792987779153, guid: 88fa8b72e818c4d42a9cc41e7ad138d0, type: 3}
......
...@@ -59,9 +59,12 @@ namespace GuidewireSim ...@@ -59,9 +59,12 @@ namespace GuidewireSim
*/ */
private void OnCollisionEnter(Collision other) private void OnCollisionEnter(Collision other)
{ {
Debug.Log("trigger " + transform.name); ContactPoint collisionContact = other.GetContact(0);
collisionHandler.RegisterCollision(this.transform, other, sphereID); Vector3 contactPoint = collisionContact.point;
Vector3 collisionNormal = collisionContact.normal;
collisionHandler.RegisterCollision(this.transform, sphereID, contactPoint, collisionNormal);
} }
/** /**
...@@ -69,9 +72,12 @@ namespace GuidewireSim ...@@ -69,9 +72,12 @@ namespace GuidewireSim
*/ */
private void OnCollisionStay(Collision other) private void OnCollisionStay(Collision other)
{ {
Debug.Log("trigger " + transform.name); ContactPoint collisionContact = other.GetContact(0);
Vector3 contactPoint = collisionContact.point;
Vector3 collisionNormal = collisionContact.normal;
collisionHandler.RegisterCollision(this.transform, other, sphereID); collisionHandler.RegisterCollision(this.transform, sphereID, contactPoint, collisionNormal);
} }
} }
} }
\ No newline at end of file
...@@ -32,10 +32,16 @@ namespace GuidewireSim ...@@ -32,10 +32,16 @@ namespace GuidewireSim
* @param collision The Collision instance. * @param collision The Collision instance.
* @param sphereID The unique ID of @p sphere. * @param sphereID The unique ID of @p sphere.
*/ */
public void RegisterCollision(Transform sphere, Collision collision, int sphereID) public void RegisterCollision(Transform sphere, int sphereID, Vector3 contactPoint, Vector3 collisionNormal)
{ {
CollisionPair registeredCollision = new CollisionPair(sphere, collision, sphereID); // Debug.Log(sphereID + " " + collision.GetContact(0).point);
CollisionPair registeredCollision = new CollisionPair(sphere, sphereID, contactPoint, collisionNormal);
registeredCollisions.Add(registeredCollision); registeredCollisions.Add(registeredCollision);
// for (int index = 0; index < registeredCollisions.Count; index++)
// {
// Debug.Log("entry" + index + " " + registeredCollisions[index].sphereID + " " + registeredCollisions[index].contactPoint);
// }
} }
/** /**
......
...@@ -10,14 +10,16 @@ namespace GuidewireSim ...@@ -10,14 +10,16 @@ namespace GuidewireSim
public struct CollisionPair public struct CollisionPair
{ {
public Transform sphere; //!< The sphere object of the guidewire that was part of the collision. public Transform sphere; //!< The sphere object of the guidewire that was part of the collision.
public Collision vesselCollider; //!< The blood vessel object that was part of the collision. public Vector3 contactPoint; //!< The contact point of the collision.
public Vector3 collisionNormal; //!< The normal of the collision.
public int sphereID; //!< The ID of the sphere object of the guidewire that was part of the collision. public int sphereID; //!< The ID of the sphere object of the guidewire that was part of the collision.
public CollisionPair(Transform sphere, Collision vesselCollider, int sphereID) public CollisionPair(Transform sphere, int sphereID, Vector3 contactPoint, Vector3 collisionNormal)
{ {
this.sphere = sphere; this.sphere = sphere;
this.vesselCollider = vesselCollider;
this.sphereID = sphereID; this.sphereID = sphereID;
this.contactPoint = contactPoint;
this.collisionNormal = collisionNormal;
} }
} }
} }
\ No newline at end of file
...@@ -43,13 +43,15 @@ namespace GuidewireSim ...@@ -43,13 +43,15 @@ namespace GuidewireSim
*/ */
public void SolveCollisionConstraints(Vector3[] spherePositionPredictions, int solverStep, int constraintSolverSteps) public void SolveCollisionConstraints(Vector3[] spherePositionPredictions, int solverStep, int constraintSolverSteps)
{ {
for (int collisionIndex = 0; collisionIndex < collisionHandler.registeredCollisions.Count; collisionIndex++) for (int collisionIndex = 0; collisionIndex < collisionHandler.registeredCollisions.Count; collisionIndex++)
{ {
CollisionPair collisionPair = collisionHandler.registeredCollisions[collisionIndex]; CollisionPair collisionPair = collisionHandler.registeredCollisions[collisionIndex];
int sphereID = collisionPair.sphereID; int sphereID = collisionPair.sphereID;
Vector3 spherePositionPrediction = spherePositionPredictions[sphereID]; Vector3 spherePositionPrediction = spherePositionPredictions[sphereID];
SolveCollisionConstraint(spherePositionPrediction, collisionPair, solverStep, out deltaPosition); SolveCollisionConstraint(spherePositionPrediction, collisionPair.contactPoint, collisionPair.collisionNormal,
solverStep, out deltaPosition);
CorrectCollisionPredictions(sphereID, spherePositionPredictions, solverStep, constraintSolverSteps); CorrectCollisionPredictions(sphereID, spherePositionPredictions, solverStep, constraintSolverSteps);
} }
} }
...@@ -61,35 +63,27 @@ namespace GuidewireSim ...@@ -61,35 +63,27 @@ namespace GuidewireSim
* @param solverStep The current iteration of the constraint solving step. * @param solverStep The current iteration of the constraint solving step.
* @attention Current calculation of the normal only works for spheres. * @attention Current calculation of the normal only works for spheres.
*/ */
protected virtual void SolveCollisionConstraint(Vector3 spherePositionPrediction, CollisionPair collisionPair, int solverStep, protected virtual void SolveCollisionConstraint(Vector3 spherePositionPrediction, Vector3 contactPoint, Vector3 collisionNormal,
out Vector3 deltaPosition) int solverStep, out Vector3 deltaPosition)
{ {
ContactPoint contactPoint = collisionPair.vesselCollider.GetContact(0);
Vector3 otherPosition = contactPoint.otherCollider.transform.position;
Vector3 closestSurfacePoint = contactPoint.point;
Vector3 normalVector = contactPoint.normal;
if (solverStep == 0) if (solverStep == 0)
{ {
DrawCollisionInformation(spherePositionPrediction, contactPoint, otherPosition, closestSurfacePoint, normalVector); DrawCollisionInformation(spherePositionPrediction, contactPoint, collisionNormal);
} }
deltaPosition = CalculateDeltaPosition(spherePositionPrediction, closestSurfacePoint, normalVector); deltaPosition = CalculateDeltaPosition(spherePositionPrediction, contactPoint, collisionNormal);
} }
/** /**
* Draws the contact point, collision normal, and displacement corrections into the scene. * Draws the contact point, collision normal, and displacement corrections into the scene.
*/ */
protected void DrawCollisionInformation(Vector3 spherePositionPrediction, ContactPoint contactPoint, Vector3 otherPosition, protected void DrawCollisionInformation(Vector3 spherePositionPrediction, Vector3 contactPoint, Vector3 collisionNormal)
Vector3 closestSurfacePoint, Vector3 normalVector)
{ {
Debug.DrawLine(closestSurfacePoint, closestSurfacePoint + 10f * normalVector, Color.blue, 2f); Debug.DrawLine(contactPoint, contactPoint + 10f * collisionNormal, Color.blue, 2f);
DebugExtension.DrawPoint(spherePositionPrediction, Color.white); DebugExtension.DrawPoint(spherePositionPrediction, Color.white);
DebugExtension.DrawPoint(contactPoint.point, Color.black); DebugExtension.DrawPoint(contactPoint, Color.yellow);
DebugExtension.DrawPoint(closestSurfacePoint, Color.yellow);
} }
/** /**
...@@ -120,13 +114,10 @@ namespace GuidewireSim ...@@ -120,13 +114,10 @@ namespace GuidewireSim
initialPositionPrediction = spherePositionPredictions[sphereIndex]; initialPositionPrediction = spherePositionPredictions[sphereIndex];
} }
// spherePositionPredictions[sphereIndex] += Mathf.Pow(1 - (1 - collisionStiffness), 1 / 1000f) * deltaPosition;
spherePositionPredictions[sphereIndex] += collisionStiffness * deltaPosition; spherePositionPredictions[sphereIndex] += collisionStiffness * deltaPosition;
// spherePositionPredictions[sphereIndex] += deltaPosition;
if (solverStep == constraintSolverSteps - 1) if (solverStep == constraintSolverSteps - 1)
{ {
Debug.DrawLine(initialPositionPrediction, spherePositionPredictions[sphereIndex], Color.red, 2f);
DebugExtension.DrawPoint(spherePositionPredictions[sphereIndex], Color.red); DebugExtension.DrawPoint(spherePositionPredictions[sphereIndex], Color.red);
} }
} }
......
...@@ -9,21 +9,5 @@ namespace GuidewireSim ...@@ -9,21 +9,5 @@ namespace GuidewireSim
*/ */
public class CollisionSolvingStep_UnityVariant : CollisionSolvingStep public class CollisionSolvingStep_UnityVariant : CollisionSolvingStep
{ {
protected override void SolveCollisionConstraint(Vector3 spherePositionPrediction, CollisionPair collisionPair, int solverStep,
out Vector3 deltaPosition)
{
ContactPoint contactPoint = collisionPair.vesselCollider.GetContact(0);
Vector3 otherPosition = contactPoint.otherCollider.transform.position;
Vector3 closestSurfacePoint = contactPoint.point;
Vector3 normalVector = contactPoint.normal;
if (solverStep == 0)
{
DrawCollisionInformation(spherePositionPrediction, contactPoint, otherPosition, closestSurfacePoint, normalVector);
}
deltaPosition = CalculateDeltaPosition(spherePositionPrediction, closestSurfacePoint, normalVector);
}
} }
} }
\ No newline at end of file
...@@ -6,34 +6,34 @@ EditorUserSettings: ...@@ -6,34 +6,34 @@ EditorUserSettings:
serializedVersion: 4 serializedVersion: 4
m_ConfigSettings: m_ConfigSettings:
RecentlyUsedScenePath-0: RecentlyUsedScenePath-0:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee3121ed191e18d111d31fe51a0fc32915dec822fdecdbd1ebdfd6e2f3c1e8deb2fbf5fefab8a4bfb0f5a3bce5 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc22314ec182318d51dfa2fc61608d22f1ddcc41cdff79bc3dae3ccf2
flags: 0 flags: 0
RecentlyUsedScenePath-1: RecentlyUsedScenePath-1:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc22314ec182318d51dfa2fc61608d22f1ddcc41cdff79bc3dae3ccf2 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc23810ed191c04d637c710cd1c08d85709c0c409c8
flags: 0 flags: 0
RecentlyUsedScenePath-2: RecentlyUsedScenePath-2:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc23810ed191c04d637c710cd1c08d85709c0c409c8 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc23e03f61d1a05cc0ec029d81719d91c23fbc314c5fae3d7c6e3d9e5f396cbd1a9e6ed
flags: 0 flags: 0
RecentlyUsedScenePath-3: RecentlyUsedScenePath-3:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc23e03f61d1a05cc0ec029d81719d91c23fbc314c5fae3d7c6e3d9e5f396cbd1a9e6ed value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc9e8deee7faf9fae1
flags: 0 flags: 0
RecentlyUsedScenePath-4: RecentlyUsedScenePath-4:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc9e8deee7faf9fae1 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc9e8eeee7faf9fae1
flags: 0 flags: 0
RecentlyUsedScenePath-5: RecentlyUsedScenePath-5:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc9e8eeee7faf9fae1 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc90caaefbe0e9
flags: 0 flags: 0
RecentlyUsedScenePath-6: RecentlyUsedScenePath-6:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f735585bc51b020e030bee313cfa031b33cc1ed008cb1e08c21612f1f813d8f7cce0d5f8d1eae9cc90caaefbe0e9 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141db5011dfb595d04cb11d103
flags: 0 flags: 0
RecentlyUsedScenePath-7: RecentlyUsedScenePath-7:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141db5011dfb595d04cb11d103 value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355a5bc51b020e030bee3130f0020710fa3ddd1bc50f10ce264f80d813d8f7cc
flags: 0 flags: 0
RecentlyUsedScenePath-8: RecentlyUsedScenePath-8:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355a5bc51b020e030bee3130f0020710fa3ddd1bc50f10ce264f80d813d8f7cc value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355c5bc51b020e030bee3130f0020710fa3ddd1bc50f10ce264f80d813d8f7cc
flags: 0 flags: 0
RecentlyUsedScenePath-9: RecentlyUsedScenePath-9:
value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355c5bc51b020e030bee3130f0020710fa3ddd1bc50f10ce264f80d813d8f7cc value: 224247031146467c18070827072a4d1529360b39293c357f1e2a183de7f37b1bedeb3ae0f43330320d07fc28373d0431f7355b38e6030f19141dc22314ec182318d51dfa36c7111bf42c12c7d904e7e2c7dfd5e4cca5f2d6d7cbb9
flags: 0 flags: 0
vcSharedLogLevel: vcSharedLogLevel:
value: 0d5e400f0650 value: 0d5e400f0650
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment