Skip to content

Commit 731c765

Browse files
committed
Need to fix drag and friction algorithms
1 parent f6a7bb3 commit 731c765

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Game Physics/Assets/Scenes/Scene_Main.unity

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,14 @@ MonoBehaviour:
444444
staticFrictionCoefficient: 0.6
445445
staticFrictionNormal: {x: 1, y: 0}
446446
staticFrictionOpposingForce: {x: -1, y: 0}
447-
slidingNormalForce: {x: 1, y: 0}
447+
slidingNormalForce: {x: 1, y: -0.5}
448448
gravityActive: 1
449449
normalActive: 1
450450
slidingActive: 0
451451
staticFrictionActive: 0
452452
kinematicFrictionActive: 0
453453
dragActive: 0
454-
springActive: 0
454+
springActive: 1
455455
dampingSpringActive: 0
456456
--- !u!65 &1373288496
457457
BoxCollider:

Game Physics/Assets/Scripts/ForceGenerator.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ public static Vector2 GenerateForce_Friction_Static(Vector2 _forceNormal, Vector
3838
float max = _frictionCoefficient_Static * _forceNormal.magnitude;
3939

4040
Vector2 force = _frictionCoefficient_Static * _forceNormal;
41+
float mag = _forceOpposing.magnitude;
4142

42-
if (_forceOpposing.magnitude > max)
43+
if (mag > max)
4344
{
44-
force -= _forceOpposing;
45+
force = -_forceOpposing;
4546
}
4647
else
4748
{
48-
force = -_frictionCoefficient_Static * _forceNormal;
49+
force = -_forceOpposing * max / mag;
4950
}
5051

5152
return force;
@@ -55,7 +56,7 @@ public static Vector2 GenerateForce_Friction_Kinetic(Vector2 f_normal, Vector2 p
5556
{
5657
// f_friction_k = -coeff*|f_normal| * unit(vel)
5758

58-
Vector2 force = -frictionCoefficient_kinetic * f_normal.magnitude * particleVelocity;
59+
Vector2 force = -frictionCoefficient_kinetic * f_normal.magnitude * particleVelocity.normalized;
5960

6061
return force;
6162
}
@@ -64,7 +65,9 @@ public static Vector2 GenerateForce_Drag(Vector2 particleVelocity, Vector2 fluid
6465
{
6566
// f_drag = (p * u^2 * area * coeff)/2
6667

67-
Vector2 force = (particleVelocity - fluidVelocity) * (fluidDensity * particleVelocity.magnitude * particleVelocity.magnitude * objectArea_crossSection * objectDragCoefficient * 0.5f);
68+
Vector2 diff = particleVelocity - fluidVelocity;
69+
70+
Vector2 force = diff * diff.magnitude * (fluidDensity * objectArea_crossSection * objectDragCoefficient * 0.5f);
6871

6972
return force;
7073
}
@@ -77,11 +80,13 @@ public static Vector2 GenerateForce_Spring(Vector2 particlePosition, Vector2 anc
7780
// Calculate relative position of the particle to the anchor.
7881
Vector2 position = particlePosition - anchorPosition;
7982

83+
float springLength = position.magnitude;
84+
8085
// Generate the force.
81-
float force = -springStiffnessCoefficient * (position.magnitude - springRestingLength);
86+
float force = -springStiffnessCoefficient * (springLength - springRestingLength);
8287

8388
// Return the force at the current position.
84-
return position * force;
89+
return position * force / springLength;
8590
}
8691
public static Vector2 GenerateForce_Spring_Damping(Vector2 particlePosition, Vector2 anchorPosition, float springRestingLength, float springStiffnessCoefficient, float springDamping, float springConstant, Vector2 particleVelocity)
8792
{

0 commit comments

Comments
 (0)