Skip to content

Commit

Permalink
Dev (#32)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
notgiven688 authored Oct 17, 2023
1 parent 06be001 commit 2e51d84
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ sidebar_position: 4

# Changelog

<!-- ### Jitter 2.0.0-beta
### Jitter 2.0.0-beta (10-17-2023)

- Added softbodies. -->
- Added softbodies

### Jitter 2.0.0-alpha (09-18-2023)

Initial Release.
Initial Release.
4 changes: 2 additions & 2 deletions src/Jitter2/Collision/CollisionIsland.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace Jitter2.Collision;
public sealed class Island : IListIndex
{
internal readonly HashSet<RigidBody> bodies = new();
internal bool markedAsActive;
internal bool needsUpdate;
internal bool MarkedAsActive;
internal bool NeedsUpdate;

/// <summary>
/// Gets a collection of all the bodies present in this island.
Expand Down
1 change: 0 additions & 1 deletion src/Jitter2/Collision/Shapes/TransformedShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/Jitter2/DataStructures/ActiveList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ public bool IsActive(T element)
return list.IsActive(element);
}

public IEnumerator<T> GetEnumerator()
public ActiveList<T>.Enumerator GetEnumerator()
{
return new ActiveList<T>.Enumerator(list);
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
Expand Down Expand Up @@ -187,11 +192,16 @@ public void Remove(T element)
element.ListIndex = -1;
}

public IEnumerator<T> GetEnumerator()
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
Expand Down
13 changes: 0 additions & 13 deletions src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
28 changes: 14 additions & 14 deletions src/Jitter2/SoftBodies/SoftBodyTetrahedron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -81,15 +81,15 @@ 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;
closest = i;
}
}

return bodies[closest];
return Bodies[closest];
}

public SoftBody SoftBody { get; }
Expand All @@ -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;
Expand All @@ -122,14 +122,14 @@ 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;
furthest = i;
}
}

result = bodies[furthest].Position;
result = Bodies[furthest].Position;
}
}
10 changes: 5 additions & 5 deletions src/Jitter2/World.Step.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Jitter2/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2e51d84

Please sign in to comment.