From 2e51d849321bf015a4f564e98b2ced249c1d4259 Mon Sep 17 00:00:00 2001 From: notgiven688 <37874600+notgiven688@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:27:13 +0200 Subject: [PATCH] Dev (#32) * Make Bodies in SoftBodyTetrahedron.cs public. * Make internal variables in CollisionIsland.cs uppercase. * Remove Filter2 in DynamicTreeCollisionFilter.cs * Attaching the rigid body to the original shape not necessary in TransformedShape.cs * Added soft bodies to feature list in README.md * Use correct pattern for GC-free IEnumerable implementation * Update changelog.md --- README.md | 1 + docs/docs/changelog.md | 6 ++-- src/Jitter2/Collision/CollisionIsland.cs | 4 +-- .../Collision/Shapes/TransformedShape.cs | 1 - src/Jitter2/DataStructures/ActiveList.cs | 14 ++++++++-- .../SoftBodies/DynamicTreeCollisionFilter.cs | 13 --------- src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs | 28 +++++++++---------- src/Jitter2/World.Step.cs | 10 +++---- src/Jitter2/World.cs | 4 +-- 9 files changed, 39 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 03167cf6..055c6c5f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ JitterDemo uses [GLFW](https://www.glfw.org/) for accessing OpenGL and managing - [x] "One-shot" contact manifolds using auxiliary contacts for flat surface collisions. - [x] Efficient compound shapes. - [x] Easy integration of custom shapes. Integrated: Box, Capsule, Cone, Convex Hull, Point Cloud, Sphere, Triangle, Transformed. +- [x] Soft-body dynamics! ## Documentation diff --git a/docs/docs/changelog.md b/docs/docs/changelog.md index 4fca9068..74d4206a 100644 --- a/docs/docs/changelog.md +++ b/docs/docs/changelog.md @@ -4,10 +4,10 @@ sidebar_position: 4 # Changelog - +- Added softbodies ### Jitter 2.0.0-alpha (09-18-2023) -Initial Release. \ No newline at end of file +Initial Release. diff --git a/src/Jitter2/Collision/CollisionIsland.cs b/src/Jitter2/Collision/CollisionIsland.cs index 4987a4db..7e5f118d 100644 --- a/src/Jitter2/Collision/CollisionIsland.cs +++ b/src/Jitter2/Collision/CollisionIsland.cs @@ -33,8 +33,8 @@ namespace Jitter2.Collision; public sealed class Island : IListIndex { internal readonly HashSet bodies = new(); - internal bool markedAsActive; - internal bool needsUpdate; + internal bool MarkedAsActive; + internal bool NeedsUpdate; /// /// Gets a collection of all the bodies present in this island. diff --git a/src/Jitter2/Collision/Shapes/TransformedShape.cs b/src/Jitter2/Collision/Shapes/TransformedShape.cs index 26530b85..ba5f6cbc 100644 --- a/src/Jitter2/Collision/Shapes/TransformedShape.cs +++ b/src/Jitter2/Collision/Shapes/TransformedShape.cs @@ -132,7 +132,6 @@ public override void CalculateBoundingBox(in JMatrix orientation, in JVector pos public override void CalculateMassInertia(out JMatrix inertia, out JVector com, out float mass) { - OriginalShape.AttachRigidBody(RigidBody); OriginalShape.CalculateMassInertia(out inertia, out com, out mass); com = JVector.Transform(com, transformation) + translation; diff --git a/src/Jitter2/DataStructures/ActiveList.cs b/src/Jitter2/DataStructures/ActiveList.cs index 61020eb3..c73505d9 100644 --- a/src/Jitter2/DataStructures/ActiveList.cs +++ b/src/Jitter2/DataStructures/ActiveList.cs @@ -52,11 +52,16 @@ public bool IsActive(T element) return list.IsActive(element); } - public IEnumerator GetEnumerator() + public ActiveList.Enumerator GetEnumerator() { return new ActiveList.Enumerator(list); } + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); @@ -187,11 +192,16 @@ public void Remove(T element) element.ListIndex = -1; } - public IEnumerator GetEnumerator() + public Enumerator GetEnumerator() { return new Enumerator(this); } + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); diff --git a/src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs b/src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs index 2ab8090b..d72745f1 100644 --- a/src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs +++ b/src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs @@ -41,17 +41,4 @@ public static bool Filter(Shape shapeA, Shape shapeB) return false; } - - public static bool Filter2(Shape shapeA, Shape shapeB) - { - if (shapeA.RigidBody != shapeB.RigidBody) return true; - - if (shapeA is ISoftBodyShape softBodyShapeA && - shapeB is ISoftBodyShape softBodyShapeB) - { - return true; - } - - return false; - } } \ No newline at end of file diff --git a/src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs b/src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs index 5125340c..1df9ad1a 100644 --- a/src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs +++ b/src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs @@ -29,20 +29,20 @@ namespace Jitter2.SoftBodies; public class SoftBodyTetrahedron : Shape, ISoftBodyShape { - private readonly RigidBody[] bodies = new RigidBody[4]; - public SoftBodyTetrahedron(SoftBody body, RigidBody p1, RigidBody p2, RigidBody p3, RigidBody p4) { - bodies[0] = p1; - bodies[1] = p2; - bodies[2] = p3; - bodies[3] = p4; + Bodies[0] = p1; + Bodies[1] = p2; + Bodies[2] = p3; + Bodies[3] = p4; SoftBody = body; UpdateShape(); } + public RigidBody[] Bodies { get; } = new RigidBody[4]; + public override JVector Velocity { get @@ -51,7 +51,7 @@ public override JVector Velocity for (int i = 0; i < 4; i++) { - vel += bodies[i].Velocity; + vel += Bodies[i].Velocity; } vel *= 0.25f; @@ -68,7 +68,7 @@ public override void CalculateMassInertia(out JMatrix inertia, out JVector com, for (int i = 0; i < 4; i++) { - com += bodies[i].Position; + com += Bodies[i].Position; } com *= 0.25f; @@ -81,7 +81,7 @@ public RigidBody GetClosest(in JVector pos) for (int i = 0; i < 4; i++) { - float len = (pos - bodies[i].Position).LengthSquared(); + float len = (pos - Bodies[i].Position).LengthSquared(); if (len < dist) { dist = len; @@ -89,7 +89,7 @@ public RigidBody GetClosest(in JVector pos) } } - return bodies[closest]; + return Bodies[closest]; } public SoftBody SoftBody { get; } @@ -103,8 +103,8 @@ public override void UpdateWorldBoundingBox() for (int i = 0; i < 4; i++) { - box.AddPoint(bodies[i].Position); - GeometricCenter += bodies[i].Position; + box.AddPoint(Bodies[i].Position); + GeometricCenter += Bodies[i].Position; } GeometricCenter *= 0.25f; @@ -122,7 +122,7 @@ public override void SupportMap(in JVector direction, out JVector result) for (int i = 0; i < 4; i++) { - float dot = JVector.Dot(direction, bodies[i].Position); + float dot = JVector.Dot(direction, Bodies[i].Position); if (dot > maxDot) { maxDot = dot; @@ -130,6 +130,6 @@ public override void SupportMap(in JVector direction, out JVector result) } } - result = bodies[furthest].Position; + result = Bodies[furthest].Position; } } \ No newline at end of file diff --git a/src/Jitter2/World.Step.cs b/src/Jitter2/World.Step.cs index ad406cba..9f7d533b 100644 --- a/src/Jitter2/World.Step.cs +++ b/src/Jitter2/World.Step.cs @@ -264,7 +264,7 @@ private void UpdateBodiesCallback(Parallel.Batch batch) if (body.sleepTime < body.deactivationTimeThreshold) { - body.island.markedAsActive = true; + body.island.MarkedAsActive = true; } if (!rigidBody.IsStatic && rigidBody.IsActive) @@ -760,17 +760,17 @@ private void CheckDeactivation() { Island island = islands[i]; - bool deactivateIsland = !island.markedAsActive; + bool deactivateIsland = !island.MarkedAsActive; if (!AllowDeactivation) deactivateIsland = false; // Mark the island as inactive // Next frame one active body will be enough to set // MarkedAsActive back to true; - island.markedAsActive = false; + island.MarkedAsActive = false; - if (!deactivateIsland && !island.needsUpdate) continue; + if (!deactivateIsland && !island.NeedsUpdate) continue; - island.needsUpdate = false; + island.NeedsUpdate = false; foreach (RigidBody body in island.bodies) { diff --git a/src/Jitter2/World.cs b/src/Jitter2/World.cs index 8d8189fe..1dd2782b 100644 --- a/src/Jitter2/World.cs +++ b/src/Jitter2/World.cs @@ -439,8 +439,8 @@ internal void DeactivateBodyNextStep(RigidBody body) private void AddToActiveList(Island island) { - island.markedAsActive = true; - island.needsUpdate = true; + island.MarkedAsActive = true; + island.NeedsUpdate = true; islands.MoveToActive(island); }