From 22dbcf6ad3b10c47c548e3b2f64cdde61bb3deaa Mon Sep 17 00:00:00 2001 From: stephengold Date: Fri, 7 Feb 2025 23:04:51 -0800 Subject: [PATCH] samples: add 4 soft-body tests --- .../testjoltjni/app/samples/SmokeTestAll.java | 4 + .../softbody/SoftBodyBendConstraintTest.java | 100 ++++++++++ .../softbody/SoftBodyContactListenerTest.java | 174 ++++++++++++++++++ .../softbody/SoftBodyCustomUpdateTest.java | 66 +++++++ .../samples/softbody/SoftBodyForceTest.java | 79 ++++++++ 5 files changed, 423 insertions(+) create mode 100644 src/test/java/testjoltjni/app/samples/softbody/SoftBodyBendConstraintTest.java create mode 100644 src/test/java/testjoltjni/app/samples/softbody/SoftBodyContactListenerTest.java create mode 100644 src/test/java/testjoltjni/app/samples/softbody/SoftBodyCustomUpdateTest.java create mode 100644 src/test/java/testjoltjni/app/samples/softbody/SoftBodyForceTest.java diff --git a/src/test/java/testjoltjni/app/samples/SmokeTestAll.java b/src/test/java/testjoltjni/app/samples/SmokeTestAll.java index ac0987b3..ac7482e7 100644 --- a/src/test/java/testjoltjni/app/samples/SmokeTestAll.java +++ b/src/test/java/testjoltjni/app/samples/SmokeTestAll.java @@ -310,6 +310,10 @@ private static void smokeTestShapes() { * Smoke test the "softbody" package. */ private static void smokeTestSoftBody() { + smokeTest(new SoftBodyBendConstraintTest()); + smokeTest(new SoftBodyContactListenerTest()); + smokeTest(new SoftBodyCustomUpdateTest()); + smokeTest(new SoftBodyForceTest()); smokeTest(new SoftBodyPressureTest()); smokeTest(new SoftBodyUpdatePositionTest()); } diff --git a/src/test/java/testjoltjni/app/samples/softbody/SoftBodyBendConstraintTest.java b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyBendConstraintTest.java new file mode 100644 index 00000000..7e9a80f0 --- /dev/null +++ b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyBendConstraintTest.java @@ -0,0 +1,100 @@ +/* +Copyright (c) 2025 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package testjoltjni.app.samples.softbody; +import com.github.stephengold.joltjni.*; +import com.github.stephengold.joltjni.enumerate.*; +import com.github.stephengold.joltjni.std.*; +import java.util.function.BiFunction; +import testjoltjni.app.samples.*; +/** + * A line-for-line Java translation of the Jolt Physics soft-body bend-constraint test. + *

+ * Compare with the original by Jorrit Rouwe at + * https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyBendConstraintTest.cpp + */ +public class SoftBodyBendConstraintTest extends Test{ +float cVertexSpacing=0.5f; +int cNumVerticesX=10,cNumVerticesZ=10; + +public void Initialize() +{ + CreateFloor(); + + DefaultRandomEngine random=new DefaultRandomEngine(); + UniformFloatDistribution random_float=new UniformFloatDistribution(-0.1f, 0.1f); + + BiFunction inv_mass = (Integer inX, Integer inZ)-> { return inZ < 2? 0.0f : 1.0f; }; + BiFunction perturbation = (Integer inX, Integer inZ)-> { return new Vec3(random_float.nextFloat(random), (inZ & 1)!=0? 0.1f : -0.1f, random_float.nextFloat(random)); }; + + { + random.seed(1234); + + // Cloth without bend constraints + SoftBodySharedSettingsRef cloth_settings = SoftBodyCreator.CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, EBendType.None); + SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(cloth_settings,new RVec3(-5.0f, 5.0f, 0), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate); + } + + { + random.seed(1234); + + // Cloth with distance bend constraints + SoftBodySharedSettingsRef cloth_settings = SoftBodyCreator.CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, EBendType.Distance); + SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(cloth_settings,new RVec3(0.0f, 5.0f, 0), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate); + } + + { + random.seed(1234); + + // Cloth with dihedral bend constraints + SoftBodySharedSettingsRef cloth_settings = SoftBodyCreator.CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, EBendType.Dihedral); + SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(cloth_settings,new RVec3(5.0f, 5.0f, 0), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate); + } + + { + // Create sphere + SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(SoftBodyCreator.CreateSphere(1.0f, 10, 20, EBendType.None),new RVec3(-5.0f, 5.0f, 10.0f), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(sphere, EActivation.Activate); + } + + { + // Create sphere with distance bend constraints + SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(SoftBodyCreator.CreateSphere(1.0f, 10, 20, EBendType.Distance),new RVec3(0.0f, 5.0f, 10.0f), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(sphere, EActivation.Activate); + } + + { + // Create sphere with dihedral bend constraints + SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(SoftBodyCreator.CreateSphere(1.0f, 10, 20, EBendType.Dihedral),new RVec3(5.0f, 5.0f, 10.0f), Quat.sIdentity(), Layers.MOVING); + mBodyInterface.createAndAddSoftBody(sphere, EActivation.Activate); + } +} + +public void PrePhysicsUpdate( PreUpdateParams inParams) +{ + mDebugRenderer.drawText3D(new RVec3(-5.0f, 7.5f, 0), "No bend constraints", Color.sWhite); + mDebugRenderer.drawText3D(new RVec3(0.0f, 7.5f, 0), "Distance bend constraints", Color.sWhite); + mDebugRenderer.drawText3D(new RVec3(5.0f, 7.5f, 0), "Dihedral angle bend constraints", Color.sWhite); +} +} diff --git a/src/test/java/testjoltjni/app/samples/softbody/SoftBodyContactListenerTest.java b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyContactListenerTest.java new file mode 100644 index 00000000..41280954 --- /dev/null +++ b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyContactListenerTest.java @@ -0,0 +1,174 @@ +/* +Copyright (c) 2025 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package testjoltjni.app.samples.softbody; +import com.github.stephengold.joltjni.*; +import com.github.stephengold.joltjni.enumerate.*; +import com.github.stephengold.joltjni.readonly.ConstBody; +import testjoltjni.app.samples.*; +import static com.github.stephengold.joltjni.Jolt.*; +import static com.github.stephengold.joltjni.operator.Op.*; +/** + * A line-for-line Java translation of the Jolt Physics soft-body contact-listener test. + *

+ * Compare with the original by Jorrit Rouwe at + * https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyContactListenerTest.cpp + */ +public class SoftBodyContactListenerTest extends Test{ +BodyId mOtherBodyID=new BodyId(),mSoftBodyID=new BodyId(); +float mTime; +int mCycle; + +public void Initialize() +{ + // Install contact listener for soft bodies + mPhysicsSystem.setSoftBodyContactListener(new CustomSoftBodyContactListener(){ + public void onSoftBodyContactAdded(long bodyVa,long manifoldVa){OnSoftBodyContactAdded(new Body(bodyVa),new SoftBodyManifold(manifoldVa));} + public int onSoftBodyContactValidate(long softBodyVa,long otherBodyVa,long settingsVa){return OnSoftBodyContactValidate(new Body(softBodyVa),new Body(otherBodyVa),new SoftBodyContactSettings(settingsVa)).ordinal();} + }); + + // Floor + CreateFloor(); + + // Start the 1st cycle + StartCycle(); +} + +public void PrePhysicsUpdate( PreUpdateParams inParams) +{ + mTime += inParams.mDeltaTime; + if (mTime > 2.5f) + { + // Next cycle + mCycle = (mCycle + 1) % 10; + mTime = 0.0f; + + // Remove the old scene + mBodyInterface.removeBody(mOtherBodyID); + mBodyInterface.destroyBody(mOtherBodyID); + mBodyInterface.removeBody(mSoftBodyID); + mBodyInterface.destroyBody(mSoftBodyID); + + // Start the new + StartCycle(); + } + + // Draw current state + String cycle_names[] = { "Accept contact", "Sphere 10x mass", "Cloth 10x mass", "Sphere infinite mass", "Cloth infinite mass", "Sensor contact", "Reject contact", "Kinematic Sphere", "Kinematic Sphere, cloth infinite mass", "Kinematic sphere, sensor contact", "Kinematic Sphere, reject contact" }; + mDebugRenderer.drawText3D(mBodyInterface.getPosition(mOtherBodyID), cycle_names[mCycle], Color.sWhite, 1.0f); +} + +void StartCycle() +{ + // Create the cloth + SoftBodySharedSettingsRef cloth_settings = SoftBodyCreator.CreateClothWithFixatedCorners(15, 15, 0.75f); + + // Create cloth that's fixated at the corners + SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(cloth_settings,new RVec3(0, 5, 0), Quat.sRotation(Vec3.sAxisY(), 0.25f * JPH_PI), Layers.MOVING); + cloth.setUpdatePosition ( false); // Don't update the position of the cloth as it is fixed to the world + cloth.setMakeRotationIdentity ( false); // Test explicitly checks if soft bodies with a rotation collide with shapes properly + mSoftBodyID = mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate); + + // If we want a kinematic sphere + boolean kinematic = mCycle > 6; + + // Create sphere + BodyCreationSettings bcs=new BodyCreationSettings(new SphereShape(1.0f),new RVec3(0, 7, 0), Quat.sIdentity(), kinematic? EMotionType.Kinematic : EMotionType.Dynamic, Layers.MOVING); + bcs.setOverrideMassProperties ( EOverrideMassProperties.CalculateInertia); + bcs.getMassPropertiesOverride().setMass ( 100.0f); + if (kinematic) + bcs.setLinearVelocity (new Vec3(0, -2.5f, 0)); + mOtherBodyID = mBodyInterface.createAndAddBody(bcs, EActivation.Activate); +} + +SoftBodyValidateResult OnSoftBodyContactValidate( ConstBody inSoftBody, ConstBody inOtherBody, SoftBodyContactSettings ioSettings) +{ + switch (mCycle) + { + case 0: + // Normal + return SoftBodyValidateResult.AcceptContact; + + case 1: + // Makes the sphere 10x as heavy + ioSettings.setInvMassScale2 ( 0.1f); + ioSettings.setInvInertiaScale2 ( 0.1f); + return SoftBodyValidateResult.AcceptContact; + + case 2: + // Makes the cloth 10x as heavy + ioSettings.setInvMassScale1 ( 0.1f); + return SoftBodyValidateResult.AcceptContact; + + case 3: + // Makes the sphere have infinite mass + ioSettings.setInvMassScale2 ( 0.0f); + ioSettings.setInvInertiaScale2 ( 0.0f); + return SoftBodyValidateResult.AcceptContact; + + case 4: + // Makes the cloth have infinite mass + ioSettings.setInvMassScale1 ( 0.0f); + return SoftBodyValidateResult.AcceptContact; + + case 5: + // Sensor contact + ioSettings.setIsSensor ( true); + return SoftBodyValidateResult.AcceptContact; + + case 6: + // No contacts + return SoftBodyValidateResult.RejectContact; + + case 7: + // Kinematic sphere + return SoftBodyValidateResult.AcceptContact; + + case 8: + // Kinematic sphere, cloth infinite mass + ioSettings.setInvMassScale1 ( 0.0f); + return SoftBodyValidateResult.AcceptContact; + + case 9: + // Kinematic sphere, sensor contact + ioSettings.setIsSensor ( true); + return SoftBodyValidateResult.AcceptContact; + + default: + // No contacts + return SoftBodyValidateResult.RejectContact; + } +} + +void OnSoftBodyContactAdded(ConstBody inSoftBody, SoftBodyManifold inManifold) +{ + // Draw contacts + RMat44 com = inSoftBody.getCenterOfMassTransform(); + for (SoftBodyVertex vertex : inManifold.getVertices()) + if (inManifold.hasContact(vertex)) + { + RVec3 position = star(com , inManifold.getLocalContactPoint(vertex)); + Vec3 normal = inManifold.getContactNormal(vertex); + mDebugRenderer.drawMarker(position, Color.sRed, 0.1f); + mDebugRenderer.drawArrow(position, plus(position , normal), Color.sGreen, 0.1f); + } +} +} diff --git a/src/test/java/testjoltjni/app/samples/softbody/SoftBodyCustomUpdateTest.java b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyCustomUpdateTest.java new file mode 100644 index 00000000..1fc22f5a --- /dev/null +++ b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyCustomUpdateTest.java @@ -0,0 +1,66 @@ +/* +Copyright (c) 2025 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package testjoltjni.app.samples.softbody; +import com.github.stephengold.joltjni.*; +import testjoltjni.app.samples.*; +import static com.github.stephengold.joltjni.operator.Op.*; +/** + * A line-for-line Java translation of the Jolt Physics soft-body custom-update test. + *

+ * Compare with the original by Jorrit Rouwe at + * https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyCustomUpdateTest.cpp + */ +public class SoftBodyCustomUpdateTest extends Test{ +Body mBody; + +public void Initialize() +{ + // Floor + CreateFloor(); + + // Create a body but do not add it to the physics system (we're updating it ourselves) + SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(SoftBodyCreator.CreateSphere(),new RVec3(0, 5, 0), Quat.sIdentity(), Layers.MOVING); + sphere.setPressure ( 2000.0f); + mBody = mBodyInterface.createSoftBody(sphere); +} + +public void PrePhysicsUpdate( PreUpdateParams inParams) +{ + // Note that passing a variable delta time results in differences in behavior, usually you want to have a fixed time step. + // For this demo we'll just clamp the delta time to 1/60th of a second and allow behavioral changes due to frame rate fluctuations. + float dt = Math.min(inParams.mDeltaTime, 1.0f / 60.0f); + + // Call the update now + SoftBodyMotionProperties mp = (SoftBodyMotionProperties )(mBody.getMotionProperties()); + mp.customUpdate(dt, mBody, mPhysicsSystem); + +if (Jolt.implementsDebugRendering()){ + // Draw it as well since it's not added to the world + mBody.getShape().draw(mDebugRenderer, mBody.getCenterOfMassTransform(), Vec3.sOne(), Color.sWhite, false, false); +}else{ + // Draw the vertices + RMat44 com = mBody.getCenterOfMassTransform(); + for ( SoftBodyVertex v : mp.getVertices()) + mDebugRenderer.drawMarker(star(com , v.getPosition()), Color.sRed, 0.1f); +} // JPH_DEBUG_RENDERER +} +} diff --git a/src/test/java/testjoltjni/app/samples/softbody/SoftBodyForceTest.java b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyForceTest.java new file mode 100644 index 00000000..d2366e5d --- /dev/null +++ b/src/test/java/testjoltjni/app/samples/softbody/SoftBodyForceTest.java @@ -0,0 +1,79 @@ +/* +Copyright (c) 2025 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package testjoltjni.app.samples.softbody; +import com.github.stephengold.joltjni.*; +import com.github.stephengold.joltjni.enumerate.*; +import java.util.function.BiFunction; +import testjoltjni.app.samples.*; +import static com.github.stephengold.joltjni.Jolt.*; +import static com.github.stephengold.joltjni.operator.Op.*; +/** + * A line-for-line Java translation of the Jolt Physics soft-body force test. + *

+ * Compare with the original by Jorrit Rouwe at + * https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyForceTest.cpp + */ +public class SoftBodyForceTest extends Test{ +BodyId mBodyID=new BodyId(); +float mTime; + +public void Initialize() +{ + CreateFloor(); + + final int cGridSize = 30; + + // Create hanging cloth + BiFunction inv_mass = (Integer inX, Integer inZ) ->{ + return (inX == 0 && inZ == 0) + || (inX == cGridSize - 1 && inZ == 0)? 0.0f : 1.0f; + }; + SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(SoftBodyCreator.CreateCloth(cGridSize, cGridSize, 0.75f, inv_mass),new RVec3(0, 15.0f, 0), Quat.sRotation(Vec3.sAxisX(), 0.5f * JPH_PI), Layers.MOVING); + mBodyID = mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate); +} + +public void PrePhysicsUpdate( PreUpdateParams inParams) +{ + mTime += inParams.mDeltaTime; + + // Apply a fluctuating force + final float cMaxForce = 10000.0f; + final float cMaxAngle = degreesToRadians(90.0f); + Vec3 force=new Vec3(0, 0, 0.5f * cMaxForce * (1.0f + perlinNoise3(0, 0, mTime / 2.0f, 256, 256, 256))); + force = star(Mat44.sRotationY(cMaxAngle * perlinNoise3(mTime / 10.0f, 0, 0, 256, 256, 256)) , force); + mBodyInterface.addForce(mBodyID, force); + + // Draw the force + RVec3 offset=new RVec3(0, 10, 0); + DebugRenderer.sInstance().drawArrow(offset, plus(offset ,star( 10.0f , force.normalized())), Color.sGreen, 0.1f); +} + +public void SaveState(StateRecorder inStream) +{ + inStream.write(mTime); +} + +public void RestoreState(StateRecorder inStream) +{ + mTime=inStream.readFloat(mTime); +} +}