Skip to content

Commit

Permalink
SoftBodyMotionProperties: add a constructor and 5 public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 20, 2025
1 parent 949acd2 commit d2939e2
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public MotionProperties() {
setVirtualAddress(propertiesVa, () -> free(propertiesVa));
}

/**
* Instantiate properties with no containing object and no native object
* assigned.
*
* @param dummy unused argument to distinguish from the zero-arg constructor
*/
MotionProperties(boolean dummy) {
}

/**
* Instantiate with the specified container and native object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.readonly.Mat44Arg;
import com.github.stephengold.joltjni.readonly.RMat44Arg;

/**
* The motion properties of a soft body.
*
Expand All @@ -30,6 +33,15 @@ public class SoftBodyMotionProperties extends MotionProperties {
// *************************************************************************
// constructors

/**
* Instantiate with the default properties.
*/
public SoftBodyMotionProperties() {
super(false);
long propertiesVa = createDefault();
setVirtualAddress(propertiesVa, () -> free(propertiesVa));
}

/**
* Instantiate with the specified container and native object.
*
Expand Down Expand Up @@ -59,6 +71,18 @@ public void customUpdate(
customUpdate(propertiesVa, deltaTime, bodyVa, systemVa);
}

/**
* Test whether skinning constraints are enabled.
*
* @return {@code true} if enabled, {@code false} if disabled
*/
public boolean getEnableSkinConstraints() {
long propertiesVa = va();
boolean result = getEnableSkinConstraints(propertiesVa);

return result;
}

/**
* Access the specified face.
*
Expand Down Expand Up @@ -115,6 +139,19 @@ public SoftBodySharedSettings getSettings() {
return result;
}

/**
* Return the maximum distance multiplier for skinned vertices. The
* properties are unaffected.
*
* @return the multiplier
*/
public float getSkinnedMaxDistanceMultiplier() {
long propertiesVa = va();
float result = getSkinnedMaxDistanceMultiplier(propertiesVa);

return result;
}

/**
* Access the specified vertex.
*
Expand Down Expand Up @@ -147,6 +184,17 @@ public SoftBodyVertex[] getVertices() {
return result;
}

/**
* Enable or disable any skinning constraints.
*
* @param enable {@code true} to enable any skinning constraints,
* {@code false} to disable them (default=true)
*/
public void setEnableSkinConstraints(boolean enable) {
long propertiesVa = va();
setEnableSkinConstraints(propertiesVa, enable);
}

/**
* Alter the number of solver iterations.
*
Expand All @@ -156,24 +204,79 @@ public void setNumIterations(int numIterations) {
long propertiesVa = va();
setNumIterations(propertiesVa, numIterations);
}

/**
* Alter the maximum distance multiplier for skinned vertices.
*
* @param multiplier the desired multiplier (default=1)
*/
public void setSkinnedMaxDistanceMultiplier(float multiplier) {
long propertiesVa = va();
setSkinnedMaxDistanceMultiplier(propertiesVa, multiplier);
}

/**
* Skin vertices to the specified joints.
*
* @param comTransform the body's center-of-mass transform (not null,
* unaffected)
* @param jointMatrices the joint matrices (relative to the center-of-mass
* transform, not null, length≥numJoints, unaffected)
* @param numJoints the number of joints (≥0)
* @param hardSkinAll {@code true} to reposition all vertices to their
* skinned locations
* @param allocator for temporary allocations (not null)
*/
public void skinVertices(
RMat44Arg comTransform, Mat44Arg[] jointMatrices, int numJoints,
boolean hardSkinAll, TempAllocator allocator) {
long propertiesVa = va();
long comTransformVa = comTransform.targetVa();
long[] jointMatrixVas = new long[numJoints];
for (int i = 0; i < numJoints; ++i) {
jointMatrixVas[i] = jointMatrices[i].targetVa();
}
long allocatorVa = allocator.va();
skinVertices(propertiesVa, comTransformVa, jointMatrixVas,
hardSkinAll, allocatorVa);
}
// *************************************************************************
// native private methods

native private static int countFaces(long propertiesVa);

native private static int countVertices(long propertiesVa);

native private static long createDefault();

native private static void customUpdate(
long propertiesVa, float deltaTime, long bodyVa, long systemVa);

native private static void free(long propertiesVa);

native private static boolean getEnableSkinConstraints(long propertiesVa);

native private static long getFace(long propertiesVa, int index);

native private static int getNumIterations(long propertiesVa);

native private static long getSettings(long propertiesVa);

native private static float getSkinnedMaxDistanceMultiplier(
long propertiesVa);

native private static long getVertex(long propertiesVa, int index);

native private static void setEnableSkinConstraints(
long propertiesVa, boolean enable);

native private static void setNumIterations(
long propertiesVa, int numInterations);

native private static void setSkinnedMaxDistanceMultiplier(
long propertiesVa, float multiplier);

native private static void skinVertices(
long propertiesVa, long comTransformVa, long[] jointMatrixVas,
boolean hardSkinAll, long allocatorVa);
}
109 changes: 109 additions & 0 deletions src/main/native/glue/s/SoftBodyMotionProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOFTWARE.
#include "Jolt/Jolt.h"
#include "Jolt/Physics/SoftBody/SoftBodyMotionProperties.h"
#include "auto/com_github_stephengold_joltjni_SoftBodyMotionProperties.h"
#include "glue/glue.h"

using namespace JPH;

Expand Down Expand Up @@ -57,6 +58,19 @@ JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionPropert
return result;
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: createDefault
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_createDefault
(JNIEnv *, jclass) {
SoftBodyMotionProperties * const pResult
= new SoftBodyMotionProperties();
TRACE_NEW("SoftBodyMotionProperties", pResult)
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: customUpdate
Expand All @@ -71,6 +85,32 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionPropert
pProperties->CustomUpdate(deltaTime, *pBody, *pSystem);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: free
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_free
(JNIEnv *, jclass, jlong propertiesVa) {
SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
TRACE_DELETE("SoftBodyMotionProperties", pProperties);
delete pProperties;
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: getEnableSkinConstraints
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_getEnableSkinConstraints
(JNIEnv *, jclass, jlong propertiesVa) {
const SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
const bool result = pProperties->GetEnableSkinConstraints();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: getFace
Expand Down Expand Up @@ -110,6 +150,19 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProper
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: getSkinnedMaxDistanceMultiplier
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_getSkinnedMaxDistanceMultiplier
(JNIEnv *, jclass, jlong propertiesVa) {
const SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
const float result = pProperties->GetSkinnedMaxDistanceMultiplier();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: getVertex
Expand All @@ -123,6 +176,18 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProper
return reinterpret_cast<jlong> (&result);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: setEnableSkinConstraints
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_setEnableSkinConstraints
(JNIEnv *, jclass, jlong propertiesVa, jboolean enable) {
SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
pProperties->SetEnableSkinConstraints(enable);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: setNumIterations
Expand All @@ -133,4 +198,48 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionPropert
SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
pProperties->SetNumIterations(numIterations);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: setSkinnedMaxDistanceMultiplier
* Signature: (JF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_setSkinnedMaxDistanceMultiplier
(JNIEnv *, jclass, jlong propertiesVa, jfloat multiplier) {
SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
pProperties->SetSkinnedMaxDistanceMultiplier(multiplier);
}

/*
* Class: com_github_stephengold_joltjni_SoftBodyMotionProperties
* Method: skinVertices
* Signature: (JJ[JZJ)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SoftBodyMotionProperties_skinVertices
(JNIEnv *pEnv, jclass, jlong propertiesVa, jlong comTransformVa,
jlongArray jointMatrixVas, jboolean hardSkinAll, jlong allocatorVa) {
const jsize numJoints = pEnv->GetArrayLength(jointMatrixVas);
Mat44 * const pTempArray = new Mat44[numJoints];
TRACE_NEW("Mat44[]", pTempArray)
jboolean isCopy;
jlong * const pMatrixVas
= pEnv->GetLongArrayElements(jointMatrixVas, &isCopy);
for (jsize i = 0; i < numJoints; ++i) {
const jlong matrixVa = pMatrixVas[i];
Mat44 * const pMatrix = reinterpret_cast<Mat44 *> (matrixVa);
pTempArray[i] = *pMatrix;
}
pEnv->ReleaseLongArrayElements(jointMatrixVas, pMatrixVas, JNI_ABORT);
SoftBodyMotionProperties * const pProperties
= reinterpret_cast<SoftBodyMotionProperties *> (propertiesVa);
const RMat44 * const pComTransform
= reinterpret_cast<RMat44 *> (comTransformVa);
TempAllocator * const pAllocator
= reinterpret_cast<TempAllocator *> (allocatorVa);
pProperties->SkinVertices(*pComTransform, pTempArray, numJoints,
hardSkinAll, *pAllocator);
TRACE_DELETE("Mat44[]", pTempArray)
delete[] pTempArray;
}
Loading

0 comments on commit d2939e2

Please sign in to comment.