-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0be2fbb
commit 22dbcf6
Showing
5 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/java/testjoltjni/app/samples/softbody/SoftBodyBendConstraintTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* 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<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 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); | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
src/test/java/testjoltjni/app/samples/softbody/SoftBodyContactListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* 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); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/testjoltjni/app/samples/softbody/SoftBodyCustomUpdateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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. | ||
* <p> | ||
* 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 | ||
} | ||
} |
Oops, something went wrong.