Skip to content

Commit

Permalink
samples: add 3 more soft-body tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 10, 2025
1 parent 9cad9ec commit f42e122
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/test/java/testjoltjni/app/samples/SmokeTestAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ private static void smokeTestSoftBody() {
smokeTest(new SoftBodyContactListenerTest());
smokeTest(new SoftBodyCustomUpdateTest());
smokeTest(new SoftBodyForceTest());
smokeTest(new SoftBodyFrictionTest());
smokeTest(new SoftBodyGravityFactorTest());
smokeTest(new SoftBodyLRAConstraintTest());
smokeTest(new SoftBodyPressureTest());
smokeTest(new SoftBodyUpdatePositionTest());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 com.github.stephengold.joltjni.enumerate.*;
import testjoltjni.app.samples.*;
/**
* A line-for-line Java translation of the Jolt Physics soft-body friction test.
* <p>
* Compare with the original by Jorrit Rouwe at
* https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyFrictionTest.cpp
*/
public class SoftBodyFrictionTest extends Test{

public void Initialize()
{
// Floor
Body floor = CreateFloor();
floor.setFriction(1.0f);

// Bodies with increasing friction
SoftBodySharedSettingsRef sphere_settings = SoftBodyCreator.CreateSphere();
for (Vertex v : sphere_settings.getPtr().getVertices())
v.setVelocity (new Float3(0, 0, 10));
SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(sphere_settings, RVec3.sZero(), Quat.sIdentity(), Layers.MOVING);
sphere.setPressure ( 2000.0f);

for (int i = 0; i <= 10; ++i)
{
sphere.setPosition (new RVec3(-50.0f + i * 10.0f, 1.0f, 0));
sphere.setFriction ( 0.1f * i);
mBodyInterface.createAndAddSoftBody(sphere, EActivation.Activate);
}

SoftBodySharedSettingsRef cube_settings = SoftBodySharedSettings.sCreateCube(5, 0.5f);
for (Vertex v : cube_settings.getPtr().getVertices())
v.setVelocity (new Float3(0, 0, 10));
SoftBodyCreationSettings cube=new SoftBodyCreationSettings(cube_settings, RVec3.sZero(), Quat.sIdentity(), Layers.MOVING);

for (int i = 0; i <= 10; ++i)
{
cube.setPosition (new RVec3(-50.0f + i * 10.0f, 1.0f, -5.0f));
cube.setFriction ( 0.1f * i);
mBodyInterface.createAndAddSoftBody(cube, EActivation.Activate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 testjoltjni.app.samples.*;
/**
* A line-for-line Java translation of the Jolt Physics soft-body gravity-factor test.
* <p>
* Compare with the original by Jorrit Rouwe at
* https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyGravityFactorTest.cpp
*/
public class SoftBodyGravityFactorTest extends Test{

public void Initialize()
{
// Floor
CreateFloor();

// Bodies with increasing gravity factor
SoftBodyCreationSettings sphere=new SoftBodyCreationSettings(SoftBodyCreator.CreateSphere(), RVec3.sZero(), Quat.sIdentity(), Layers.MOVING);
sphere.setPressure ( 2000.0f);

for (int i = 0; i <= 10; ++i)
{
sphere.setPosition (new RVec3(-50.0f + i * 10.0f, 10.0f, 0));
sphere.setGravityFactor ( 0.1f * i);
mBodyInterface.createAndAddSoftBody(sphere, EActivation.Activate);
}

SoftBodyCreationSettings cube=new SoftBodyCreationSettings(SoftBodySharedSettings.sCreateCube(5, 0.5f), RVec3.sZero(), Quat.sIdentity(), Layers.MOVING);

for (int i = 0; i <= 10; ++i)
{
cube.setPosition (new RVec3(-50.0f + i * 10.0f, 10.0f, -5.0f));
cube.setGravityFactor ( 0.1f * i);
mBodyInterface.createAndAddSoftBody(cube, EActivation.Activate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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.*;
/**
* A line-for-line Java translation of the Jolt Physics soft-body LRA-constraint test.
* <p>
* Compare with the original by Jorrit Rouwe at
* https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/SoftBody/SoftBodyLRAConstraintTest.cpp
*/
public class SoftBodyLRAConstraintTest extends Test{
float cVertexSpacing=.5f;
int cNumVerticesX=10,cNumVerticesZ = 50;

public void Initialize()
{
CreateFloor();

for (int i = 0; i < 2; ++i)
{
BiFunction<Integer,Integer,Float> inv_mass = (Integer inX, Integer inZ)-> { return inZ < 2? 0.0f : 1.0f; };
BiFunction<Integer,Integer,Vec3> perturbation = (Integer inX, Integer inZ)-> { return Vec3.sZero(); };

VertexAttributes va=new VertexAttributes();
va.setShearCompliance ( va.setCompliance ( 1.0e-3f)); // Soften the edges a bit so that the effect of the LRA constraints is more visible
va.setLraType ( i == 0? ELraType.None : ELraType.EuclideanDistance);

SoftBodySharedSettingsRef cloth_settings = SoftBodyCreator.CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, EBendType.None, va);

SoftBodyCreationSettings cloth=new SoftBodyCreationSettings(cloth_settings,new RVec3(-10.0f + i * 20.0f, 25.0f, 0), Quat.sIdentity(), Layers.MOVING);
mBodyInterface.createAndAddSoftBody(cloth, EActivation.Activate);
}
}

public void PrePhysicsUpdate(PreUpdateParams inParams)
{
mDebugRenderer.drawText3D(new RVec3(-10, 26, -0.5f * cNumVerticesZ * cVertexSpacing), "Without LRA constraints", Color.sWhite);
mDebugRenderer.drawText3D(new RVec3(10, 26, -0.5f * cNumVerticesZ * cVertexSpacing), "With LRA constraints", Color.sWhite);
}
}

0 comments on commit f42e122

Please sign in to comment.