Skip to content

Commit

Permalink
add the OffsetCenterOfMassShapeSettings class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 8, 2024
1 parent 66f7aec commit 6af0f59
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright (c) 2024 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 com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.enumerate.EShapeSubType;
import com.github.stephengold.joltjni.readonly.Vec3Arg;

/**
* Settings used to construct an {@code OffsetCenterOfMassShape}.
*
* @author Stephen Gold [email protected]
*/
public class OffsetCenterOfMassShapeSettings extends DecoratedShapeSettings {
// *************************************************************************
// constructors

/**
* Instantiate with the specified native object assigned but not owned.
*
* @param ocomssVa the virtual address of the native object to assign (not
* zero)
*/
OffsetCenterOfMassShapeSettings(long ocomssVa) {
super(ocomssVa);
setSubType(EShapeSubType.OffsetCenterOfMass);
}

/**
* Instantiate a settings object with the specified offset and base shape.
*
* @param offset (not null, unaffected)
* @param baseShapeRef a reference to the base shape (not null)
*/
public OffsetCenterOfMassShapeSettings(
Vec3Arg offset, ShapeRefC baseShapeRef) {
float offsetX = offset.getX();
float offsetY = offset.getY();
float offsetZ = offset.getZ();
long baseShapeRefVa = baseShapeRef.va();
long ocomssVa = createSettingsFromShapeRef(
offsetX, offsetY, offsetZ, baseShapeRefVa);
setVirtualAddress(ocomssVa, null); // no owner due to ref counting
setSubType(EShapeSubType.OffsetCenterOfMass);
}
// *************************************************************************
// new methods exposed

/**
* Copy the offset relative to the base shape. The settings are unaffected.
* (native field: mOffset)
*
* @return a new, mutable offset vector
*/
public Vec3 getOffset() {
long ocomssVa = va();
float offsetX = getOffsetX(ocomssVa);
float offsetY = getOffsetY(ocomssVa);
float offsetZ = getOffsetZ(ocomssVa);
Vec3 result = new Vec3(offsetX, offsetY, offsetZ);

return result;
}

/**
* Alter the offset relative to the base shape. (native field: mOffset)
*
* @param offset the desired offset vector (not null, unaffected,
* default=(0,0,0))
*/
public void setOffset(Vec3Arg offset) {
long ocomssVa = va();
float offsetX = offset.getX();
float offsetY = offset.getY();
float offsetZ = offset.getZ();
setOffset(ocomssVa, offsetX, offsetY, offsetZ);
}
// *************************************************************************
// native private methods

native private static long createSettingsFromShapeRef(
float offsetX, float offsetY, float offsetZ, long baseShapeRefVa);

native private static float getOffsetX(long ocomssVa);

native private static float getOffsetY(long ocomssVa);

native private static float getOffsetZ(long ocomssVa);

native private static void setOffset(
long ocomssVa, float offsetX, float offsetY, float offsetZ);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ static ShapeSettings newShapeSettings(long settingsVa) {
case MutableCompound:
result = new MutableCompoundShapeSettings(settingsVa);
break;
case OffsetCenterOfMass:
result = new OffsetCenterOfMassShapeSettings(settingsVa);
break;
case Plane:
result = new PlaneShapeSettings(settingsVa);
break;
Expand Down
99 changes: 99 additions & 0 deletions src/main/native/glue/o/OffsetCenterOfMassShapeSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright (c) 2024 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.
*/

/*
* Author: Stephen Gold
*/
#include <Jolt/Jolt.h>
#include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
#include "auto/com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings.h"
#include "glue/glue.h"

using namespace JPH;

/*
* Class: com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings
* Method: createSettingsFromShapeRef
* Signature: (FFFJ)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings_createSettingsFromShapeRef
(JNIEnv *, jclass, jfloat offsetX, jfloat offsetY, jfloat offsetZ, jlong baseShapeRefVa) {
const Vec3 offset(offsetX, offsetY, offsetZ);
const ShapeRefC * const pBaseRef
= reinterpret_cast<ShapeRefC *> (baseShapeRefVa);
OffsetCenterOfMassShapeSettings * const pResult
= new OffsetCenterOfMassShapeSettings(offset, pBaseRef->GetPtr());
TRACE_NEW("OffsetCenterOfMassShapeSettings", pResult)
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings
* Method: getOffsetX
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings_getOffsetX
(JNIEnv *, jclass, jlong ocomssVa) {
const OffsetCenterOfMassShapeSettings * const pSettings
= reinterpret_cast<OffsetCenterOfMassShapeSettings *> (ocomssVa);
const float result = pSettings->mOffset.GetX();
return result;
}

/*
* Class: com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings
* Method: getOffsetY
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings_getOffsetY
(JNIEnv *, jclass, jlong ocomssVa) {
const OffsetCenterOfMassShapeSettings * const pSettings
= reinterpret_cast<OffsetCenterOfMassShapeSettings *> (ocomssVa);
const float result = pSettings->mOffset.GetY();
return result;
}

/*
* Class: com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings
* Method: getOffsetZ
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings_getOffsetZ
(JNIEnv *, jclass, jlong ocomssVa) {
const OffsetCenterOfMassShapeSettings * const pSettings
= reinterpret_cast<OffsetCenterOfMassShapeSettings *> (ocomssVa);
const float result = pSettings->mOffset.GetZ();
return result;
}

/*
* Class: com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings
* Method: setOffset
* Signature: (JFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_OffsetCenterOfMassShapeSettings_setOffset
(JNIEnv *, jclass, jlong ocomssVa, jfloat offsetX, jfloat offsetY, jfloat offsetZ) {
OffsetCenterOfMassShapeSettings * const pSettings
= reinterpret_cast<OffsetCenterOfMassShapeSettings *> (ocomssVa);
const Vec3 offset(offsetX, offsetY, offsetZ);
pSettings->mOffset = offset;
}
46 changes: 46 additions & 0 deletions src/test/java/testjoltjni/junit/Test006.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.Jolt;
import com.github.stephengold.joltjni.MeshShapeSettings;
import com.github.stephengold.joltjni.MutableCompoundShapeSettings;
import com.github.stephengold.joltjni.OffsetCenterOfMassShapeSettings;
import com.github.stephengold.joltjni.PhysicsMaterial;
import com.github.stephengold.joltjni.Plane;
import com.github.stephengold.joltjni.PlaneShapeSettings;
Expand Down Expand Up @@ -77,6 +78,7 @@ public void test006() {
doHeightFieldShapeSettings();
doMeshShapeSettings();
doMutableCompoundShapeSettings();
doOffsetCenterOfMassShapeSettings();
doPlaneShapeSettings();
doRotatedTranslatedShapeSettings();
doSphereShapeSettings();
Expand Down Expand Up @@ -188,6 +190,21 @@ private static void doMutableCompoundShapeSettings() {
System.gc();
}

/**
* Test the {@code OffsetCenterOfMassShapeSettings} class.
*/
private static void doOffsetCenterOfMassShapeSettings() {
ShapeRefC baseShapeRef = new SphereShape(1f).toRefC();
OffsetCenterOfMassShapeSettings settings
= new OffsetCenterOfMassShapeSettings(new Vec3(), baseShapeRef);

testOffsetCenterOfMassSsDefaults(settings);
testOffsetCenterOfMassSsSetters(settings);

TestUtils.testClose(settings, baseShapeRef);
System.gc();
}

/**
* Test the {@code PlaneShapeSettings} class.
*/
Expand Down Expand Up @@ -533,6 +550,35 @@ private static void testMutableCompoundSsDefaults(
testSsDefaults(settings);
}

/**
* Test the getters and defaults of the specified
* {@code OffsetCenterOfMassShapeSettings}.
*
* @param settings the settings to test (not null, unaffected)
*/
private static void testOffsetCenterOfMassSsDefaults(
OffsetCenterOfMassShapeSettings settings) {
testSsDefaults(settings);
TestUtils.assertEquals(0f, 0f, 0f, settings.getOffset(), 0f);
}

/**
* Test the setters of the specified
* {@code OffsetCenterOfMassShapeSettings}.
*
* @param settings the settings to test (not null, modified)
*/
private static void testOffsetCenterOfMassSsSetters(
OffsetCenterOfMassShapeSettings settings) {
settings.setOffset(new Vec3(2f, 3f, 4f));

ShapeSettingsRef ref = settings.toRef();
Assert.assertEquals(1, settings.getRefCount());
Assert.assertEquals(settings, ref.getPtr());

TestUtils.assertEquals(2f, 3f, 4f, settings.getOffset(), 0f);
}

/**
* Test the getters and defaults of the specified
* {@code PlaneShapeSettings}.
Expand Down

0 comments on commit 6af0f59

Please sign in to comment.