Rope shrink extend done

This commit is contained in:
Sveske Juice 2024-02-03 11:01:12 -08:00
parent cd20189a2c
commit e971b101b4
6 changed files with 99 additions and 36 deletions

View File

@ -0,0 +1,36 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c46f07b5ed07e4e92aa78254188d3d10, type: 3}
m_Name: InputSystem.inputsettings
m_EditorClassIdentifier:
m_SupportedDevices: []
m_UpdateMode: 1
m_MaxEventBytesPerUpdate: 5242880
m_MaxQueuedEventsPerUpdate: 1000
m_CompensateForScreenOrientation: 1
m_BackgroundBehavior: 0
m_EditorInputBehaviorInPlayMode: 0
m_DefaultDeadzoneMin: 0.125
m_DefaultDeadzoneMax: 0.925
m_DefaultButtonPressPoint: 0.5
m_ButtonReleaseThreshold: 0.75
m_DefaultTapTime: 0.2
m_DefaultSlowTapTime: 0.5
m_DefaultHoldTime: 0.4
m_TapRadius: 5
m_MultiTapDelayTime: 0.75
m_DisableRedundantEventsMerging: 0
m_ShortcutKeysConsumeInputs: 0
m_iOSSettings:
m_MotionUsage:
m_Enabled: 0
m_Description:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: de04e34f85a966e4da851d9b3d804fa1
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2072,7 +2072,7 @@ MonoBehaviour:
end: {fileID: 1259212308}
subDivision: 50
collisionCheckDist: 0.1
distBetweenRopePoints: 0.3
distBetweenRopePoints: 0.35
ropeRadius: 0.171
ignoreResolveThreshold: 0
ropeCollidersParent: {fileID: 1647138193}
@ -2081,9 +2081,10 @@ MonoBehaviour:
m_Bits: 1
pullForce: 65
xyGravityDampScalor: 1
ropeExtendSpeed: 15
ropeExtendSpeed: 10
ropeShrinkSpeed: 15
ropeMaxLength: 0
ropeMaxLength: 50
ropeMinLength: 20
lineRenderer: {fileID: 1647138191}
--- !u!4 &1647138193
Transform:

View File

@ -19,7 +19,7 @@ public class PlayerInput : MonoBehaviour
public int PlayerNum => playerNumber;
private void Awake()
private void Start()
{
controller = Gamepad.all.ElementAtOrDefault(playerNumber);
if (controller == null)

View File

@ -17,6 +17,7 @@ public class Rope
float sum = 0f;
foreach (Stick stick in sticks)
{
Debug.DrawRay(stick.B.position, (stick.A.position - stick.B.position).normalized * stick.desiredLength);
float dist = Vector3.Distance(stick.A.position, stick.B.position);
sum += dist - stick.desiredLength;
}

View File

@ -94,48 +94,57 @@ public class RopeSimulator : MonoBehaviour
if (subDivision < ropeMinLength)
subDivision = ropeMinLength;
if (prevSubDivision - (int)subDivision > 0) return;
if (prevSubDivision - (int)subDivision <= 0) return;
// Shrink from start
if (playerNumber == start.playerInput.PlayerNum)
rope.sticks.Clear();
rope.points.RemoveAt(1);
var builder = new RopeBuilder(rope.points, rope.sticks);
// Re-gen sticks
for (int i = 0; i < (int) subDivision; i++)
{
rope.sticks.Clear();
rope.points.RemoveAt(0);
var builder = new RopeBuilder(rope.points, new List<Stick>());
// Re-gen sticks
for (int i = 0; i < (int) subDivision; i++)
{
builder.ConnectPoints(i, i + 1);
}
rope = builder.Build();
builder.ConnectPointsWithDesiredLength(i, i + 1, distBetweenRopePoints);
}
// Shrink from end
else if (playerNumber == end.playerInput.PlayerNum)
{
rope.points.RemoveAt(rope.points.Count - 2);
rope.sticks.Clear();
rope = builder.Build();
var builder = new RopeBuilder(rope.points, new List<Stick>());
// Re-gen sticks
for (int i = 0; i < (int)subDivision; i++)
{
builder.ConnectPoints(i, i + 1);
}
rope = builder.Build();
}
RebuildRopeColliders();
CreateOrderArray();
}
void ExtendRope(int playerNumber)
{
int prevSubDivision = (int)subDivision;
subDivision += ropeExtendSpeed * Time.deltaTime;
if (subDivision > ropeMaxLength)
subDivision = ropeMaxLength;
Rebuild();
if (prevSubDivision - (int) subDivision >= 0) return;
// Extend from start
rope.points.Insert(1, new Point(rope.points[1].position));
rope.sticks.Clear();
// Ripple existing rope points
//for (int i = 2; i < (int) subDivision; i++)
//{
// rope.points[i].position = rope.points[i + 1].position;
//}
var builder = new RopeBuilder(rope.points, rope.sticks);
// Re-gen sticks
for (int i = 0; i < (int)subDivision; i++)
{
//Debug.Log($"Reg-gen stick. from: {i} to {i + 1}, with dist: {distBetweenRopePoints}");
builder.ConnectPointsWithDesiredLength(i, i + 1, distBetweenRopePoints);
}
rope = builder.Build();
RebuildRopeColliders();
CreateOrderArray();
}
private void OnDestroy()
@ -150,7 +159,6 @@ public class RopeSimulator : MonoBehaviour
private void Rebuild()
{
Debug.Log("rebuild");
ropeCollidersParent.DestroyChildren();
RopeBuilder builder = new RopeBuilder();
builder.AddPoint(new Point(start.position, locked: true));
@ -171,6 +179,14 @@ public class RopeSimulator : MonoBehaviour
}
rope = builder.Build();
RebuildRopeColliders();
CreateOrderArray();
}
private void RebuildRopeColliders()
{
ropeCollidersParent.DestroyChildren();
foreach (var point in rope.points)
{
GameObject ropeCollider = new GameObject("Rope Collider");
@ -184,12 +200,13 @@ public class RopeSimulator : MonoBehaviour
var rigidBody = ropeCollider.AddComponent<Rigidbody2D>();
rigidBody.isKinematic = true;
}
CreateOrderArray();
}
private void Update()
{
ShrinkenRope(1);
//Debug.Log($"overshoot: {rope.CalculateLengthOvershoot()}");
//ShrinkenRope(1);
//ExtendRope(0);
colliderToSquezeForce.Clear();
rope.points.First().position = start.position;
@ -300,7 +317,7 @@ public class RopeSimulator : MonoBehaviour
Vector3 stickCentre = (stick.A.position + stick.B.position) / 2;
Vector3 stickDir = (stick.A.position - stick.B.position).normalized;
float length = (stick.A.position - stick.B.position).magnitude;
float length = Vector2.Distance(stick.A.position, stick.B.position);
if (length > stick.desiredLength || constrainStickMinLength)
{